icewind1991 /
SearchDAV
| 1 | <?php declare(strict_types=1); |
||
| 2 | /** |
||
| 3 | * @copyright Copyright (c) 2017 Robin Appelman <[email protected]> |
||
| 4 | * |
||
| 5 | * @license GNU AGPL version 3 or any later version |
||
| 6 | * |
||
| 7 | * This program is free software: you can redistribute it and/or modify |
||
| 8 | * it under the terms of the GNU Affero General Public License as |
||
| 9 | * published by the Free Software Foundation, either version 3 of the |
||
| 10 | * License, or (at your option) any later version. |
||
| 11 | * |
||
| 12 | * This program is distributed in the hope that it will be useful, |
||
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
| 15 | * GNU Affero General Public License for more details. |
||
| 16 | * |
||
| 17 | * You should have received a copy of the GNU Affero General Public License |
||
| 18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
||
| 19 | * |
||
| 20 | */ |
||
| 21 | |||
| 22 | namespace SearchDAV\XML; |
||
| 23 | |||
| 24 | use Sabre\Xml\Reader; |
||
| 25 | use Sabre\Xml\XmlDeserializable; |
||
| 26 | use SearchDAV\Query\Operator as QueryOperator; |
||
| 27 | |||
| 28 | class Operator implements XmlDeserializable { |
||
| 29 | /** |
||
| 30 | * @var string |
||
| 31 | * |
||
| 32 | * The type of operation, one of the Operator::OPERATION_* constants |
||
| 33 | */ |
||
| 34 | public $type; |
||
| 35 | /** |
||
| 36 | * @var (Literal|string|Operation)[] |
||
| 37 | * |
||
| 38 | * The list of arguments for the operation |
||
| 39 | * |
||
| 40 | * - string: property name for comparison |
||
| 41 | * - Literal: literal value for comparison |
||
| 42 | * - Operation: nested operation for and/or/not operations |
||
| 43 | * |
||
| 44 | * Which type and what number of argument an Operator takes depends on the operator type. |
||
| 45 | */ |
||
| 46 | public $arguments; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Operator constructor. |
||
| 50 | * |
||
| 51 | * @param string $type |
||
| 52 | * @param array $arguments |
||
| 53 | */ |
||
| 54 | public function __construct(string $type = '', array $arguments = []) { |
||
| 55 | $this->type = $type; |
||
| 56 | $this->arguments = $arguments; |
||
| 57 | } |
||
| 58 | |||
| 59 | static function xmlDeserialize(Reader $reader): Operator { |
||
|
0 ignored issues
–
show
|
|||
| 60 | $operator = new self(); |
||
| 61 | |||
| 62 | $operator->type = $reader->getClark(); |
||
| 63 | if ($reader->isEmptyElement) { |
||
| 64 | $reader->next(); |
||
| 65 | return $operator; |
||
| 66 | } |
||
| 67 | |||
| 68 | if ($operator->type === QueryOperator::OPERATION_CONTAINS) { |
||
| 69 | $operator->arguments[] = $reader->readString(); |
||
| 70 | $reader->next(); |
||
| 71 | return $operator; |
||
| 72 | } |
||
| 73 | |||
| 74 | $reader->read(); |
||
| 75 | do { |
||
| 76 | if ($reader->nodeType === Reader::ELEMENT) { |
||
| 77 | $argument = $reader->parseCurrentElement(); |
||
| 78 | if ($argument['name'] === '{DAV:}prop') { |
||
| 79 | $operator->arguments[] = $argument['value'][0] ?? ''; |
||
| 80 | } else { |
||
| 81 | $operator->arguments[] = $argument['value']; |
||
| 82 | } |
||
| 83 | } else { |
||
| 84 | if (!$reader->read()) { |
||
| 85 | break; |
||
| 86 | } |
||
| 87 | } |
||
| 88 | } while ($reader->nodeType !== Reader::END_ELEMENT); |
||
| 89 | |||
| 90 | $reader->read(); |
||
| 91 | |||
| 92 | return $operator; |
||
| 93 | } |
||
| 94 | } |
||
| 95 |
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.