1<?php
2
3namespace Sabre\CalDAV\Xml\Filter;
4
5use Sabre\Xml\Reader;
6use Sabre\Xml\XmlDeserializable;
7use Sabre\CalDAV\Plugin;
8
9/**
10 * PropFilter parser.
11 *
12 * This class parses the {urn:ietf:params:xml:ns:caldav}param-filter XML
13 * element, as defined in:
14 *
15 * https://tools.ietf.org/html/rfc4791#section-9.7.3
16 *
17 * The result will be spit out as an array.
18 *
19 * @copyright Copyright (C) 2007-2015 fruux GmbH (https://fruux.com/).
20 * @author Evert Pot (http://www.rooftopsolutions.nl/)
21 * @license http://sabre.io/license/ Modified BSD License
22 */
23class ParamFilter implements XmlDeserializable {
24
25    /**
26     * The deserialize method is called during xml parsing.
27     *
28     * This method is called statictly, this is because in theory this method
29     * may be used as a type of constructor, or factory method.
30     *
31     * Often you want to return an instance of the current class, but you are
32     * free to return other data as well.
33     *
34     * Important note 2: You are responsible for advancing the reader to the
35     * next element. Not doing anything will result in a never-ending loop.
36     *
37     * If you just want to skip parsing for this element altogether, you can
38     * just call $reader->next();
39     *
40     * $reader->parseInnerTree() will parse the entire sub-tree, and advance to
41     * the next element.
42     *
43     * @param Reader $reader
44     * @return mixed
45     */
46    static function xmlDeserialize(Reader $reader) {
47
48        $result = [
49            'name'           => null,
50            'is-not-defined' => false,
51            'text-match'     => null,
52        ];
53
54        $att = $reader->parseAttributes();
55        $result['name'] = $att['name'];
56
57        $elems = $reader->parseInnerTree();
58
59        if (is_array($elems)) foreach ($elems as $elem) {
60
61            switch ($elem['name']) {
62
63                case '{' . Plugin::NS_CALDAV . '}is-not-defined' :
64                    $result['is-not-defined'] = true;
65                    break;
66                case '{' . Plugin::NS_CALDAV . '}text-match' :
67                    $result['text-match'] = [
68                        'negate-condition' => isset($elem['attributes']['negate-condition']) && $elem['attributes']['negate-condition'] === 'yes',
69                        'collation'        => isset($elem['attributes']['collation']) ? $elem['attributes']['collation'] : 'i;ascii-casemap',
70                        'value'            => $elem['value'],
71                    ];
72                    break;
73
74            }
75
76        }
77
78        return $result;
79
80    }
81
82}
83