1<?php 2 3namespace Sabre\Xml\Element; 4 5use Sabre\Xml; 6use Sabre\Xml\Deserializer; 7 8/** 9 * 'KeyValue' parses out all child elements from a single node, and outputs a 10 * key=>value struct. 11 * 12 * Attributes will be removed, and duplicate child elements are discarded. 13 * Complex values within the elements will be parsed by the 'standard' parser. 14 * 15 * For example, KeyValue will parse: 16 * 17 * <?xml version="1.0"?> 18 * <s:root xmlns:s="http://sabredav.org/ns"> 19 * <s:elem1>value1</s:elem1> 20 * <s:elem2>value2</s:elem2> 21 * <s:elem3 /> 22 * </s:root> 23 * 24 * Into: 25 * 26 * [ 27 * "{http://sabredav.org/ns}elem1" => "value1", 28 * "{http://sabredav.org/ns}elem2" => "value2", 29 * "{http://sabredav.org/ns}elem3" => null, 30 * ]; 31 * 32 * @copyright Copyright (C) 2009-2015 fruux GmbH (https://fruux.com/). 33 * @author Evert Pot (http://evertpot.com/) 34 * @license http://sabre.io/license/ Modified BSD License 35 */ 36class KeyValue implements Xml\Element { 37 38 /** 39 * Value to serialize 40 * 41 * @var array 42 */ 43 protected $value; 44 45 /** 46 * Constructor 47 * 48 * @param array $value 49 */ 50 function __construct(array $value = []) { 51 52 $this->value = $value; 53 54 } 55 56 /** 57 * The xmlSerialize metod is called during xml writing. 58 * 59 * Use the $writer argument to write its own xml serialization. 60 * 61 * An important note: do _not_ create a parent element. Any element 62 * implementing XmlSerializble should only ever write what's considered 63 * its 'inner xml'. 64 * 65 * The parent of the current element is responsible for writing a 66 * containing element. 67 * 68 * This allows serializers to be re-used for different element names. 69 * 70 * If you are opening new elements, you must also close them again. 71 * 72 * @param Writer $writer 73 * @return void 74 */ 75 function xmlSerialize(Xml\Writer $writer) { 76 77 $writer->write($this->value); 78 79 } 80 81 /** 82 * The deserialize method is called during xml parsing. 83 * 84 * This method is called staticly, this is because in theory this method 85 * may be used as a type of constructor, or factory method. 86 * 87 * Often you want to return an instance of the current class, but you are 88 * free to return other data as well. 89 * 90 * Important note 2: You are responsible for advancing the reader to the 91 * next element. Not doing anything will result in a never-ending loop. 92 * 93 * If you just want to skip parsing for this element altogether, you can 94 * just call $reader->next(); 95 * 96 * $reader->parseInnerTree() will parse the entire sub-tree, and advance to 97 * the next element. 98 * 99 * @param Xml\Reader $reader 100 * @return mixed 101 */ 102 static function xmlDeserialize(Xml\Reader $reader) { 103 104 return Deserializer\keyValue($reader); 105 106 } 107 108} 109