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