Completed
Push — master ( 5f67eb...e271d9 )
by
unknown
48:48 queued 23:12
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
        // @deprecated 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, and will be removed in 8.0. Use Filter instead, or Query for criteria that should affect scoring.';
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
        // -- facetBuilderListType
72
        if (array_key_exists('FacetBuilders', $data)) {
73
            $facetBuilders = [];
74
            foreach ($data['FacetBuilders'] as $facetBuilderName => $facetBuilderData) {
75
                $facetBuilders[] = $this->dispatchFacetBuilder($facetBuilderName, $facetBuilderData, $parsingDispatcher);
76
            }
77
            $query->facetBuilders = $facetBuilders;
78
        }
79
80
        return $query;
81
    }
82
83
    /**
84
     * Builds and returns the Query (Location or Content object).
85
     * @return \eZ\Publish\API\Repository\Values\Content\Query
86
     */
87
    abstract protected function buildQuery();
88
89
    /**
90
     * @param array $criteriaArray
91
     * @param ParsingDispatcher $parsingDispatcher
92
     *
93
     * @return CriterionValue|null A criterion, or a LogicalAnd with a set of Criterion, or null if an empty array was given
94
     */
95
    private function processCriteriaArray(array $criteriaArray, ParsingDispatcher $parsingDispatcher)
96
    {
97
        if (count($criteriaArray) === 0) {
98
            return null;
99
        }
100
101
        $criteria = array();
102
        foreach ($criteriaArray as $criterionName => $criterionData) {
103
            $criteria[] = $this->dispatchCriterion($criterionName, $criterionData, $parsingDispatcher);
104
        }
105
106
        return (count($criteria) === 1) ? $criteria[0] : new CriterionValue\LogicalAnd($criteria);
107
    }
108
109
    /**
110
     * Handles SortClause data.
111
     *
112
     * @param array $sortClausesArray
113
     * @param \eZ\Publish\Core\REST\Common\Input\ParsingDispatcher $parsingDispatcher
114
     *
115
     * @return array
116
     */
117
    private function processSortClauses(array $sortClausesArray, ParsingDispatcher $parsingDispatcher)
118
    {
119
        $sortClauses = [];
120
        foreach ($sortClausesArray as $sortClauseName => $sortClauseData) {
121
            if (!is_array($sortClauseData) || !isset($sortClauseData[0])) {
122
                $sortClauseData = [$sortClauseData];
123
            }
124
125
            foreach ($sortClauseData as $data) {
126
                $sortClauses[] = $this->dispatchSortClause($sortClauseName, $data, $parsingDispatcher);
127
            }
128
        }
129
130
        return $sortClauses;
131
    }
132
}
133