ILIAS  trunk Revision v11.0_alpha-1761-g6dbbfa7b760
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
StructurallyCoupled.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
35 
37 {
42 
43  public function __construct(
44  MarkerFactoryInterface $marker_factory,
45  ScaffoldProviderInterface $scaffold_provider,
46  DictionaryInterface $dictionary,
47  CopyrightHandlerInterface $copyright_handler
48  ) {
49  $this->marker_factory = $marker_factory;
50  $this->scaffold_provider = $scaffold_provider;
51  $this->dictionary = $dictionary;
52  $this->copyright_handler = $copyright_handler;
53  }
54 
59  public function read(
60  \SimpleXMLElement $xml,
62  ): SetInterface {
63  $set = $this->scaffold_provider->set();
64  $root_element = $set->getRoot();
65 
66  if ($xml->getName() !== $root_element->getDefinition()->name()) {
67  throw new \ilMDXMLException(
68  $xml->getName() . ' is not the correct root element, should be ' .
69  $root_element->getDefinition()->name()
70  );
71  }
72 
73  $this->prepareAddingSubElementsFromXML(
74  $version,
75  $root_element,
76  $this->dictionary->tagForElement($root_element, $version),
77  $xml
78  );
79 
80  return $set;
81  }
82 
83  protected function prepareAddingSubElementsFromXML(
85  ElementInterface $element,
86  ?TagInterface $tag,
87  \SimpleXMLElement $xml,
88  int $depth = 0
89  ): void {
90  if ($depth > 30) {
91  throw new \ilMDXMLException('LOM XML is nested too deep.');
92  }
93 
94  if ($tag?->isExportedAsLangString()) {
95  $this->prepareAddingLangStringFromXML($version, $element, $xml);
96  return;
97  }
98 
99  $children_and_attributes = new \AppendIterator();
100  if (!empty($children = $xml->children())) {
101  $children_and_attributes->append($children);
102  }
103  if (!empty($attributes = $xml->attributes())) {
104  $children_and_attributes->append($attributes);
105  }
107  foreach ($children_and_attributes as $child_or_attrib_xml) {
108  $sub_scaffold = $element->addScaffoldToSubElements(
109  $this->scaffold_provider,
110  $child_or_attrib_xml->getName(),
111  );
112  if (is_null($sub_scaffold)) {
113  continue;
114  }
115 
116  $sub_tag = $this->dictionary->tagForElement($sub_scaffold, $version);
117  if ($sub_tag?->isOmitted()) {
118  continue;
119  }
120 
121  $sub_value = $this->parseElementValue(
122  $sub_scaffold->getDefinition(),
123  $sub_tag,
124  (string) $child_or_attrib_xml
125  );
126  $sub_scaffold->mark($this->marker_factory, Action::CREATE_OR_UPDATE, $sub_value);
127 
128  $this->prepareAddingSubElementsFromXML(
129  $version,
130  $sub_scaffold,
131  $sub_tag,
132  $child_or_attrib_xml,
133  $depth + 1
134  );
135  }
136  }
137 
138  protected function prepareAddingLangStringFromXML(
139  Version $version,
140  ElementInterface $element,
141  \SimpleXMLElement $xml,
142  ): void {
143  $string_xml = $xml->string;
144  $language_xml = $string_xml->attributes()->language;
145 
146  if (!empty($string_xml) && ((string) $string_xml) !== '') {
147  $string_element = $element->addScaffoldToSubElements(
148  $this->scaffold_provider,
149  'string'
150  );
151  $string_element->mark(
152  $this->marker_factory,
153  Action::CREATE_OR_UPDATE,
154  $this->parseElementValue(
155  $string_element->getDefinition(),
156  $this->dictionary->tagForElement($string_element, $version),
157  (string) $string_xml
158  )
159  );
160  }
161 
162  if (!empty($language_xml)) {
163  $language_element = $element->addScaffoldToSubElements(
164  $this->scaffold_provider,
165  'language'
166  );
167  $language_element->mark(
168  $this->marker_factory,
169  Action::CREATE_OR_UPDATE,
170  $this->parseElementValue(
171  $language_element->getDefinition(),
172  $this->dictionary->tagForElement($language_element, $version),
173  (string) $language_xml
174  )
175  );
176  }
177  }
178 
179  protected function parseElementValue(
180  DefinitionInterface $definition,
181  ?TagInterface $tag,
182  string $value
183  ): string {
184  $value = strip_tags($value);
185 
186  if ($tag?->isTranslatedAsCopyright()) {
187  return $this->copyright_handler->copyrightFromExport($value);
188  }
189 
190  switch ($definition->dataType()) {
191  case Type::NULL:
192  return '';
193 
194  case Type::LANG:
195  if ($value === 'none') {
196  return 'xx';
197  }
198  return $value;
199 
200  case Type::STRING:
201  case Type::VOCAB_SOURCE:
202  case Type::VOCAB_VALUE:
203  case Type::DATETIME:
204  case Type::NON_NEG_INT:
205  case Type::DURATION:
206  default:
207  return $value;
208  }
209  }
210 }
prepareAddingLangStringFromXML(Version $version, ElementInterface $element, \SimpleXMLElement $xml,)
$version
Definition: plugin.php:24
getRoot()
Returns the root element of the metadata set.
parseElementValue(DefinitionInterface $definition, ?TagInterface $tag, string $value)
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...
__construct(MarkerFactoryInterface $marker_factory, ScaffoldProviderInterface $scaffold_provider, DictionaryInterface $dictionary, CopyrightHandlerInterface $copyright_handler)
read(\SimpleXMLElement $xml, Version $version)
Assumes that the structure of the xml is identical to the structure of LOM in ILIAS, with exceptions defined in the dictionary.
dataType()
Type of data this element can carry.