1<?php 2 3namespace Sabre\CardDAV\Xml\Filter; 4 5use Sabre\CardDAV\Plugin; 6use Sabre\DAV\Exception\BadRequest; 7use Sabre\Xml\Reader; 8use Sabre\Xml\XmlDeserializable; 9 10/** 11 * PropFilter parser. 12 * 13 * This class parses the {urn:ietf:params:xml:ns:carddav}prop-filter XML 14 * element, as defined in: 15 * 16 * http://tools.ietf.org/html/rfc6352#section-10.5.1 17 * 18 * The result will be spit out as an array. 19 * 20 * @copyright Copyright (C) fruux GmbH (https://fruux.com/) 21 * @author Evert Pot (http://evertpot.com/) 22 * @license http://sabre.io/license/ Modified BSD License 23 */ 24class PropFilter implements XmlDeserializable { 25 26 /** 27 * The deserialize method is called during xml parsing. 28 * 29 * This method is called statically, 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 'test' => 'anyof', 52 'is-not-defined' => false, 53 'param-filters' => [], 54 'text-matches' => [], 55 ]; 56 57 $att = $reader->parseAttributes(); 58 $result['name'] = $att['name']; 59 60 if (isset($att['test']) && $att['test'] === 'allof') { 61 $result['test'] = 'allof'; 62 } 63 64 $elems = $reader->parseInnerTree(); 65 66 if (is_array($elems)) foreach ($elems as $elem) { 67 68 switch ($elem['name']) { 69 70 case '{' . Plugin::NS_CARDDAV . '}param-filter' : 71 $result['param-filters'][] = $elem['value']; 72 break; 73 case '{' . Plugin::NS_CARDDAV . '}is-not-defined' : 74 $result['is-not-defined'] = true; 75 break; 76 case '{' . Plugin::NS_CARDDAV . '}text-match' : 77 $matchType = isset($elem['attributes']['match-type']) ? $elem['attributes']['match-type'] : 'contains'; 78 79 if (!in_array($matchType, ['contains', 'equals', 'starts-with', 'ends-with'])) { 80 throw new BadRequest('Unknown match-type: ' . $matchType); 81 } 82 $result['text-matches'][] = [ 83 'negate-condition' => isset($elem['attributes']['negate-condition']) && $elem['attributes']['negate-condition'] === 'yes', 84 'collation' => isset($elem['attributes']['collation']) ? $elem['attributes']['collation'] : 'i;unicode-casemap', 85 'value' => $elem['value'], 86 'match-type' => $matchType, 87 ]; 88 break; 89 90 } 91 92 } 93 94 return $result; 95 96 } 97 98} 99