xref: /plugin/davcal/vendor/sabre/dav/lib/DAV/Xml/Property/Complex.php (revision a1a3b6794e0e143a4a8b51d3185ce2d339be61ab)
1*a1a3b679SAndreas Boehler<?php
2*a1a3b679SAndreas Boehler
3*a1a3b679SAndreas Boehlernamespace Sabre\DAV\Xml\Property;
4*a1a3b679SAndreas Boehler
5*a1a3b679SAndreas Boehleruse Sabre\Xml\Element\XmlFragment;
6*a1a3b679SAndreas Boehleruse Sabre\Xml\Reader;
7*a1a3b679SAndreas Boehler
8*a1a3b679SAndreas Boehler/**
9*a1a3b679SAndreas Boehler * This class represents a 'complex' property that didn't have a default
10*a1a3b679SAndreas Boehler * decoder.
11*a1a3b679SAndreas Boehler *
12*a1a3b679SAndreas Boehler * It's basically a container for an xml snippet.
13*a1a3b679SAndreas Boehler *
14*a1a3b679SAndreas Boehler * @copyright Copyright (C) 2007-2015 fruux GmbH. (https://fruux.com/)
15*a1a3b679SAndreas Boehler * @author Evert Pot (http://evertpot.com/)
16*a1a3b679SAndreas Boehler * @license http://sabre.io/license/ Modified BSD License
17*a1a3b679SAndreas Boehler */
18*a1a3b679SAndreas Boehlerclass Complex extends XmlFragment {
19*a1a3b679SAndreas Boehler
20*a1a3b679SAndreas Boehler    /**
21*a1a3b679SAndreas Boehler     * The deserialize method is called during xml parsing.
22*a1a3b679SAndreas Boehler     *
23*a1a3b679SAndreas Boehler     * This method is called statictly, this is because in theory this method
24*a1a3b679SAndreas Boehler     * may be used as a type of constructor, or factory method.
25*a1a3b679SAndreas Boehler     *
26*a1a3b679SAndreas Boehler     * Often you want to return an instance of the current class, but you are
27*a1a3b679SAndreas Boehler     * free to return other data as well.
28*a1a3b679SAndreas Boehler     *
29*a1a3b679SAndreas Boehler     * You are responsible for advancing the reader to the next element. Not
30*a1a3b679SAndreas Boehler     * doing anything will result in a never-ending loop.
31*a1a3b679SAndreas Boehler     *
32*a1a3b679SAndreas Boehler     * If you just want to skip parsing for this element altogether, you can
33*a1a3b679SAndreas Boehler     * just call $reader->next();
34*a1a3b679SAndreas Boehler     *
35*a1a3b679SAndreas Boehler     * $reader->parseInnerTree() will parse the entire sub-tree, and advance to
36*a1a3b679SAndreas Boehler     * the next element.
37*a1a3b679SAndreas Boehler     *
38*a1a3b679SAndreas Boehler     * @param Reader $reader
39*a1a3b679SAndreas Boehler     * @return mixed
40*a1a3b679SAndreas Boehler     */
41*a1a3b679SAndreas Boehler    static function xmlDeserialize(Reader $reader) {
42*a1a3b679SAndreas Boehler
43*a1a3b679SAndreas Boehler        $xml = $reader->readInnerXml();
44*a1a3b679SAndreas Boehler
45*a1a3b679SAndreas Boehler        if ($reader->nodeType === Reader::ELEMENT && $reader->isEmptyElement) {
46*a1a3b679SAndreas Boehler            // Easy!
47*a1a3b679SAndreas Boehler            $reader->next();
48*a1a3b679SAndreas Boehler            return null;
49*a1a3b679SAndreas Boehler        }
50*a1a3b679SAndreas Boehler        // Now we have a copy of the inner xml, we need to traverse it to get
51*a1a3b679SAndreas Boehler        // all the strings. If there's no non-string data, we just return the
52*a1a3b679SAndreas Boehler        // string, otherwise we return an instance of this class.
53*a1a3b679SAndreas Boehler        $reader->read();
54*a1a3b679SAndreas Boehler
55*a1a3b679SAndreas Boehler        $nonText = false;
56*a1a3b679SAndreas Boehler        $text = '';
57*a1a3b679SAndreas Boehler
58*a1a3b679SAndreas Boehler        while (true) {
59*a1a3b679SAndreas Boehler
60*a1a3b679SAndreas Boehler            switch ($reader->nodeType) {
61*a1a3b679SAndreas Boehler                case Reader::ELEMENT :
62*a1a3b679SAndreas Boehler                    $nonText = true;
63*a1a3b679SAndreas Boehler                    $reader->next();
64*a1a3b679SAndreas Boehler                    continue 2;
65*a1a3b679SAndreas Boehler                case Reader::TEXT :
66*a1a3b679SAndreas Boehler                case Reader::CDATA :
67*a1a3b679SAndreas Boehler                    $text .= $reader->value;
68*a1a3b679SAndreas Boehler                    break;
69*a1a3b679SAndreas Boehler                case Reader::END_ELEMENT :
70*a1a3b679SAndreas Boehler                    break 2;
71*a1a3b679SAndreas Boehler            }
72*a1a3b679SAndreas Boehler            $reader->read();
73*a1a3b679SAndreas Boehler
74*a1a3b679SAndreas Boehler        }
75*a1a3b679SAndreas Boehler
76*a1a3b679SAndreas Boehler        // Make sure we advance the cursor one step further.
77*a1a3b679SAndreas Boehler        $reader->read();
78*a1a3b679SAndreas Boehler
79*a1a3b679SAndreas Boehler        if ($nonText) {
80*a1a3b679SAndreas Boehler            $new = new self($xml);
81*a1a3b679SAndreas Boehler            return $new;
82*a1a3b679SAndreas Boehler        } else {
83*a1a3b679SAndreas Boehler            return $text;
84*a1a3b679SAndreas Boehler        }
85*a1a3b679SAndreas Boehler
86*a1a3b679SAndreas Boehler    }
87*a1a3b679SAndreas Boehler
88*a1a3b679SAndreas Boehler
89*a1a3b679SAndreas Boehler}
90