|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* File containing the Field Criterion parser class. |
|
5
|
|
|
* |
|
6
|
|
|
* @copyright Copyright (C) eZ Systems AS. All rights reserved. |
|
7
|
|
|
* @license For full copyright and license information view LICENSE file distributed with this source code. |
|
8
|
|
|
*/ |
|
9
|
|
|
namespace eZ\Publish\Core\REST\Server\Input\Parser\Criterion; |
|
10
|
|
|
|
|
11
|
|
|
use eZ\Publish\API\Repository\Values\Content\Query\Criterion\Field as FieldCriterion; |
|
12
|
|
|
use eZ\Publish\API\Repository\Values\Content\Query\Criterion\Operator; |
|
|
|
|
|
|
13
|
|
|
use eZ\Publish\Core\REST\Common\Input\BaseParser; |
|
14
|
|
|
use eZ\Publish\Core\REST\Common\Input\ParsingDispatcher; |
|
15
|
|
|
use eZ\Publish\Core\REST\Common\Exceptions; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Parser for Field Criterion. |
|
19
|
|
|
*/ |
|
20
|
|
|
class Field extends BaseParser |
|
21
|
|
|
{ |
|
22
|
|
|
const OPERATORS = [ |
|
23
|
|
|
'IN' => Operator::IN, |
|
24
|
|
|
'EQ' => Operator::EQ, |
|
25
|
|
|
'GT' => Operator::GT, |
|
26
|
|
|
'GTE' => Operator::GTE, |
|
27
|
|
|
'LT' => Operator::LT, |
|
28
|
|
|
'LTE' => Operator::LTE, |
|
29
|
|
|
'LIKE' => Operator::LIKE, |
|
30
|
|
|
'BETWEEN' => Operator::BETWEEN, |
|
31
|
|
|
'CONTAINS' => Operator::CONTAINS, |
|
32
|
|
|
]; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Parses input structure to a Criterion object. |
|
36
|
|
|
* |
|
37
|
|
|
* @param array $data |
|
38
|
|
|
* @param \eZ\Publish\Core\REST\Common\Input\ParsingDispatcher $parsingDispatcher |
|
39
|
|
|
* |
|
40
|
|
|
* @throws \eZ\Publish\Core\REST\Common\Exceptions\Parser |
|
41
|
|
|
* |
|
42
|
|
|
* @return \eZ\Publish\API\Repository\Values\Content\Query\Criterion\Field |
|
43
|
|
|
*/ |
|
44
|
|
|
public function parse(array $data, ParsingDispatcher $parsingDispatcher) |
|
45
|
|
|
{ |
|
46
|
|
|
if (!array_key_exists('Field', $data)) { |
|
47
|
|
|
throw new Exceptions\Parser('Invalid <Field> format'); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
$fieldData = $data['Field']; |
|
51
|
|
|
if (empty($fieldData['name']) || empty($fieldData['operator']) || !array_key_exists('value', $fieldData)) { |
|
52
|
|
|
throw new Exceptions\Parser('<Field> format expects name, operator and value keys'); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
$operator = $this->getOperator($fieldData['operator']); |
|
56
|
|
|
|
|
57
|
|
|
return new FieldCriterion( |
|
|
|
|
|
|
58
|
|
|
$fieldData['name'], |
|
59
|
|
|
$operator, |
|
60
|
|
|
$fieldData['value'] |
|
61
|
|
|
); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Get operator for the given literal name. |
|
66
|
|
|
* |
|
67
|
|
|
* For the full list of supported operators: |
|
68
|
|
|
* @see \eZ\Publish\Core\REST\Server\Input\Parser\Criterion\Field::OPERATORS |
|
69
|
|
|
* |
|
70
|
|
|
* @param string $operatorName operator literal operator name |
|
71
|
|
|
* |
|
72
|
|
|
* @return string |
|
73
|
|
|
*/ |
|
74
|
|
|
private function getOperator($operatorName) |
|
75
|
|
|
{ |
|
76
|
|
|
$operatorName = strtoupper($operatorName); |
|
77
|
|
|
if (!isset(self::OPERATORS[$operatorName])) { |
|
78
|
|
|
throw new Exceptions\Parser( |
|
79
|
|
|
sprintf( |
|
80
|
|
|
'Unexpected Field operator, expected one of the following: %s', |
|
81
|
|
|
implode(', ', array_keys(self::OPERATORS)) |
|
82
|
|
|
) |
|
83
|
|
|
); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
return self::OPERATORS[$operatorName]; |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: