FilterCondition::toUQL()   B
last analyzed

Complexity

Conditions 6
Paths 5

Size

Total Lines 40
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 40
c 0
b 0
f 0
rs 8.439
cc 6
eloc 27
nc 5
nop 0
1
<?php
2
3
namespace Netdudes\DataSourceryBundle\Query;
4
5
use Netdudes\DataSourceryBundle\UQL\Interpreter;
6
7
/**
8
 * Defines a filter
9
 */
10
class FilterCondition
11
{
12
    /**
13
     * Filter types: standard string and numeric comparison.
14
     * This set of comparison methods are understood by any DataSource that wants to be easily swappable
15
     *
16
     * Additional filtering methods may be defined to be consumed by custom data sources.
17
     */
18
19
    const METHOD_STRING_EQ = "STRING_EQ";
20
    const METHOD_STRING_LIKE = "STRING_LIKE";
21
    const METHOD_STRING_NEQ = "STRING_NEQ";
22
    const METHOD_NUMERIC_GT = "NUMERIC_GT";
23
    const METHOD_NUMERIC_GTE = "NUMERIC_GTE";
24
    const METHOD_NUMERIC_EQ = "NUMERIC_EQ";
25
    const METHOD_NUMERIC_LTE = "NUMERIC_LTE";
26
    const METHOD_NUMERIC_LT = "NUMERIC_LT";
27
    const METHOD_NUMERIC_NEQ = "NUMERIC_NEQ";
28
    const METHOD_IN = "IN";
29
    const METHOD_NIN = "NIN";
30
    const METHOD_BOOLEAN = "BOOLEAN";
31
    const METHOD_DATETIME_GT = "DATETIME_GT";
32
    const METHOD_DATETIME_GTE = "DATETIME_GTE";
33
    const METHOD_DATETIME_EQ = "DATETIME_EQ";
34
    const METHOD_DATETIME_LTE = "DATETIME_LTE";
35
    const METHOD_DATETIME_LT = "DATETIME_LT";
36
    const METHOD_DATETIME_NEQ = "DATETIME_NEQ";
37
38
    /**
39
     * Column ID to filter by
40
     *
41
     * @var string
42
     */
43
    private $fieldName;
44
45
    /**
46
     * Filtering method, of those defined in this class' constants
47
     *
48
     * @var string
49
     */
50
    private $method;
51
52
    /**
53
     * A value to filter by
54
     *
55
     * @var mixed
56
     */
57
    private $value;
58
59
    /**
60
     * @var mixed
61
     */
62
    private $valueInDatabase;
63
64
    /**
65
     * @param $columnIdentifier  string The unique identifier of the column to filter by
66
     * @param $method            string A value  passed to the data source in order to decide how to filter
67
     * @param $value             string The value to filter with
68
     * @param $valueInDatabase   string The presentation of the filtered value inside the database
69
     */
70
    public function __construct($columnIdentifier, $method, $value, $valueInDatabase)
71
    {
72
        $this->fieldName = $columnIdentifier;
73
        $this->method = $method;
74
        $this->value = $value;
75
        $this->valueInDatabase = $valueInDatabase;
76
    }
77
78
    /**
79
     * Returns an array serializable to JSON for the compact exchange format.
80
     *
81
     * @return array
82
     */
83
    public function toJsonSerializable()
84
    {
85
        return [
86
            'field' => $this->fieldName,
87
            'method' => $this->method,
88
            'value' => $this->value
89
        ];
90
    }
91
92
    /**
93
     * Get a UQL representation of this filter statement
94
     *
95
     * @return string
96
     */
97
    public function toUQL()
98
    {
99
        $numericMethods = [
100
            self::METHOD_NUMERIC_EQ,
101
            self::METHOD_NUMERIC_GT,
102
            self::METHOD_NUMERIC_GTE,
103
            self::METHOD_NUMERIC_LT,
104
            self::METHOD_NUMERIC_LTE,
105
            self::METHOD_NUMERIC_NEQ,
106
        ];
107
108
        $booleanMethods = [
109
            self::METHOD_BOOLEAN,
110
        ];
111
112
        $value = $this->getValue();
113
        if (is_array($value)) {
114
            // Array value (e.g. a IN statement)
115
            // Wrap every element in quotes, separate by commas and make into a square-bracket array.
116
            $value = '[' . implode(
117
                    ', ',
118
                    array_map(
119
                        function ($string) {
120
                            return "\"$string\"";
121
                        },
122
                        $value
123
                    )
124
                ) . ']';
125
        } else {
126
            if (in_array($this->method, $booleanMethods)) {
127
                $value = $value ? 'true' : 'false';
128
            } elseif (in_array($this->method, $numericMethods) && preg_match("/^[0-9]+$/", trim($value))) {
129
                $value = trim($value);
130
            } else {
131
                $value = "\"$value\"";
132
            }
133
        }
134
135
        return $this->getFieldName() . " " . Interpreter::methodToUQLOperator($this->getMethod()) . " " . $value;
136
    }
137
138
    /**
139
     * @return mixed
140
     */
141
    public function getValue()
142
    {
143
        return $this->value;
144
    }
145
146
    /**
147
     * @param mixed $value
148
     */
149
    public function setValue($value)
150
    {
151
        $this->value = $value;
152
    }
153
154
    /**
155
     * @return mixed
156
     */
157
    public function getFieldName()
158
    {
159
        return $this->fieldName;
160
    }
161
162
    /**
163
     * @param mixed $fieldName
164
     */
165
    public function setFieldName($fieldName)
166
    {
167
        $this->fieldName = $fieldName;
168
    }
169
170
    /**
171
     * @return mixed
172
     */
173
    public function getMethod()
174
    {
175
        return $this->method;
176
    }
177
178
    /**
179
     * @param mixed $method
180
     */
181
    public function setMethod($method)
182
    {
183
        $this->method = $method;
184
    }
185
186
    /**
187
     * @return mixed
188
     */
189
    public function getValueInDatabase()
190
    {
191
        return $this->valueInDatabase;
192
    }
193
194
    /**
195
     * @param mixed $valueInDatabase
196
     */
197
    public function setValueInDatabase($valueInDatabase)
198
    {
199
        $this->valueInDatabase = $valueInDatabase;
200
    }
201
}
202