ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
NavigatorBridge.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
29 
31 {
35  public function getNextElementsByStep(
36  StepInterface $step,
37  BaseElementInterface ...$elements
38  ): \Generator {
39  $next_elements = [];
40  foreach ($elements as $element) {
41  $next_elements = array_merge(
42  $next_elements,
43  iterator_to_array($this->getNextElementsByName($element, $step->name()))
44  );
45  }
46 
47  foreach ($step->filters() as $filter) {
48  switch ($filter->type()) {
49  case FilterType::NULL:
50  break;
51 
52  case FilterType::MDID:
53  $next_elements = $this->filterByMDID(
54  $filter,
55  ...$next_elements
56  );
57  break;
58 
59  case FilterType::INDEX:
60  $next_elements = $this->filterByIndex(
61  $filter,
62  ...$next_elements
63  );
64  break;
65 
66  case FilterType::DATA:
67  $next_elements = $this->filterByData(
68  $filter,
69  ...$next_elements
70  );
71  break;
72  }
73  }
74 
75  yield from $next_elements;
76  }
77 
78  public function getParents(
79  BaseElementInterface ...$elements
80  ): \Generator {
81  $next_elements = [];
82  foreach ($elements as $element) {
83  $parent = $element->getSuperElement();
84  if (!in_array($parent, $next_elements, true)) {
85  $next_elements[] = $parent;
86  }
87  }
88  yield from $next_elements;
89  }
90 
94  protected function getNextElementsByName(
95  BaseElementInterface $element,
96  string|StepToken $name
97  ): \Generator {
98  if ($name === StepToken::SUPER) {
99  if ($super = $element->getSuperElement()) {
100  yield $super;
101  }
102  return;
103  }
104 
105  foreach ($element->getSubElements() as $sub) {
106  if (strtolower($sub->getDefinition()->name()) === strtolower($name)) {
107  yield $sub;
108  }
109  }
110  }
111 
115  protected function filterByMDID(
116  FilterInterface $filter,
117  BaseElementInterface ...$elements
118  ): \Generator {
119  foreach ($elements as $element) {
120  $id = $element->getMDID();
121  $id = is_int($id) ? (string) $id : $id->value;
122  if ($element instanceof StructureElement) {
123  yield $element;
124  }
125  if (in_array($id, iterator_to_array($filter->values()), true)) {
126  yield $element;
127  }
128  }
129  }
130 
134  protected function filterByIndex(
135  FilterInterface $filter,
136  BaseElementInterface ...$elements
137  ): \Generator {
138  $index = 0;
139  $filter_values = [];
140  $select_last = false;
141  foreach ($filter->values() as $value) {
142  if (preg_match('/^\d+$/', $value)) {
143  $filter_values[] = (int) $value;
144  } else {
145  $select_last = true;
146  }
147  }
148  foreach ($elements as $element) {
149  if (
150  in_array($index, $filter_values, true) ||
151  ($select_last && array_key_last($elements) === $index)
152  ) {
153  yield $element;
154  }
155  $index++;
156  }
157  }
158 
162  protected function filterByData(
163  FilterInterface $filter,
164  BaseElementInterface ...$elements
165  ): \Generator {
166  foreach ($elements as $element) {
167  if (!($element instanceof ElementInterface)) {
168  continue;
169  }
170  $data = $element->getData()->value();
171  if ($element instanceof MarkableInterface && $element->isMarked()) {
172  $data = $element->getMarker()->dataValue();
173  }
174  if (in_array($data, iterator_to_array($filter->values()), true)) {
175  yield $element;
176  }
177  }
178  }
179 }
getNextElementsByStep(StepInterface $step, BaseElementInterface ... $elements)
filterByMDID(FilterInterface $filter, BaseElementInterface ... $elements)
values()
When there are multiple values, an element passes the filter if it would pass it for one of the value...
FilterType
Values should always be all lowercase.
Definition: FilterType.php:26
filters()
Filters restrict the elements a step leads to.
getNextElementsByName(BaseElementInterface $element, string|StepToken $name)
filterByIndex(FilterInterface $filter, BaseElementInterface ... $elements)
getParents(BaseElementInterface ... $elements)
if(!file_exists(getcwd() . '/ilias.ini.php'))
Definition: confirmReg.php:21
filterByData(FilterInterface $filter, BaseElementInterface ... $elements)
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:27
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
isMarked()
Elements can be marked to be created, updated or deleted.