ILIAS  trunk Revision v11.0_alpha-1769-g99a433fe2dc
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
NavigatorBridge.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
30 
32 {
36  public function getNextElementsByStep(
37  StepInterface $step,
38  BaseElementInterface ...$elements
39  ): \Generator {
40  $next_elements = [];
41  foreach ($elements as $element) {
42  $next_elements = array_merge(
43  $next_elements,
44  iterator_to_array($this->getNextElementsByName($element, $step->name()))
45  );
46  }
47 
48  foreach ($step->filters() as $filter) {
49  switch ($filter->type()) {
50  case FilterType::NULL:
51  break;
52 
53  case FilterType::MDID:
54  $next_elements = $this->filterByMDID(
55  $filter,
56  ...$next_elements
57  );
58  break;
59 
60  case FilterType::INDEX:
61  $next_elements = $this->filterByIndex(
62  $filter,
63  ...$next_elements
64  );
65  break;
66 
67  case FilterType::DATA:
68  $next_elements = $this->filterByData(
69  $filter,
70  ...$next_elements
71  );
72  break;
73  }
74  }
75 
76  yield from $next_elements;
77  }
78 
79  public function getParents(
80  BaseElementInterface ...$elements
81  ): \Generator {
82  $next_elements = [];
83  foreach ($elements as $element) {
84  $parent = $element->getSuperElement();
85  if (!in_array($parent, $next_elements, true)) {
86  $next_elements[] = $parent;
87  }
88  }
89  yield from $next_elements;
90  }
91 
95  protected function getNextElementsByName(
96  BaseElementInterface $element,
97  string|StepToken $name
98  ): \Generator {
99  if ($name === StepToken::SUPER) {
100  if ($super = $element->getSuperElement()) {
101  yield $super;
102  }
103  return;
104  }
105 
106  foreach ($element->getSubElements() as $sub) {
107  if (strtolower($sub->getDefinition()->name()) === strtolower($name)) {
108  yield $sub;
109  }
110  }
111  }
112 
116  protected function filterByMDID(
117  FilterInterface $filter,
118  BaseElementInterface ...$elements
119  ): \Generator {
120  foreach ($elements as $element) {
121  if ($element instanceof StructureElementInterface) {
122  yield $element;
123  continue;
124  }
125  $id = $element->getMDID();
126  $id = is_int($id) ? (string) $id : $id->value;
127  if (in_array($id, iterator_to_array($filter->values()), true)) {
128  yield $element;
129  }
130  }
131  }
132 
136  protected function filterByIndex(
137  FilterInterface $filter,
138  BaseElementInterface ...$elements
139  ): \Generator {
140  $index = 0;
141  $filter_values = [];
142  $select_last = false;
143  foreach ($filter->values() as $value) {
144  if (preg_match('/^\d+$/', $value)) {
145  $filter_values[] = (int) $value;
146  } else {
147  $select_last = true;
148  }
149  }
150  foreach ($elements as $element) {
151  if (
152  in_array($index, $filter_values, true) ||
153  ($select_last && array_key_last($elements) === $index) ||
154  $element instanceof StructureElementInterface
155  ) {
156  yield $element;
157  }
158  $index++;
159  }
160  }
161 
165  protected function filterByData(
166  FilterInterface $filter,
167  BaseElementInterface ...$elements
168  ): \Generator {
169  foreach ($elements as $element) {
170  if (!($element instanceof ElementInterface)) {
171  yield $element;
172  continue;
173  }
174  $data = $element->getData()->value();
175  if ($element instanceof MarkableInterface && $element->isMarked()) {
176  $data = $element->getMarker()->dataValue();
177  }
178  if (in_array($data, iterator_to_array($filter->values()), true)) {
179  yield $element;
180  }
181  }
182  }
183 }
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
if(!file_exists('../ilias.ini.php'))
filters()
Filters restrict the elements a step leads to.
getNextElementsByName(BaseElementInterface $element, string|StepToken $name)
filterByIndex(FilterInterface $filter, BaseElementInterface ... $elements)
getParents(BaseElementInterface ... $elements)
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.