ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
BaseElement.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
26 
27 abstract class BaseElement implements BaseElementInterface
28 {
32  private array $sub_elements = [];
33  private ?BaseElement $super_element = null;
35  private int|NoID $md_id;
36 
37  public function __construct(
38  int|NoID $md_id,
39  DefinitionInterface $definition,
40  BaseElement ...$sub_elements
41  ) {
42  foreach ($sub_elements as $sub_element) {
43  $this->addSubElement($sub_element);
44  }
45  $this->definition = $definition;
46  $this->md_id = $md_id;
47  }
48 
49  public function __clone()
50  {
51  if (!is_null($this->super_element)) {
52  $this->setSuperElement(null);
53  }
54  $map = function (BaseElement $arg) {
55  $arg = clone $arg;
56  $arg->setSuperElement($this);
57  return $arg;
58  };
59  $this->sub_elements = array_map(
60  $map,
61  $this->sub_elements
62  );
63  }
64 
65  public function getMDID(): int|NoID
66  {
67  return $this->md_id;
68  }
69 
73  public function getSubElements(): \Generator
74  {
76  }
77 
78  protected function addSubElement(
79  BaseElement $sub_element,
80  string $insert_before = ''
81  ): void {
82  $sub_element->setSuperElement($this);
83  if ($insert_before === '') {
84  $this->sub_elements[] = $sub_element;
85  return;
86  }
87 
88  $new_subs = [];
89  $added = false;
90  foreach ($this->getSubElements() as $sub) {
91  if (!$added && $sub->getDefinition()->name() === $insert_before) {
92  $new_subs[] = $sub_element;
93  $added = true;
94  }
95  $new_subs[] = $sub;
96  }
97  if (!$added) {
98  $new_subs[] = $sub_element;
99  }
100  $this->sub_elements = $new_subs;
101  }
102 
103  public function getSuperElement(): ?BaseElement
104  {
105  return $this->super_element;
106  }
107 
108  protected function setSuperElement(?BaseElement $super_element): void
109  {
110  if ($this->isRoot()) {
111  throw new \ilMDElementsException(
112  'Metadata root can not have a super element.'
113  );
114  }
115  $this->super_element = $super_element;
116  }
117 
118  public function isRoot(): bool
119  {
120  return $this->getMDID() === NoID::ROOT;
121  }
122 
124  {
125  return $this->definition;
126  }
127 }
addSubElement(BaseElement $sub_element, string $insert_before='')
Definition: BaseElement.php:78
__construct(int|NoID $md_id, DefinitionInterface $definition, BaseElement ... $sub_elements)
Definition: BaseElement.php:37
setSuperElement(?BaseElement $super_element)
getDefinition()
Defining properties of the metadata element.