ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
PathConditionsCollection.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
27
29{
30 protected PathFactory $path_factory;
34 protected array $history;
38 protected array $path_conditions;
39 protected bool $is_relative;
41
42 public function __construct(PathFactory $path_factory, PathInterface $path_interface)
43 {
44 $this->path_factory = $path_factory;
45 $this->history = [];
46 $this->path_conditions = [];
47 $this->is_relative = $path_interface->isRelative();
48 $this->leads_to_exactly_one_element = $path_interface->leadsToExactlyOneElement();
49 $this->buildPathConditionsDict($path_interface);
50 }
51
52 public function getConditionPathByStepName(string $name): PathInterface
53 {
54 if ($this->hasPathConditionWithName($name)) {
55 return $this->path_conditions[$name];
56 }
57 return $this->buildPathFromSteps([], true, false);
58 }
59
61 {
62 return $this->buildPathFromSteps(
63 $this->history,
64 $this->is_relative,
65 $this->leads_to_exactly_one_element
66 );
67 }
68
72 protected function buildPathFromSteps(array $steps, bool $is_relative, bool $leads_to_exactly_one): PathInterface
73 {
74 $builder = $this->path_factory->custom()
75 ->withRelative($is_relative)
76 ->withLeadsToExactlyOneElement($leads_to_exactly_one);
77 foreach ($steps as $step) {
78 $builder = $builder->withNextStepFromStep($step, false);
79 }
80 return $builder->get();
81 }
82
83 protected function isStepUpCommand(StepInterface $step): bool
84 {
85 return $step->name() === StepToken::SUPER;
86 }
87
88 protected function addStepToHistory(StepInterface $step): void
89 {
90 $this->history[] = $step;
91 }
92
93 protected function buildPathConditionsDict(PathInterface $path_interface): void
94 {
98 foreach ($path_interface->steps() as $step) {
99 $this->addStepToHistory($step);
100 if ($this->isStepUpCommand($step)) {
101 $this->buildPathCondition();
102 }
103 }
104 }
105
106 protected function hasPathConditionWithName(string $name): bool
107 {
108 return array_key_exists($name, $this->path_conditions);
109 }
110
111 protected function buildPathCondition(): void
112 {
122 $step_up = array_pop($this->history);
123 $step_condition = array_pop($this->history);
124 $target = $this->history[count($this->history) - 1];
125 $steps = [$step_condition, $step_up];
126
127 // nested condition path found, wrap
128 if ($this->hasPathConditionWithName($step_condition->name())) {
129 $nested_condition_path = $this->path_conditions[$step_condition->name()];
130 array_pop($steps); // Remove step up before adding steps
131 foreach ($nested_condition_path->steps() as $step) {
132 $steps[] = $step;
133 }
134 $steps[] = $step_up;
135 unset($this->path_conditions[$step_condition->name()]);
136 }
137
138 // target has path, append
139 if ($this->hasPathConditionWithName($target->name())) {
140 $existing_condition_path = $this->path_conditions[$target->name()];
141 $frontConditions = [];
142 foreach ($existing_condition_path->steps() as $step) {
143 $frontConditions[] = $step;
144 }
145 array_unshift($steps, ...$frontConditions);
146 unset($this->path_conditions[$target->name()]);
147 }
148
149 $this->path_conditions[$target->name()] = $this->buildPathFromSteps(
150 $steps,
151 true,
152 false
153 );
154 }
155}
__construct(PathFactory $path_factory, PathInterface $path_interface)
buildPathFromSteps(array $steps, bool $is_relative, bool $leads_to_exactly_one)
isRelative()
Relative paths start at some otherwise determined element, absolute paths start at root.
steps()
Get all steps in the path.
leadsToExactlyOneElement()
Specifies whether the path should point to exactly one element, or whether it can also lead to no or ...
name()
Steps are identified by the names of LOM elements, or a token to specify a step to the super-element.
StepToken
The string representation of these tokens must not occur as names of metadata elements.
Definition: StepToken.php:28