ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
Dictionary.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
25use ILIAS\MetaData\Paths\FactoryInterface as PathFactoryInterface;
31
32abstract class Dictionary
33{
34 protected PathFactoryInterface $path_factory;
36
40 private array $tag_assignments = [];
41
42 public function __construct(
43 PathFactoryInterface $path_factory,
46 ) {
47 $this->path_factory = $path_factory;
48 $this->navigator_factory = $navigator_factory;
49 foreach ($tag_assignments as $tag_assignment) {
50 $this->tag_assignments[$tag_assignment->path()->toString()][] = $tag_assignment->tag();
51 }
52 }
53
59 public function tagsForElement(
61 ): \Generator {
62 $path = $this->path_factory->toElement($element);
63 foreach ($this->getAllTagsForElement($path) as $tag) {
64 if (!$this->doesIndexMatch($path, $element, $tag)) {
65 continue;
66 }
67 yield $tag;
68 }
69 }
70
74 protected function getAllTagsForElement(
76 ): \Generator {
77 yield from $this->tag_assignments[$path->toString()] ?? [];
78 }
79
80 protected function doesIndexMatch(
82 BaseElementInterface $element,
83 TagInterface $tag
84 ): bool {
85 if (!($element instanceof ElementInterface)) {
86 return true;
87 }
88 if (!$tag->isRestrictedToIndices()) {
89 return true;
90 }
91 $index = $this->findIndexOfElement($path, $element);
92 if (in_array($index, iterator_to_array($tag->indices()), true)) {
93 return true;
94 }
95 return false;
96 }
97
98 protected function findIndexOfElement(
100 ElementInterface $element
101 ): int {
102 $name = $element->getDefinition()->name();
103 $navigator = $this->navigator_factory->navigator($path, $element);
104 $index = 0;
105 foreach ($navigator->elementsAtFinalStep() as $sibling_element) {
106 if ($sibling_element === $element) {
107 return $index;
108 }
109 $index++;
110 }
111 throw new \ilMDStructureException('Invalid metadata set');
112 }
113}
tagsForElement(BaseElementInterface $element)
If possible, takes into account the index of elements when finding tags (beginning with 0).
Definition: Dictionary.php:59
doesIndexMatch(PathInterface $path, BaseElementInterface $element, TagInterface $tag)
Definition: Dictionary.php:80
findIndexOfElement(PathInterface $path, ElementInterface $element)
Definition: Dictionary.php:98
__construct(PathFactoryInterface $path_factory, NavigatorFactoryInterface $navigator_factory, TagAssignmentInterface ... $tag_assignments)
Definition: Dictionary.php:42
NavigatorFactoryInterface $navigator_factory
Definition: Dictionary.php:35
$path
Definition: ltiservices.php:30
if(!file_exists('../ilias.ini.php'))