ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
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 
104  FilterType $type,
105  string ...$values
106  ): BuilderInterface {
107  if (!isset($this->current_step_name)) {
108  throw new \ilMDPathException(
109  'Cannot add filter because there is no current step.'
110  );
111  }
112  $clone = clone $this;
113  $clone->current_step_filters[] = new Filter(
114  $type,
115  ...$values
116  );
117  return $clone;
118  }
119 
123  public function get(): PathInterface
124  {
125  $clone = $this->withCurrentStepSaved();
126  $path = new Path(
127  $clone->is_relative,
128  $clone->leads_to_one,
129  ...$clone->steps
130  );
131 
132  if (!$path->isRelative()) {
133  $this->validatePathFromRoot($path);
134  }
135  return $path;
136  }
137 
141  protected function validatePathFromRoot(PathInterface $path): void
142  {
143  $element = $this->structure->getRoot();
144  foreach ($path->steps() as $step) {
145  $name = $step->name();
146  if ($name === StepToken::SUPER) {
147  $element = $element->getSuperElement();
148  } else {
149  $element = $element->getSubElement($name);
150  }
151  if (is_null($element)) {
152  $name = is_string($name) ? $name : $name->value;
153  throw new \ilMDPathException(
154  "In the path '" . $path->toString() . "', the step '" . $name . "' is invalid."
155  );
156  }
157  }
158  }
159 
160  protected function withNextStepFromName(
161  string|StepToken $name,
162  bool $add_as_first = false
163  ): BuilderInterface {
164  $clone = $this->withCurrentStepSaved();
165  $clone->current_step_name = $name;
166  $clone->current_add_as_first = $add_as_first;
167  return $clone;
168  }
169 
170  protected function withCurrentStepSaved(): Builder
171  {
172  $clone = clone $this;
173  if (!isset($clone->current_step_name)) {
174  return $clone;
175  }
176 
177  $new_step = new Step(
178  $clone->current_step_name,
179  ...$clone->current_step_filters
180  );
181  if ($clone->current_add_as_first) {
182  array_unshift($clone->steps, $new_step);
183  } else {
184  $clone->steps[] = $new_step;
185  }
186 
187  $clone->current_step_name = null;
188  $clone->current_step_filters = [];
189  $clone->current_add_as_first = false;
190 
191  return $clone;
192  }
193 }
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:32
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)
Adds a filter to the current step, restricting what elements are included in it:
Definition: Builder.php:103
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:160
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:141