Completed
Pull Request — master (#7)
by Daniel
01:26
created

Operator::xmlDeserialize()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 33
rs 8.7697
c 0
b 0
f 0
cc 6
nc 5
nop 1
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
27
class Operator implements XmlDeserializable {
28
	/**
29
	 * @var string
30
	 *
31
	 * The type of operation, one of the Operator::OPERATION_* constants
32
	 */
33
	public $type;
34
	/**
35
	 * @var (Literal|string|Operation)[]
36
	 *
37
	 * The list of arguments for the operation
38
	 *
39
	 *  - string: property name for comparison
40
	 *  - Literal: literal value for comparison
41
	 *  - Operation: nested operation for and/or/not operations
42
	 *
43
	 * Which type and what number of argument an Operator takes depends on the operator type.
44
	 */
45
	public $arguments;
46
47
	/**
48
	 * Operator constructor.
49
	 *
50
	 * @param string $type
51
	 * @param array $arguments
52
	 */
53
	public function __construct(string $type = '', array $arguments = []) {
54
		$this->type = $type;
55
		$this->arguments = $arguments;
56
	}
57
58
	static function xmlDeserialize(Reader $reader): Operator {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
59
		$operator = new self();
60
61
		$operator->type = $reader->getClark();
62
		if ($reader->isEmptyElement) {
63
			$reader->next();
64
			return $operator;
65
		}
66
67
        if ($operator->type === QueryOperator::OPERATION_CONTAINS) {
68
            $operator->arguments[] = $reader->readString();
69
            $reader->next();
70
            return $operator;
71
        }
72
73
		$reader->read();
74
		do {
75
			if ($reader->nodeType === Reader::ELEMENT) {
76
				$argument = $reader->parseCurrentElement();
77
				if ($argument['name'] === '{DAV:}prop') {
78
					$operator->arguments[] = $argument['value'][0];
79
				} else {
80
					$operator->arguments[] = $argument['value'];
81
				}
82
			} else {
83
				$reader->read();
84
			}
85
		} while ($reader->nodeType !== Reader::END_ELEMENT);
86
87
		$reader->read();
88
89
		return $operator;
90
	}
91
}
92