ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
Service.php
Go to the documentation of this file.
1<?php
2
3namespace Sabre\Xml;
4
16class Service {
17
29 public $elementMap = [];
30
39 public $namespaceMap = [];
40
59 public $classMap = [];
60
66 function getReader() {
67
68 $r = new Reader();
69 $r->elementMap = $this->elementMap;
70 return $r;
71
72 }
73
79 function getWriter() {
80
81 $w = new Writer();
82 $w->namespaceMap = $this->namespaceMap;
83 $w->classMap = $this->classMap;
84 return $w;
85
86 }
87
107 function parse($input, $contextUri = null, &$rootElementName = null) {
108
109 if (is_resource($input)) {
110 // Unfortunately the XMLReader doesn't support streams. When it
111 // does, we can optimize this.
112 $input = stream_get_contents($input);
113 }
114 $r = $this->getReader();
115 $r->contextUri = $contextUri;
116 $r->xml($input);
117
118 $result = $r->parse();
119 $rootElementName = $result['name'];
120 return $result['value'];
121
122 }
123
143 function expect($rootElementName, $input, $contextUri = null) {
144
145 if (is_resource($input)) {
146 // Unfortunately the XMLReader doesn't support streams. When it
147 // does, we can optimize this.
148 $input = stream_get_contents($input);
149 }
150 $r = $this->getReader();
151 $r->contextUri = $contextUri;
152 $r->xml($input);
153
154 $rootElementName = (array)$rootElementName;
155
156 foreach ($rootElementName as &$rEl) {
157 if ($rEl[0] !== '{') $rEl = '{}' . $rEl;
158 }
159
160 $result = $r->parse();
161 if (!in_array($result['name'], $rootElementName, true)) {
162 throw new ParseException('Expected ' . implode(' or ', (array)$rootElementName) . ' but received ' . $result['name'] . ' as the root element');
163 }
164 return $result['value'];
165
166 }
167
186 function write($rootElementName, $value, $contextUri = null) {
187
188 $w = $this->getWriter();
189 $w->openMemory();
190 $w->contextUri = $contextUri;
191 $w->setIndent(true);
192 $w->startDocument();
193 $w->writeElement($rootElementName, $value);
194 return $w->outputMemory();
195
196 }
197
226 function mapValueObject($elementName, $className) {
227 list($namespace) = self::parseClarkNotation($elementName);
228
229 $this->elementMap[$elementName] = function(Reader $reader) use ($className, $namespace) {
231 };
232 $this->classMap[$className] = function(Writer $writer, $valueObject) use ($namespace) {
234 };
235 $this->valueObjectMap[$className] = $elementName;
236 }
237
251 function writeValueObject($object, $contextUri = null) {
252
253 if (!isset($this->valueObjectMap[get_class($object)])) {
254 throw new \InvalidArgumentException('"' . get_class($object) . '" is not a registered value object class. Register your class with mapValueObject.');
255 }
256 return $this->write(
257 $this->valueObjectMap[get_class($object)],
258 $object,
260 );
261
262 }
263
274 static function parseClarkNotation($str) {
275 static $cache = [];
276
277 if (!isset($cache[$str])) {
278
279 if (!preg_match('/^{([^}]*)}(.*)$/', $str, $matches)) {
280 throw new \InvalidArgumentException('\'' . $str . '\' is not a valid clark-notation formatted string');
281 }
282
283 $cache[$str] = [
284 $matches[1],
285 $matches[2]
286 ];
287 }
288
289 return $cache[$str];
290 }
291
295 protected $valueObjectMap = [];
296
297}
$result
An exception for terminatinating execution or to throw for unit testing.
This is a base exception for any exception related to parsing xml files.
The Reader class expands upon PHP's built-in XMLReader.
Definition: Reader.php:20
XML parsing and writing service.
Definition: Service.php:16
expect($rootElementName, $input, $contextUri=null)
Parses a document in full, and specify what the expected root element name is.
Definition: Service.php:143
writeValueObject($object, $contextUri=null)
Writes a value object.
Definition: Service.php:251
write($rootElementName, $value, $contextUri=null)
Generates an XML document in one go.
Definition: Service.php:186
mapValueObject($elementName, $className)
Map an xml element to a PHP class.
Definition: Service.php:226
static parseClarkNotation($str)
Parses a clark-notation string, and returns the namespace and element name components.
Definition: Service.php:274
parse($input, $contextUri=null, &$rootElementName=null)
Parses a document in full.
Definition: Service.php:107
getWriter()
Returns a fresh xml writer.
Definition: Service.php:79
getReader()
Returns a fresh XML Reader.
Definition: Service.php:66
The XML Writer class.
Definition: Writer.php:31
if($err=$client->getError()) $namespace
$w
$r
Definition: example_031.php:79
valueObject(Reader $reader, $className, $namespace)
The valueObject deserializer turns an xml element into a PHP object of a specific class.
Definition: functions.php:181