ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
Standard.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
34
35class Standard implements WriterInterface
36{
39 protected PathFactory $path_factory;
41
42 public function __construct(
45 PathFactory $path_factory,
47 ) {
48 $this->dictionary = $dictionary;
49 $this->copyright_handler = $copyright_handler;
50 $this->path_factory = $path_factory;
51 $this->manipulator = $manipulator;
52 }
53
54 public function write(SetInterface $set): \SimpleXMLElement
55 {
56 $set = $this->prepareCopyright($set);
57 $root = $set->getRoot();
58 $root_name = $root->getDefinition()->name();
59 $xml = new \SimpleXMLElement('<' . $root_name . '></' . $root_name . '>');
60
62 $root,
63 $this->getTagForElement($root),
64 $xml
65 );
66
67 return $xml;
68 }
69
74 protected function prepareCopyright(SetInterface $set): SetInterface
75 {
76 if (!$this->copyright_handler->isCopyrightSelectionActive()) {
77 return $set;
78 }
79
80 $path_to_copyright = $this->path_factory->custom()
81 ->withNextStep('rights')
82 ->withNextStep('description')
83 ->withNextStep('string')
84 ->get();
85 // markers are ignored here, so we can safely use the manipulator
86 return $this->manipulator->prepareCreateOrUpdate($set, $path_to_copyright, '');
87 }
88
89 protected function addSubElementsToXML(
90 ElementInterface $element,
91 ?TagInterface $tag,
92 \SimpleXMLElement $xml,
93 int $depth = 0
94 ): void {
95 if ($depth > 30) {
96 throw new \ilMDXMLException('LOM set is nested too deep.');
97 }
98
99 if ($tag?->isExportedAsLangString()) {
100 $this->addLangStringToXML($element, $xml);
101 return;
102 }
103
104 foreach ($element->getSubElements() as $sub_element) {
105 $sub_tag = $this->getTagForElement($sub_element);
106 $sub_name = $sub_element->getDefinition()->name();
107 $sub_value = $this->getDataValue($sub_element->getData(), $sub_tag);
108
109 if ($sub_tag?->isOmitted()) {
110 continue;
111 }
112
113 if ($sub_tag?->isExportedAsAttribute()) {
114 $xml->addAttribute($sub_name, (string) $sub_value);
115 continue;
116 }
117
118 $child_xml = $xml->addChild($sub_name);
119 $child_xml[0] = $sub_value;
120 $this->addSubElementsToXML($sub_element, $sub_tag, $child_xml, $depth + 1);
121 }
122 }
123
124 protected function addLangStringToXML(
125 ElementInterface $element,
126 \SimpleXMLElement $xml
127 ): void {
128 $string_element = null;
129 $language_element = null;
130 foreach ($element->getSubElements() as $sub_element) {
131 if ($sub_element->getDefinition()->name() === 'string') {
132 $string_element = $sub_element;
133 } elseif ($sub_element->getDefinition()->name() === 'language') {
134 $language_element = $sub_element;
135 }
136 }
137
138 $string_value = '';
139 if (!is_null($string_element)) {
140 $string_value = $this->getDataValue(
141 $string_element->getData(),
142 $this->getTagForElement($string_element)
143 );
144 }
145 $string_xml = $xml->addChild('string');
146 $xml->string = $string_value;
147
148 if (is_null($language_element)) {
149 return;
150 }
151 $language_value = $this->getDataValue(
152 $language_element->getData(),
153 $this->getTagForElement($language_element)
154 );
155 $string_xml->addAttribute(
156 'language',
157 $language_value
158 );
159 }
160
161 protected function getDataValue(
163 ?TagInterface $tag
164 ): ?string {
165 if ($tag?->isTranslatedAsCopyright()) {
166 return $this->copyright_handler->copyrightForExport($data->value());
167 }
168
169 switch ($data->type()) {
170 case Type::NULL:
171 return null;
172
173 case Type::LANG:
174 $value = $data->value();
175 if ($value === 'xx') {
176 return 'none';
177 }
178 return $value;
179
180 case Type::STRING:
181 case Type::VOCAB_SOURCE:
182 case Type::VOCAB_VALUE:
183 case Type::DATETIME:
184 case Type::NON_NEG_INT:
185 case Type::DURATION:
186 default:
187 return $data->value();
188 }
189 }
190
191 protected function getTagForElement(ElementInterface $element): ?TagInterface
192 {
193 return $this->dictionary->tagForElement($element, $this->currentVersion());
194 }
195
196 protected function currentVersion(): Version
197 {
198 return Version::V10_0;
199 }
200}
CopyrightHandlerInterface $copyright_handler
Definition: Standard.php:38
addLangStringToXML(ElementInterface $element, \SimpleXMLElement $xml)
Definition: Standard.php:124
getDataValue(DataInterface $data, ?TagInterface $tag)
Definition: Standard.php:161
addSubElementsToXML(ElementInterface $element, ?TagInterface $tag, \SimpleXMLElement $xml, int $depth=0)
Definition: Standard.php:89
getTagForElement(ElementInterface $element)
Definition: Standard.php:191
__construct(DictionaryInterface $dictionary, CopyrightHandlerInterface $copyright_handler, PathFactory $path_factory, ManipulatorInterface $manipulator)
Definition: Standard.php:42
prepareCopyright(SetInterface $set)
When copyright selection is active, objects should always be exported with copyright,...
Definition: Standard.php:74
write(SetInterface $set)
Currently, this does not write information from markers to xml, so calling this with a set from a sta...
Definition: Standard.php:54
getRoot()
Returns the root element of the metadata set.
if(!file_exists('../ilias.ini.php'))