ILIAS  trunk Revision v11.0_alpha-1723-g8e69f309bab
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
Builder.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
30 
31 class Builder implements BuilderInterface
32 {
34 
35  public function __construct(StructureSetInterface $structure)
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 
56  public function withRelative(bool $is_relative): BuilderInterface
57  {
58  $clone = clone $this;
59  $clone->is_relative = $is_relative;
60  return $clone;
61  }
62 
64  bool $leads_to_one
65  ): BuilderInterface {
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
74  ): BuilderInterface {
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
92  ): BuilderInterface {
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
109  ): BuilderInterface {
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  $clone = $this->withCurrentStepSaved();
129  $path = new Path(
130  $clone->is_relative,
131  $clone->leads_to_one,
132  ...$clone->steps
133  );
134 
135  if (!$path->isRelative()) {
136  $this->validatePathFromRoot($path);
137  }
138  return $path;
139  }
140 
144  protected function validatePathFromRoot(PathInterface $path): void
145  {
146  $element = $this->structure->getRoot();
147  foreach ($path->steps() as $step) {
148  $name = $step->name();
149  if ($name === StepToken::SUPER) {
150  $element = $element->getSuperElement();
151  } else {
152  $element = $element->getSubElement($name);
153  }
154  if (is_null($element)) {
155  $name = is_string($name) ? $name : $name->value;
156  throw new \ilMDPathException(
157  "In the path '" . $path->toString() . "', the step '" . $name . "' is invalid."
158  );
159  }
160  }
161  }
162 
163  protected function withNextStepFromName(
164  string|StepToken $name,
165  bool $add_as_first = false
166  ): BuilderInterface {
167  $clone = $this->withCurrentStepSaved();
168  $clone->current_step_name = $name;
169  $clone->current_add_as_first = $add_as_first;
170  return $clone;
171  }
172 
173  protected function withCurrentStepSaved(): Builder
174  {
175  $clone = clone $this;
176  if (!isset($clone->current_step_name)) {
177  return $clone;
178  }
179 
180  $new_step = new Step(
181  $clone->current_step_name,
182  ...$clone->current_step_filters
183  );
184  if ($clone->current_add_as_first) {
185  array_unshift($clone->steps, $new_step);
186  } else {
187  $clone->steps[] = $new_step;
188  }
189 
190  $clone->current_step_name = null;
191  $clone->current_step_filters = [];
192  $clone->current_add_as_first = false;
193 
194  return $clone;
195  }
196 }
steps()
Get all steps in the path.
withNextStepToSuperElement(bool $add_as_first=false)
Add going to the super element as the next step to the path.
Definition: Builder.php:81
FilterType
Values should always be all lowercase.
Definition: FilterType.php:26
filters()
Filters restrict the elements a step leads to.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
__construct(StructureSetInterface $structure)
Definition: Builder.php:35
$path
Definition: ltiservices.php:29
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
withRelative(bool $is_relative)
Relative paths start at some otherwise determined element, absolute paths start at root...
Definition: Builder.php:56
StructureSetInterface $structure
Definition: Builder.php:33
name()
Steps are identified by the names of LOM elements, or a token to specify a step to the super-element...
withAdditionalFilterAtCurrentStep(FilterType $type, string ... $values)
Definition: Builder.php:106
StepToken
The string representation of these tokens must not occur as names of metadata elements.
Definition: StepToken.php:27
withNextStepFromStep(StepInterface $next_step, bool $add_as_first=false)
Definition: Builder.php:89
string StepToken null $current_step_name
Definition: Builder.php:45
withNextStepFromName(string|StepToken $name, bool $add_as_first=false)
Definition: Builder.php:163
withNextStep(string $name, bool $add_as_first=false)
Add the next step to the path.
Definition: Builder.php:71
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:144