Completed
Push — ezp-30806-tmp-7.5 ( 069bb0 )
by
unknown
15:57
created

LogicalOperator::getFlattenedCriteriaData()   B

Complexity

Conditions 6
Paths 10

Size

Total Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
nc 10
nop 1
dl 0
loc 31
rs 8.8017
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * File containing the LogicalOperator 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\Core\REST\Common\Input\ParsingDispatcher;
12
use eZ\Publish\Core\REST\Server\Input\Parser\Criterion;
13
14
/**
15
 * Parser for LogicalOperator Criterion.
16
 */
17
class LogicalOperator extends Criterion
18
{
19
    /**
20
     * Parses input structure to a Criterion object.
21
     *
22
     * @param array $data
23
     * @param \eZ\Publish\Core\REST\Common\Input\ParsingDispatcher $parsingDispatcher
24
     *
25
     * @throws \eZ\Publish\Core\REST\Common\Exceptions\Parser
26
     *
27
     * @return \eZ\Publish\API\Repository\Values\Content\Query\Criterion\LogicalOperator
28
     */
29
    public function parse(array $data, ParsingDispatcher $parsingDispatcher)
30
    {
31
        throw new \Exception('@todo implement');
32
    }
33
34
    /**
35
     * @param array $criteriaByType
36
     * @return array
37
     */
38
    protected function getFlattenedCriteriaData(array $criteriaByType)
39
    {
40
        if ($this->isZeroBasedArray($criteriaByType)) {
41
            $oldFormat = $criteriaByType;
42
            $criteriaByType = $this->normalizeCriteriaByType($criteriaByType);
43
            @trigger_error(
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
                sprintf(
45
                    'REST View: Passing criteria as a list of objects to a logical operator is deprecated and will cause Bad Request error in eZ Platform 3.0. Instead of "%s" provide "%s"',
46
                    json_encode($oldFormat),
47
                    json_encode($criteriaByType)
48
                ),
49
                E_USER_DEPRECATED
50
            );
51
        }
52
53
        $criteria = [];
54
        foreach ($criteriaByType as $type => $criterion) {
55
            if (!is_array($criterion) || !$this->isZeroBasedArray($criterion)) {
56
                $criterion = [$criterion];
57
            }
58
59
            foreach ($criterion as $criterionElement) {
60
                $criteria[] = [
61
                    'type' => $type,
62
                    'data' => $criterionElement,
63
                ];
64
            }
65
        }
66
67
        return $criteria;
68
    }
69
70
    /**
71
     * Checks if the given $value is zero based.
72
     *
73
     * @param array $value
74
     *
75
     * @return bool
76
     */
77
    protected function isZeroBasedArray(array $value)
78
    {
79
        reset($value);
80
81
        return empty($value) || key($value) === 0;
82
    }
83
84
    /**
85
     * Normalize list of criteria to be provided as the expected criterion type to its value map.
86
     *
87
     * Changes:
88
     * <code>
89
     * [
90
     *  0 => "CriterionType1" => "<value1>",
91
     *  1 => "CriterionType1" => "<value2>",
92
     *  2 => "CriterionType2" => "<value3>",
93
     * ]
94
     * </code>
95
     * into:
96
     * <code>
97
     * [
98
     *  "CriterionType1" => ["<value1>", "<value2>"],
99
     *  "CriterionType2" => ["<value3>"],
100
     * ]
101
     * </code>
102
     *
103
     * @param array $criterionList zero-based list of criteria
104
     *
105
     * @return array map of criterion types to their values
106
     */
107
    private function normalizeCriteriaByType(array $criterionList)
108
    {
109
        $criteriaByType = [];
110
        foreach ($criterionList as $criterion) {
111
            foreach ($criterion as $criterionType => $value) {
112
                if (!isset($criteriaByType[$criterionType])) {
113
                    $criteriaByType[$criterionType] = [];
114
                }
115
116
                $criteriaByType[$criterionType] = array_merge(
117
                    $criteriaByType[$criterionType],
118
                    !is_array($value) ? [$value] : $value
119
                );
120
            }
121
        }
122
123
        return $criteriaByType;
124
    }
125
}
126