ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
LOMVocabularies.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
31 
33 {
36 
38 
39  public function __construct(
40  DictionaryInitiatorInterface $dictionary_initiator,
41  NavigatorFactoryInterface $navigator_factory
42  ) {
43  $this->initiator = $dictionary_initiator;
44  $this->navigator_factory = $navigator_factory;
45  }
46 
50  public function vocabulariesForElement(
51  BaseElementInterface $element
52  ): \Generator {
53  if (!isset($this->dictionary)) {
54  $this->dictionary = $this->initiator->get();
55  }
56  foreach ($this->dictionary->tagsForElement($element) as $tag) {
57  yield $tag->vocabulary();
58  }
59  }
60 
65  ElementInterface $element,
66  bool $ignore_marker
67  ): \Generator {
68  $is_vocab_value = false;
69  $source = null;
70  if ($element->getDefinition()->dataType() === Type::VOCAB_VALUE) {
71  $is_vocab_value = true;
72  $source = $this->getDataValueFromRelativePath(
73  $element,
74  $this->initiator->pathFromValueToSource(),
75  $ignore_marker
76  );
77  }
78  foreach ($this->vocabulariesForElement($element) as $vocab) {
79  if ($is_vocab_value && $vocab->source() !== $source) {
80  continue;
81  }
82  if (!$this->checkCondition($element, $vocab, $ignore_marker)) {
83  continue;
84  }
85  yield $vocab;
86  }
87  }
88 
89  protected function checkCondition(
90  ElementInterface $element,
91  VocabularyInterface $vocabulary,
92  bool $ignore_marker
93  ): bool {
94  if (!$vocabulary->isConditional()) {
95  return true;
96  }
97  $value = $this->getDataValueFromRelativePath(
98  $element,
99  $vocabulary->condition()->path(),
100  $ignore_marker
101  );
102  if ($value === $vocabulary->condition()->value()) {
103  return true;
104  }
105  return false;
106  }
107 
108  protected function getDataValueFromRelativePath(
109  ElementInterface $start,
111  bool $ignore_marker
112  ): ?string {
113  $element = $this->navigator_factory->navigator(
114  $path,
115  $start
116  )->lastElementAtFinalStep();
117 
118  if (!isset($element)) {
119  return null;
120  }
121  if (
122  !$ignore_marker &&
123  ($element instanceof MarkableInterface) &&
124  $element->isMarked()
125  ) {
126  return $element->getMarker()->dataValue();
127  }
128  return $element->getData()->value();
129  }
130 }
NavigatorFactoryInterface $navigator_factory
__construct(DictionaryInitiatorInterface $dictionary_initiator, NavigatorFactoryInterface $navigator_factory)
filteredVocabulariesForElement(ElementInterface $element, bool $ignore_marker)
checkCondition(ElementInterface $element, VocabularyInterface $vocabulary, bool $ignore_marker)
$path
Definition: ltiservices.php:32
DictionaryInitiatorInterface $initiator
vocabulariesForElement(BaseElementInterface $element)
getDataValueFromRelativePath(ElementInterface $start, PathInterface $path, bool $ignore_marker)
$vocab
getDefinition()
Defining properties of the metadata element.
condition()
Contains the path to the element this vocabulary is conditional on, and the value the element needs t...
isConditional()
Some vocabularies are only available if a different MD element has a certain value.