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