Completed
Push — 6.7 ( 341122...65da37 )
by
unknown
19:52
created

Query::buildQuery()

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
nc 1
dl 0
loc 1
c 0
b 0
f 0
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;
8
9
use eZ\Publish\Core\REST\Common\Input\ParsingDispatcher;
10
use eZ\Publish\Core\REST\Server\Input\Parser\Criterion as CriterionParser;
11
use eZ\Publish\API\Repository\Values\Content\Query\Criterion as CriterionValue;
12
13
/**
14
 * Content/Location Query Parser.
15
 */
16
abstract class Query extends CriterionParser
17
{
18
    /**
19
     * Parses input structure to a Query.
20
     *
21
     * @param array $data
22
     * @param \eZ\Publish\Core\REST\Common\Input\ParsingDispatcher $parsingDispatcher
23
     *
24
     * @throws \eZ\Publish\Core\REST\Common\Exceptions\Parser
25
     *
26
     * @return \eZ\Publish\API\Repository\Values\Content\Query
27
     */
28
    public function parse(array $data, ParsingDispatcher $parsingDispatcher)
29
    {
30
        $query = $this->buildQuery();
31
32
        // Criteria
33
        // -- FullTextCriterion
34
        if (array_key_exists('Criteria', $data) && is_array($data['Criteria'])) {
35
            $message = 'The Criteria element is deprecated since ezpublish-kernel 6.6.0, and will be removed in 7.0. Use Filter instead.';
36
            if (array_key_exists('Filter', $data) && is_array($data['Filter'])) {
37
                $message .= ' The Criteria element will be merged into Filter.';
38
                $data['Filter'] = array_merge($data['Filter'], $data['Criteria']);
39
            } else {
40
                $data['Filter'] = $data['Criteria'];
41
            }
42
43
            @trigger_error($message, E_USER_DEPRECATED);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
44
        }
45
46 View Code Duplication
        if (array_key_exists('Filter', $data) && is_array($data['Filter'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
47
            $query->filter = $this->processCriteriaArray($data['Filter'], $parsingDispatcher);
48
        }
49
50 View Code Duplication
        if (array_key_exists('Query', $data) && is_array($data['Query'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
51
            $query->query = $this->processCriteriaArray($data['Query'], $parsingDispatcher);
52
        }
53
54
        // limit
55
        if (array_key_exists('limit', $data)) {
56
            $query->limit = (int)$data['limit'];
57
        }
58
59
        // offset
60
        if (array_key_exists('offset', $data)) {
61
            $query->offset = (int)$data['offset'];
62
        }
63
64
        // SortClauses
65
        // -- [SortClauseName: direction|data]
66
        if (array_key_exists('SortClauses', $data)) {
67
            $query->sortClauses = $this->processSortClauses($data['SortClauses'], $parsingDispatcher);
68
        }
69
70
        // FacetBuilders
71
        // -- contentTypeFacetBuilder
72
        if (array_key_exists('FacetBuilders', $data)) {
73
        }
74
75
        return $query;
76
    }
77
78
    /**
79
     * Builds and returns the Query (Location or Content object).
80
     * @return \eZ\Publish\API\Repository\Values\Content\Query
81
     */
82
    abstract protected function buildQuery();
83
84
    /**
85
     * @param array $criteriaArray
86
     * @param ParsingDispatcher $parsingDispatcher
87
     *
88
     * @return CriterionValue|null A criterion, or a LogicalAnd with a set of Criterion, or null if an empty array was given
89
     */
90
    private function processCriteriaArray(array $criteriaArray, ParsingDispatcher $parsingDispatcher)
91
    {
92
        if (count($criteriaArray) === 0) {
93
            return null;
94
        }
95
96
        $criteria = array();
97
        foreach ($criteriaArray as $criterionName => $criterionData) {
98
            $criteria[] = $this->dispatchCriterion($criterionName, $criterionData, $parsingDispatcher);
99
        }
100
101
        return (count($criteria) === 1) ? $criteria[0] : new CriterionValue\LogicalAnd($criteria);
102
    }
103
104
    /**
105
     * Handles SortClause data.
106
     *
107
     * @param array $sortClausesArray
108
     * @param \eZ\Publish\Core\REST\Common\Input\ParsingDispatcher $parsingDispatcher
109
     *
110
     * @return array
111
     */
112
    private function processSortClauses(array $sortClausesArray, ParsingDispatcher $parsingDispatcher)
113
    {
114
        $sortClauses = [];
115
        foreach ($sortClausesArray as $sortClauseName => $sortClauseData) {
116
            if (!is_array($sortClauseData) || !isset($sortClauseData[0])) {
117
                $sortClauseData = [$sortClauseData];
118
            }
119
120
            foreach ($sortClauseData as $data) {
121
                $sortClauses[] = $this->dispatchSortClause($sortClauseName, $data, $parsingDispatcher);
122
            }
123
        }
124
125
        return $sortClauses;
126
    }
127
}
128