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