1<?php
2
3namespace Sabre\Xml\Element;
4
5use Sabre\Xml;
6
7/**
8 * 'Elements' is a simple list of elements, without values or attributes.
9 * For example, Elements will parse:
10 *
11 * <?xml version="1.0"?>
12 * <s:root xmlns:s="http://sabredav.org/ns">
13 *   <s:elem1 />
14 *   <s:elem2 />
15 *   <s:elem3 />
16 *   <s:elem4>content</s:elem4>
17 *   <s:elem5 attr="val" />
18 * </s:root>
19 *
20 * Into:
21 *
22 * [
23 *   "{http://sabredav.org/ns}elem1",
24 *   "{http://sabredav.org/ns}elem2",
25 *   "{http://sabredav.org/ns}elem3",
26 *   "{http://sabredav.org/ns}elem4",
27 *   "{http://sabredav.org/ns}elem5",
28 * ];
29 *
30 * @copyright Copyright (C) 2009-2015 fruux GmbH (https://fruux.com/).
31 * @author Evert Pot (http://evertpot.com/)
32 * @license http://sabre.io/license/ Modified BSD License
33 */
34class Elements implements Xml\Element {
35
36    /**
37     * Value to serialize
38     *
39     * @var array
40     */
41    protected $value;
42
43    /**
44     * Constructor
45     *
46     * @param array $value
47     */
48    function __construct(array $value = []) {
49
50        $this->value = $value;
51
52    }
53
54    /**
55     * The xmlSerialize metod is called during xml writing.
56     *
57     * Use the $writer argument to write its own xml serialization.
58     *
59     * An important note: do _not_ create a parent element. Any element
60     * implementing XmlSerializble should only ever write what's considered
61     * its 'inner xml'.
62     *
63     * The parent of the current element is responsible for writing a
64     * containing element.
65     *
66     * This allows serializers to be re-used for different element names.
67     *
68     * If you are opening new elements, you must also close them again.
69     *
70     * @param Writer $writer
71     * @return void
72     */
73    function xmlSerialize(Xml\Writer $writer) {
74
75        foreach ($this->value as $val) {
76            $writer->writeElement($val);
77        }
78
79    }
80
81    /**
82     * The deserialize method is called during xml parsing.
83     *
84     * This method is called statictly, 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->parseSubTree() 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        // If there's no children, we don't do anything.
105        if ($reader->isEmptyElement) {
106            $reader->next();
107            return [];
108        }
109        $reader->read();
110        $currentDepth = $reader->depth;
111
112        $values = [];
113        do {
114
115            if ($reader->nodeType === Xml\Reader::ELEMENT) {
116                $values[] = $reader->getClark();
117            }
118
119        } while ($reader->depth >= $currentDepth && $reader->next());
120
121        $reader->next();
122        return $values;
123
124    }
125
126}
127