1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @copyright Copyright (C) eZ Systems AS. All rights reserved. |
5
|
|
|
* @license For full copyright and license information view LICENSE file distributed with this source code. |
6
|
|
|
*/ |
7
|
|
|
namespace eZ\Publish\Core\REST\Server\Input\Parser\SortClause; |
8
|
|
|
|
9
|
|
|
use eZ\Publish\API\Repository\Values\Content\Query; |
10
|
|
|
use eZ\Publish\Core\REST\Common\Input\BaseParser; |
11
|
|
|
use eZ\Publish\Core\REST\Common\Input\ParsingDispatcher; |
12
|
|
|
use eZ\Publish\Core\REST\Common\Exceptions; |
13
|
|
|
use eZ\Publish\API\Repository\Values\Content\Query\SortClause\Field as FieldSortClause; |
14
|
|
|
|
15
|
|
|
class Field extends BaseParser |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* Parse input structure for Field sort clause. |
19
|
|
|
* |
20
|
|
|
* @param array $data |
21
|
|
|
* @param \eZ\Publish\Core\REST\Common\Input\ParsingDispatcher $parsingDispatcher |
22
|
|
|
* |
23
|
|
|
* @return \eZ\Publish\API\Repository\Values\Content\Query\SortClause\Field |
24
|
|
|
*/ |
25
|
|
|
public function parse(array $data, ParsingDispatcher $parsingDispatcher) |
26
|
|
|
{ |
27
|
|
|
if (!isset($data['Field'])) { |
28
|
|
|
throw new Exceptions\Parser("The <Field> sort clause doesn't exist in the input structure"); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
if (!is_array($data['Field'])) { |
32
|
|
|
throw new Exceptions\Parser('The <Field> sort clause has missing arguments: contentTypeIdentifier, fieldDefinitionIdentifier'); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
$data['Field'] = $this->normalizeData($data['Field']); |
36
|
|
|
|
37
|
|
|
$direction = isset($data['Field']['direction']) ? $data['Field']['direction'] : null; |
38
|
|
|
|
39
|
|
View Code Duplication |
if (!in_array($direction, [Query::SORT_ASC, Query::SORT_DESC])) { |
|
|
|
|
40
|
|
|
throw new Exceptions\Parser('Invalid direction format in <Field> sort clause'); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
if (isset($data['Field']['identifier'])) { |
44
|
|
|
if (false === strpos($data['Field']['identifier'], '/')) { |
45
|
|
|
throw new Exceptions\Parser('<Field> sort clause parameter "identifier" value has to be in "contentTypeIdentifier/fieldDefinitionIdentifier" format'); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
list($contentTypeIdentifier, $fieldDefinitionIdentifier) = explode('/', $data['Field']['identifier'], 2); |
49
|
|
|
} else { |
50
|
|
|
if (!isset($data['Field']['contentTypeIdentifier'])) { |
51
|
|
|
throw new Exceptions\Parser('<Field> sort clause have missing parameter "contentTypeIdentifier"'); |
52
|
|
|
} |
53
|
|
|
if (!isset($data['Field']['fieldDefinitionIdentifier'])) { |
54
|
|
|
throw new Exceptions\Parser('<Field> sort clause have missing parameter "fieldDefinitionIdentifier"'); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$contentTypeIdentifier = $data['Field']['contentTypeIdentifier']; |
58
|
|
|
$fieldDefinitionIdentifier = $data['Field']['fieldDefinitionIdentifier']; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
return new FieldSortClause($contentTypeIdentifier, $fieldDefinitionIdentifier, $direction); |
|
|
|
|
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Normalize passed Field Sort Clause data by making both xml and json parameters to have same names (by dropping |
66
|
|
|
* xml "_" prefix and changing "#text" xml attribute to "direction"). |
67
|
|
|
* |
68
|
|
|
* @param array $data |
69
|
|
|
* |
70
|
|
|
* @return array |
71
|
|
|
*/ |
72
|
|
|
private function normalizeData($data) |
73
|
|
|
{ |
74
|
|
|
$normalizedData = []; |
75
|
|
|
|
76
|
|
|
foreach ($data as $key => $value) { |
77
|
|
|
if ('#text' === $key) { |
78
|
|
|
$key = 'direction'; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
$normalizedData[trim($key, '_')] = $value; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return $normalizedData; |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.