Completed
Push — 7.5 ( 05f391...b433d9 )
by
unknown
19:31 queued 10s
created

LogicalOperator::getFlattenedCriteriaData()   B

Complexity

Conditions 6
Paths 10

Size

Total Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

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