ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
Builder.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
30
31class Builder implements BuilderInterface
32{
34
36 {
37 $this->structure = $structure;
38 }
39
43 protected array $steps = [];
44
45 protected string|StepToken|null $current_step_name = null;
46
50 protected array $current_step_filters = [];
51 protected bool $current_add_as_first = false;
52
53 protected bool $is_relative = false;
54 protected bool $leads_to_one = false;
55
57 {
58 $clone = clone $this;
59 $clone->is_relative = $is_relative;
60 return $clone;
61 }
62
64 bool $leads_to_one
66 $clone = clone $this;
67 $clone->leads_to_one = $leads_to_one;
68 return $clone;
69 }
70
71 public function withNextStep(
72 string $name,
73 bool $add_as_first = false
75 return $this->withNextStepFromName(
76 $name,
77 $add_as_first
78 );
79 }
80
81 public function withNextStepToSuperElement(bool $add_as_first = false): BuilderInterface
82 {
83 return $this->withNextStepFromName(
85 $add_as_first
86 );
87 }
88
89 public function withNextStepFromStep(
90 StepInterface $next_step,
91 bool $add_as_first = false
93 $builder = $this->withNextStepFromName($next_step->name(), $add_as_first);
94 foreach ($next_step->filters() as $filter) {
95 $builder = $builder->withAdditionalFilterAtCurrentStep(
96 $filter->type(),
97 ...$filter->values()
98 );
99 }
100 return $builder;
101 }
102
107 FilterType $type,
108 string ...$values
110 if (!isset($this->current_step_name)) {
111 throw new \ilMDPathException(
112 'Cannot add filter because there is no current step.'
113 );
114 }
115 $clone = clone $this;
116 $clone->current_step_filters[] = new Filter(
117 $type,
118 ...$values
119 );
120 return $clone;
121 }
122
126 public function get(): PathInterface
127 {
128 return $this->getFinishedPath(true);
129 }
130
135 {
136 return $this->getFinishedPath(false);
137 }
138
139 protected function getFinishedPath(bool $validate): PathInterface
140 {
141 $clone = $this->withCurrentStepSaved();
142 $path = new Path(
143 $clone->is_relative,
144 $clone->leads_to_one,
145 ...$clone->steps
146 );
147
148 if ($validate && !$path->isRelative()) {
149 $this->validatePathFromRoot($path);
150 }
151 return $path;
152 }
153
157 protected function validatePathFromRoot(PathInterface $path): void
158 {
159 $element = $this->structure->getRoot();
160 foreach ($path->steps() as $step) {
161 $name = $step->name();
162 if ($name === StepToken::SUPER) {
163 $element = $element->getSuperElement();
164 } else {
165 $element = $element->getSubElement($name);
166 }
167 if (is_null($element)) {
168 $name = is_string($name) ? $name : $name->value;
169 throw new \ilMDPathException(
170 "In the path '" . $path->toString() . "', the step '" . $name . "' is invalid."
171 );
172 }
173 }
174 }
175
176 protected function withNextStepFromName(
177 string|StepToken $name,
178 bool $add_as_first = false
180 $clone = $this->withCurrentStepSaved();
181 $clone->current_step_name = $name;
182 $clone->current_add_as_first = $add_as_first;
183 return $clone;
184 }
185
186 protected function withCurrentStepSaved(): Builder
187 {
188 $clone = clone $this;
189 if (!isset($clone->current_step_name)) {
190 return $clone;
191 }
192
193 $new_step = new Step(
194 $clone->current_step_name,
195 ...$clone->current_step_filters
196 );
197 if ($clone->current_add_as_first) {
198 array_unshift($clone->steps, $new_step);
199 } else {
200 $clone->steps[] = $new_step;
201 }
202
203 $clone->current_step_name = null;
204 $clone->current_step_filters = [];
205 $clone->current_add_as_first = false;
206
207 return $clone;
208 }
209}
withAdditionalFilterAtCurrentStep(FilterType $type, string ... $values)
Definition: Builder.php:106
getFinishedPath(bool $validate)
Definition: Builder.php:139
withRelative(bool $is_relative)
Relative paths start at some otherwise determined element, absolute paths start at root.
Definition: Builder.php:56
withLeadsToExactlyOneElement(bool $leads_to_one)
Building a path that is flagged to lead to exactly one element, but does not actually do so can throw...
Definition: Builder.php:63
validatePathFromRoot(PathInterface $path)
Definition: Builder.php:157
string StepToken null $current_step_name
Definition: Builder.php:45
withNextStep(string $name, bool $add_as_first=false)
Add the next step to the path.
Definition: Builder.php:71
withNextStepFromStep(StepInterface $next_step, bool $add_as_first=false)
Definition: Builder.php:89
__construct(StructureSetInterface $structure)
Definition: Builder.php:35
StructureSetInterface $structure
Definition: Builder.php:33
withNextStepFromName(string|StepToken $name, bool $add_as_first=false)
Definition: Builder.php:176
withNextStepToSuperElement(bool $add_as_first=false)
Add going to the super element as the next step to the path.
Definition: Builder.php:81
return['delivery_method'=> 'php',]
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
withAdditionalFilterAtCurrentStep(FilterType $type, string ... $values)
Adds a filter to the current step, restricting what elements are included in it:
filters()
Filters restrict the elements a step leads to.
$path
Definition: ltiservices.php:30
FilterType
Values should always be all lowercase.
Definition: FilterType.php:27
StepToken
The string representation of these tokens must not occur as names of metadata elements.
Definition: StepToken.php:28
if(!file_exists('../ilias.ini.php'))