ILIAS  trunk Revision v11.0_alpha-2658-ge2404539063
Adapter.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
28 
29 class Adapter implements AdapterInterface
30 {
33 
34  protected array $cached_vocabularies_by_slot = [];
35 
36  public function __construct(
37  ReaderInterface $reader,
38  ElementHelperInterface $element_helper
39  ) {
40  $this->reader = $reader;
41  $this->element_helper = $element_helper;
42  }
43 
44  public function findElementOfCondition(
45  SlotIdentifier $slot,
46  ElementInterface $element,
47  ElementInterface ...$all_elements
48  ): ?ElementInterface {
49  return $this->element_helper->findElementOfCondition($slot, $element, ...$all_elements);
50  }
51 
52  public function slotForElement(ElementInterface $element): SlotIdentifier
53  {
54  return $this->element_helper->slotForElement($element);
55  }
56 
61  {
62  return $this->element_helper->slotsForElementWithoutCondition($element);
63  }
64 
66  ElementInterface $element,
67  ElementInterface $element_in_condition,
68  string $value
69  ): SlotIdentifier {
70  return $this->element_helper->potentialSlotForElementByCondition(
71  $element,
72  $element_in_condition,
73  $value
74  );
75  }
76 
80  protected function vocabulariesForSlot(
81  SlotIdentifier $slot
82  ): \Generator {
83  if (!isset($this->cached_vocabularies_by_slot[$slot->value])) {
84  $this->cached_vocabularies_by_slot[$slot->value] = iterator_to_array(
85  $this->reader->activeVocabulariesForSlots($slot),
86  false
87  );
88  }
89  yield from $this->cached_vocabularies_by_slot[$slot->value];
90  }
91 
92  public function doesSlotHaveVocabularies(SlotIdentifier $slot): bool
93  {
94  return $this->vocabulariesForSlot($slot)->current() !== null;
95  }
96 
97  public function doesSlotAllowCustomInput(SlotIdentifier $slot): bool
98  {
99  foreach ($this->vocabulariesForSlot($slot) as $vocab) {
100  if (!$vocab->allowsCustomInputs()) {
101  return false;
102  }
103  }
104  return true;
105  }
106 
108  SlotIdentifier $slot,
109  string $value
110  ): bool {
111  foreach ($this->vocabulariesForSlot($slot) as $vocab) {
112  if (in_array($value, iterator_to_array($vocab->values()), true)) {
113  return true;
114  }
115  }
116  return false;
117  }
118 
123  SlotIdentifier $slot,
124  ?string $add_if_not_included = null
125  ): \Generator {
126  $values = [];
127  foreach ($this->vocabulariesForSlot($slot) as $vocab) {
128  $values_from_vocab = iterator_to_array($vocab->values());
129  $values = array_merge($values, $values_from_vocab);
130  }
131 
132  if (isset($add_if_not_included) && !in_array($add_if_not_included, $values)) {
133  array_unshift($values, $add_if_not_included);
134  }
135  yield from $values;
136  }
137 
138  public function sourceMapForSlot(SlotIdentifier $slot): \Closure
139  {
140  $sources_by_value = [];
141  foreach ($this->vocabulariesForSlot($slot) as $vocab) {
142  $values_from_vocab = iterator_to_array($vocab->values());
143  $sources_by_value = array_merge(
144  $sources_by_value,
145  array_fill_keys($values_from_vocab, $vocab->source())
146  );
147  }
148 
149  return function (string $value) use ($sources_by_value) {
150  return $sources_by_value[$value] ?? null;
151  };
152  }
153 }
doesSlotHaveVocabularies(SlotIdentifier $slot)
Definition: Adapter.php:92
slotsForElementWithoutCondition(ElementInterface $element)
Definition: Adapter.php:60
slotForElement(ElementInterface $element)
Definition: Adapter.php:52
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
sourceMapForSlot(SlotIdentifier $slot)
Returned closure takes a string value as argument, and returns the source of the vocabulary it&#39;s from...
Definition: Adapter.php:138
isValueInVocabulariesForSlot(SlotIdentifier $slot, string $value)
Definition: Adapter.php:107
valuesInVocabulariesForSlot(SlotIdentifier $slot, ?string $add_if_not_included=null)
Definition: Adapter.php:122
findElementOfCondition(SlotIdentifier $slot, ElementInterface $element, ElementInterface ... $all_elements)
Definition: Adapter.php:44
$vocab
vocabulariesForSlot(SlotIdentifier $slot)
Definition: Adapter.php:80
__construct(ReaderInterface $reader, ElementHelperInterface $element_helper)
Definition: Adapter.php:36
doesSlotAllowCustomInput(SlotIdentifier $slot)
Definition: Adapter.php:97
ElementHelperInterface $element_helper
Definition: Adapter.php:32
potentialSlotForElementByCondition(ElementInterface $element, ElementInterface $element_in_condition, string $value)
Definition: Adapter.php:65