ILIAS  trunk Revision v11.0_alpha-1702-gfd3ecb7f852
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
Element.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
21 namespace ILIAS\MetaData\Elements;
22 
33 
34 class Element extends BaseElement implements ElementInterface
35 {
38 
39  public function __construct(
40  NoID|int $md_id,
42  DataInterface $data,
44  ) {
45  $this->data = $data;
46  parent::__construct($md_id, $definition, ...$sub_elements);
47  }
48 
49  public function getData(): DataInterface
50  {
51  return $this->data;
52  }
53 
54  public function isScaffold(): bool
55  {
56  return $this->getMDID() === NoID::SCAFFOLD;
57  }
58 
59  public function getSuperElement(): ?Element
60  {
61  $super = parent::getSuperElement();
62  if (!isset($super) || ($super instanceof Element)) {
63  return $super;
64  }
65  throw new \ilMDElementsException(
66  'Metadata element has invalid super-element.'
67  );
68  }
69 
73  public function getSubElements(): \Generator
74  {
75  foreach (parent::getSubElements() as $sub_element) {
76  if (!($sub_element instanceof Element)) {
77  throw new \ilMDElementsException(
78  'Metadata element has invalid sub-element.'
79  );
80  }
81  yield $sub_element;
82  }
83  }
84 
85  public function isMarked(): bool
86  {
87  return isset($this->marker);
88  }
89 
90  public function getMarker(): ?MarkerInterface
91  {
92  return $this->marker;
93  }
94 
95  public function mark(
96  MarkerFactoryInterface $factory,
97  Action $action,
98  string $data_value = ''
99  ): void {
100  $this->setMarker($factory->marker($action, $data_value));
101  $curr_element = $this->getSuperElement();
102  while ($curr_element) {
103  if ($curr_element->isMarked()) {
104  return;
105  }
106  $trail_action = Action::NEUTRAL;
107  if ($curr_element->isScaffold() && $action === Action::CREATE_OR_UPDATE) {
108  $trail_action = Action::CREATE_OR_UPDATE;
109  }
110  $curr_element->setMarker($factory->marker($trail_action));
111  $curr_element = $curr_element->getSuperElement();
112  }
113  }
114 
115  public function unmark(): void
116  {
117  $this->setMarker(null);
118  foreach ($this->getSubElements() as $sub_element) {
119  $sub_element->unmark();
120  }
121  }
122 
123  protected function setMarker(?MarkerInterface $marker): void
124  {
125  $this->marker = $marker;
126  }
127 
128  public function addScaffoldsToSubElements(
129  ScaffoldProviderInterface $scaffold_provider
130  ): void {
131  foreach ($scaffold_provider->getScaffoldsForElement($this) as $scaffold) {
132  if ($scaffold->getSubElements()->current() !== null) {
133  throw new \ilMDElementsException('Can only add scaffolds with no sub-elements.');
134  }
135  $this->addSubElement($scaffold);
136  $this->orderSubElements(...$scaffold_provider->getPossibleSubElementNamesForElementInOrder($this));
137  }
138  }
139 
140  public function addScaffoldToSubElements(
141  ScaffoldProviderInterface $scaffold_provider,
142  string $name
143  ): ?ElementInterface {
144  foreach ($scaffold_provider->getScaffoldsForElement($this) as $scaffold) {
145  if (strtolower($scaffold->getDefinition()->name()) === strtolower($name)) {
146  if ($scaffold->getSubElements()->current() !== null) {
147  throw new \ilMDElementsException('Can only add scaffolds with no sub-elements.');
148  }
149  $this->addSubElement($scaffold);
150  $this->orderSubElements(...$scaffold_provider->getPossibleSubElementNamesForElementInOrder($this));
151  return $scaffold;
152  }
153  }
154  return null;
155  }
156 }
orderSubElements(string ... $names_in_order)
Definition: BaseElement.php:84
__construct(NoID|int $md_id, DefinitionInterface $definition, DataInterface $data, Element ... $sub_elements)
Definition: Element.php:39
addScaffoldsToSubElements(ScaffoldProviderInterface $scaffold_provider)
Scaffolds are used to mark where elements could potentially be created.
Definition: Element.php:128
mark(MarkerFactoryInterface $factory, Action $action, string $data_value='')
Definition: Element.php:95
addScaffoldToSubElements(ScaffoldProviderInterface $scaffold_provider, string $name)
If possible, adds a scaffold with the given name to this element&#39;s sub-elements, and returns it...
Definition: Element.php:140
getPossibleSubElementNamesForElementInOrder(ElementInterface $element)
Returns the names of all possible sub-elements for the given element in the order defined by the stru...
setMarker(?MarkerInterface $marker)
Definition: Element.php:123
unmark()
Removes markers from this element, and recursively from all sub-elements.
Definition: Element.php:115
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
marker(Action $action, string $data_value='')
getScaffoldsForElement(ElementInterface $element)
Returns all elements that could be added as sub-elements to the given element as scaffolds.
isMarked()
Elements can be marked to be created, updated or deleted.
Definition: Element.php:85
__construct(Container $dic, ilPlugin $plugin)
addSubElement(BaseElement $sub_element)
Definition: BaseElement.php:78