ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
Sabre\Xml\Service Class Reference

XML parsing and writing service. More...

+ Inheritance diagram for Sabre\Xml\Service:
+ Collaboration diagram for Sabre\Xml\Service:

Public Member Functions

 getReader ()
 Returns a fresh XML Reader. More...
 
 getWriter ()
 Returns a fresh xml writer. More...
 
 parse ($input, $contextUri=null, &$rootElementName=null)
 Parses a document in full. More...
 
 expect ($rootElementName, $input, $contextUri=null)
 Parses a document in full, and specify what the expected root element name is. More...
 
 write ($rootElementName, $value, $contextUri=null)
 Generates an XML document in one go. More...
 
 mapValueObject ($elementName, $className)
 Map an xml element to a PHP class. More...
 
 writeValueObject ($object, $contextUri=null)
 Writes a value object. More...
 

Static Public Member Functions

static parseClarkNotation ($str)
 Parses a clark-notation string, and returns the namespace and element name components. More...
 

Data Fields

 $elementMap = []
 
 $namespaceMap = []
 
 $classMap = []
 

Protected Attributes

 $valueObjectMap = []
 A list of classes and which XML elements they map to. More...
 

Detailed Description

XML parsing and writing service.

You are encouraged to make a instance of this for your application and potentially extend it, as a central API point for dealing with xml and configuring the reader and writer.

Author
Evert Pot (http://evertpot.com/) @license http://sabre.io/license/ Modified BSD License

Definition at line 16 of file Service.php.

Member Function Documentation

◆ expect()

Sabre\Xml\Service::expect (   $rootElementName,
  $input,
  $contextUri = null 
)

Parses a document in full, and specify what the expected root element name is.

This function works similar to parse, but the difference is that the user can specify what the expected name of the root element should be, in clark notation.

This is useful in cases where you expected a specific document to be passed, and reduces the amount of if statements.

It's also possible to pass an array of expected rootElements if your code may expect more than one document type.

Parameters
string | string[]$rootElementName
string | resource$input
string | null$contextUri
Returns
void

Definition at line 143 of file Service.php.

143 {
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 }
$result
getReader()
Returns a fresh XML Reader.
Definition: Service.php:66
$r
Definition: example_031.php:79

References Sabre\Xml\$contextUri, $input, $r, $result, and Sabre\Xml\Service\getReader().

+ Here is the call graph for this function:

◆ getReader()

Sabre\Xml\Service::getReader ( )

Returns a fresh XML Reader.

Returns
Reader

Definition at line 66 of file Service.php.

66 {
67
68 $r = new Reader();
69 $r->elementMap = $this->elementMap;
70 return $r;
71
72 }

References Sabre\Xml\Service\$elementMap, and $r.

Referenced by Sabre\Xml\Service\expect(), and Sabre\Xml\Service\parse().

+ Here is the caller graph for this function:

◆ getWriter()

Sabre\Xml\Service::getWriter ( )

Returns a fresh xml writer.

Returns
Writer

Definition at line 79 of file Service.php.

79 {
80
81 $w = new Writer();
82 $w->namespaceMap = $this->namespaceMap;
83 $w->classMap = $this->classMap;
84 return $w;
85
86 }
$w

References Sabre\Xml\Service\$classMap, Sabre\Xml\Service\$namespaceMap, and $w.

Referenced by Sabre\Xml\Service\write().

+ Here is the caller graph for this function:

◆ mapValueObject()

Sabre\Xml\Service::mapValueObject (   $elementName,
  $className 
)

Map an xml element to a PHP class.

Calling this function will automatically setup the Reader and Writer classes to turn a specific XML element to a PHP class.

For example, given a class such as :

class Author { public $firstName; public $lastName; }

and an XML element such as:

<author xmlns="http://example.org/ns"> <firstName>...</firstName> <lastName>...</lastName> </author>

These can easily be mapped by calling:

$service->mapValueObject('{http://example.org}author', 'Author');

Parameters
string$elementName
object$className
Returns
void

Definition at line 226 of file Service.php.

226 {
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 }
static parseClarkNotation($str)
Parses a clark-notation string, and returns the namespace and element name components.
Definition: Service.php:274
if($err=$client->getError()) $namespace
valueObject(Reader $reader, $className, $namespace)
The valueObject deserializer turns an xml element into a PHP object of a specific class.
Definition: functions.php:181

References $namespace, $reader, Sabre\Xml\Service\parseClarkNotation(), and Sabre\Xml\Deserializer\valueObject().

+ Here is the call graph for this function:

◆ parse()

Sabre\Xml\Service::parse (   $input,
  $contextUri = null,
$rootElementName = null 
)

Parses a document in full.

Input may be specified as a string or readable stream resource. The returned value is the value of the root document.

Specifying the $contextUri allows the parser to figure out what the URI of the document was. This allows relative URIs within the document to be expanded easily.

The $rootElementName is specified by reference and will be populated with the root element name of the document.

Parameters
string | resource$input
string | null$contextUri
string | null$rootElementName
Exceptions
ParseException
Returns
array|object|string

Definition at line 107 of file Service.php.

107 {
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 }

References Sabre\Xml\$contextUri, $input, $r, $result, and Sabre\Xml\Service\getReader().

+ Here is the call graph for this function:

◆ parseClarkNotation()

static Sabre\Xml\Service::parseClarkNotation (   $str)
static

Parses a clark-notation string, and returns the namespace and element name components.

If the string was invalid, it will throw an InvalidArgumentException.

Parameters
string$str
Exceptions
InvalidArgumentException
Returns
array

Definition at line 274 of file Service.php.

274 {
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 }

Referenced by Sabre\VObject\Parser\XML\getTagName(), Sabre\Xml\Service\mapValueObject(), Sabre\VObject\Parser\XML\parseProperties(), Sabre\DAV\Client\propFind(), Sabre\Xml\Writer\startElement(), Sabre\Xml\ServiceTest\testParseClarkNotation(), Sabre\Xml\ServiceTest\testParseClarkNotationFail(), and Sabre\Xml\Writer\writeAttribute().

+ Here is the caller graph for this function:

◆ write()

Sabre\Xml\Service::write (   $rootElementName,
  $value,
  $contextUri = null 
)

Generates an XML document in one go.

The $rootElement must be specified in clark notation. The value must be a string, an array or an object implementing XmlSerializable. Basically, anything that's supported by the Writer object.

$contextUri can be used to specify a sort of 'root' of the PHP application, in case the xml document is used as a http response.

This allows an implementor to easily create URI's relative to the root of the domain.

Parameters
string$rootElementName
string | array | XmlSerializable$value
string | null$contextUri

Definition at line 186 of file Service.php.

186 {
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 }
getWriter()
Returns a fresh xml writer.
Definition: Service.php:79

References Sabre\Xml\$contextUri, $w, and Sabre\Xml\Service\getWriter().

Referenced by Sabre\Xml\Service\writeValueObject().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ writeValueObject()

Sabre\Xml\Service::writeValueObject (   $object,
  $contextUri = null 
)

Writes a value object.

This function largely behaves similar to write(), except that it's intended specifically to serialize a Value Object into an XML document.

The ValueObject must have been previously registered using mapValueObject().

Parameters
object$object
string$contextUri
Returns
void

Definition at line 251 of file Service.php.

251 {
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 }
write($rootElementName, $value, $contextUri=null)
Generates an XML document in one go.
Definition: Service.php:186

References Sabre\Xml\$contextUri, and Sabre\Xml\Service\write().

+ Here is the call graph for this function:

Field Documentation

◆ $classMap

Sabre\Xml\Service::$classMap = []

Definition at line 59 of file Service.php.

Referenced by Sabre\Xml\Service\getWriter().

◆ $elementMap

Sabre\Xml\Service::$elementMap = []

Definition at line 29 of file Service.php.

Referenced by Sabre\Xml\Service\getReader().

◆ $namespaceMap

Sabre\Xml\Service::$namespaceMap = []

Definition at line 39 of file Service.php.

Referenced by Sabre\Xml\Service\getWriter().

◆ $valueObjectMap

Sabre\Xml\Service::$valueObjectMap = []
protected

A list of classes and which XML elements they map to.

Definition at line 295 of file Service.php.


The documentation for this class was generated from the following file: