Filter::createFromJsonSerializable()   B
last analyzed

Complexity

Conditions 6
Paths 5

Size

Total Lines 27
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 27
rs 8.439
cc 6
eloc 15
nc 5
nop 1
1
<?php
2
namespace Netdudes\DataSourceryBundle\Query;
3
4
use Netdudes\DataSourceryBundle\Util\AbstractArrayAccessibleCollection;
5
6
/**
7
 * Group of filters bundled with a condition type
8
 */
9
class Filter extends AbstractArrayAccessibleCollection implements \JsonSerializable
10
{
11
    /**
12
     * The filters are concatenated AND-wise
13
     */
14
    const CONDITION_TYPE_AND = 'AND';
15
16
    /**
17
     * The filters are concatenated OR-wise
18
     */
19
    const CONDITION_TYPE_OR = 'OR';
20
21
    /**
22
     * The filters are concatenated XOR-wise
23
     * Note: This is not currently supported by Doctrine, but left in for completeness
24
     */
25
    const CONDITION_TYPE_XOR = 'XOR';
26
27
    /**
28
     * Condition concatenation type, by default OR
29
     *
30
     * @var int
31
     */
32
    private $conditionType = self::CONDITION_TYPE_AND;
33
34
    /**
35
     * Create a FilterDefinition from a deserialized compact exchange JSON filters.
36
     * This wil recursively call itself to unpack the whole filter tree.
37
     *
38
     * @param $jsonSerializable
39
     *
40
     * @return array|Filter|FilterCondition
41
     * @throws \Exception
42
     * @deprecated Deprecated to be removed in 1.0.0. It seems to be unused.
43
     */
44
    public static function createFromJsonSerializable($jsonSerializable)
45
    {
46
        @trigger_error('createFromJsonSerializable() is deprecated and will be removed in 1.0.0. It seems to be unused.', 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...
47
48
        if (!count((array) $jsonSerializable)) {
49
            // Edge case: empty filter
50
            return new Filter();
51
        }
52
53
        if (count($jsonSerializable) == 2 && is_array($jsonSerializable[1])) {
54
            // Complex Filter
55
            $logic = $jsonSerializable[0];
56
            $elements = $jsonSerializable[1];
57
            $filter = new Filter();
58
            $filter->setConditionType($logic);
59
            foreach ($elements as $element) {
60
                $filter[] = static::createFromJsonSerializable($element);
0 ignored issues
show
Deprecated Code introduced by
The method Netdudes\DataSourceryBun...eFromJsonSerializable() has been deprecated with message: Deprecated to be removed in 1.0.0. It seems to be unused.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
61
            }
62
63
            return $filter;
64
        } elseif (count($jsonSerializable) == 3) {
65
            // Simple element
66
            return new FilterCondition($jsonSerializable[0], $jsonSerializable[1], $jsonSerializable[2], $jsonSerializable[2]);
67
        }
68
69
        throw new \Exception('Unable to parse JSON-serializable filter structure');
70
    }
71
72
    public function toUQL()
73
    {
74
        $parts = [];
75
        foreach ($this as $filter) {
76
            $subFilterUql = $filter->toUQL();
77
            if ($filter instanceof Filter) {
78
                $subFilterUql = '(' . $subFilterUql . ')';
79
            }
80
            $parts[] = $subFilterUql;
81
        }
82
83
        return implode(" " . strtolower($this->conditionType) . " ", $parts);
84
    }
85
86
    /**
87
     * (PHP 5 &gt;= 5.4.0)<br/>
88
     * Specify data which should be serialized to JSON
89
     *
90
     * @link http://php.net/manual/en/jsonserializable.jsonserialize.php
91
     * @return mixed data which can be serialized by <b>json_encode</b>,
92
     *               which is a value of any type other than a resource.
93
     */
94
    public function jsonSerialize()
95
    {
96
        $serializableArray = [
97
            'logic' => $this->getConditionType(),
98
            'elements' => [],
99
        ];
100
        foreach ($this as $filter) {
101
            $serializableArray['elements'][] = $filter->toJsonSerializable();
102
        }
103
104
        return $serializableArray;
105
    }
106
107
    /**
108
     * Recursively compact into a easily JSON-serializable array
109
     * representation
110
     * @deprecated Deprecated to be removed in 1.0.0. It seems to be unused.
111
     */
112
    public function toJsonSerializable()
113
    {
114
        @trigger_error('toJsonSerializable() is deprecated and will be removed in 1.0.0. It seems to be unused.', 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...
115
116
        return $this->jsonSerialize();
117
    }
118
119
    /**
120
     * @return int
121
     */
122
    public function getConditionType()
123
    {
124
        return $this->conditionType;
125
    }
126
127
    /**
128
     * @param int $conditionType
129
     */
130
    public function setConditionType($conditionType)
131
    {
132
        $this->conditionType = $conditionType;
133
    }
134
135
    public function getAllFirstLevelFilteredDataSourceUniqueNames()
136
    {
137
        $filteredDataSourceIds = [];
138
        foreach ($this as $element) {
139
            if ($element instanceof FilterCondition) {
140
                $filteredDataSourceIds[] = $element->getFieldName();
141
            }
142
        }
143
144
        return $filteredDataSourceIds;
145
    }
146
147
    /**
148
     * Flatten the tree of Filters into a one-dimensional array
149
     * or FilterConditions.
150
     *
151
     * @return FilterCondition[]
152
     */
153
    public function getAllFilterConditionsFlat()
154
    {
155
        $flatConditionsList = [];
156
        $flatten = function (Filter $filterDefinition) use (&$flatConditionsList, &$flatten) {
157
            foreach ($filterDefinition as $filter) {
158
                if ($filter instanceof Filter) {
159
                    $flatten($filter);
160
                } elseif ($filter instanceof FilterCondition) {
161
                    $flatConditionsList[] = $filter;
162
                }
163
            }
164
        };
165
        $flatten($this);
166
167
        return $flatConditionsList;
168
    }
169
}
170