ILIAS  trunk Revision v11.0_alpha-1744-gb0451eebef4
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
Elements.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
30 
31 class Elements implements ElementsInterface
32 {
33  protected const string SEPARATOR = ': ';
34  protected const string DELIMITER = ', ';
35 
36  protected DataInterface $data;
40 
41  public function __construct(
42  DataInterface $data,
43  DictionaryInterface $dictionary,
44  NavigatorFactoryInterface $navigator_factory,
45  ElementsPresentation $elements
46  ) {
47  $this->data = $data;
48  $this->dictionary = $dictionary;
49  $this->navigator_factory = $navigator_factory;
50  $this->elements = $elements;
51  }
52 
53  public function nameWithRepresentation(
54  bool $plural,
55  ElementInterface ...$elements
56  ): string {
57  if (empty($elements)) {
58  return '';
59  }
60  $name = $this->name(
61  $elements[0],
62  $plural
63  );
64  $tag = $this->dictionary->tagForElement($elements[0]);
65  if ($tag?->hasRepresentation()) {
66  $values = $this->getDataValueStringByPath(
67  $tag->representation(),
68  ...$elements
69  );
70  if ($values !== '') {
71  $name = implode(self::SEPARATOR, [$name, $values]);
72  }
73  }
74  return $name;
75  }
76 
77  public function preview(ElementInterface ...$elements): string
78  {
79  if (empty($elements)) {
80  return '';
81  }
82  $tag = $this->dictionary->tagForElement($elements[0]);
83  if (!$tag?->hasPreview()) {
84  return '';
85  }
86  return $this->getDataValueStringByPath($tag->preview(), ...$elements);
87  }
88 
89  public function name(
90  BaseElementInterface $element,
91  bool $plural = false
92  ): string {
93  return $this->elements->name($element, $plural);
94  }
95 
96  public function nameWithParents(
97  BaseElementInterface $element,
98  ?BaseElementInterface $cut_off = null,
99  bool $plural = false,
100  bool $never_skip_initial = false
101  ): string {
102  //skip the name of the element if it does not add any information
103  $skip_arr = [Type::VOCAB_VALUE, Type::DURATION, Type::DATETIME, Type::STRING];
104  $skip_initial =
105  !$never_skip_initial &&
106  !$this->dictionary->tagForElement($element)?->isLabelImportant() &&
107  in_array($element->getDefinition()->dataType(), $skip_arr);
108 
109  return $this->elements->nameWithParents(
110  $element,
111  $cut_off,
112  $plural,
113  $skip_initial
114  );
115  }
116 
117  protected function getDataValueStringByPath(
119  ElementInterface ...$elements
120  ): string {
121  $values = [];
122  foreach ($elements as $element) {
123  $navigator = $this->navigator_factory->navigator(
124  $path,
125  $element
126  );
127  foreach ($navigator->elementsAtFinalStep() as $el) {
128  if (
129  ($data = $el->getData())->type() !== Type::NULL &&
130  $data->value() !== ''
131  ) {
132  $values[] = $this->data->dataValue($data);
133  }
134  }
135  }
136  return implode(self::DELIMITER, $values);
137  }
138 }
NavigatorFactoryInterface $navigator_factory
Definition: Elements.php:38
preview(ElementInterface ... $elements)
Definition: Elements.php:77
$path
Definition: ltiservices.php:29
getDataValueStringByPath(PathInterface $path, ElementInterface ... $elements)
Definition: Elements.php:117
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
name(BaseElementInterface $element, bool $plural=false)
Definition: Elements.php:89
nameWithRepresentation(bool $plural, ElementInterface ... $elements)
Definition: Elements.php:53
getDefinition()
Defining properties of the metadata element.
__construct(DataInterface $data, DictionaryInterface $dictionary, NavigatorFactoryInterface $navigator_factory, ElementsPresentation $elements)
Definition: Elements.php:41
nameWithParents(BaseElementInterface $element, ?BaseElementInterface $cut_off=null, bool $plural=false, bool $never_skip_initial=false)
Definition: Elements.php:96