Passed
Push — master ( ce51b5...ab932c )
by Juan
06:01
created

ProcessQueryParamsEvent::getOriginalParamValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Micayael\NativeQueryFromFileBuilderBundle\Event;
4
5
use Symfony\Component\EventDispatcher\Event;
6
7
class ProcessQueryParamsEvent extends Event
8
{
9
    /**
10
     * @var string
11
     */
12
    private $snippetKey;
13
14
    /**
15
     * @var array
16
     */
17
    private $originalParams;
18
19
    /**
20
     * @var string
21
     */
22
    private $paramKey;
23
24
    /**
25
     * @var string
26
     */
27
    private $filterType;
28
29
    /**
30
     * @var string
31
     */
32
    private $originalFilter;
33
34
    /**
35
     * @var array
36
     */
37
    private $processedParams = [];
38
39
    /**
40
     * @var string
41
     */
42
    private $processedFilter;
43
44
    public function __construct(string $snippetKey, ?string $filterType = null, string $paramKey, array $originalParams, string $originalFilter)
45
    {
46
        $this->snippetKey = $snippetKey;
47
        $this->filterType = $filterType;
48
49
        $this->paramKey = $paramKey;
50
51
        $this->originalParams = $originalParams;
52
        $this->originalFilter = $originalFilter;
53
54
        $this->processedParams = $originalParams;
55
        $this->processedFilter = $originalFilter;
56
57
        switch ($filterType) {
58
            case 'like':
0 ignored issues
show
Coding Style introduced by
case statements should be defined using a colon.

As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next break.

There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.

switch ($expr) {
    case "A": { //wrong
        doSomething();
        break;
    }
    case "B"; //wrong
        doSomething();
        break;
    case "C": //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
59
                $newValue = '%'.$this->getOriginalParamValue().'%';
60
                $this->replaceParamValue($newValue);
61
                break;
62
            case 'string_any':
63
                $newValue = '{'.implode(', ', array_map('strval', $this->getOriginalParamValue())).'}';
64
                $this->replaceParamValue($newValue);
65
                break;
66
            case 'numeric_any':
67
                $newValue = '{'.implode(', ', array_map('intval', $this->getOriginalParamValue())).'}';
68
                $this->replaceParamValue($newValue);
69
                break;
70
            case 'in':
71
                $newFilter = '';
72
                $newParams = [];
73
74
                $i = 0;
75
76
                foreach ($this->getOriginalParamValue() as $value) {
77
                    $newFilter .= ':'.$paramKey.'_'.$i.',';
78
                    $newParams[$paramKey.'_'.$i] = $value;
79
80
                    ++$i;
81
                }
82
83
                if (strlen($newFilter) > 0) {
84
                    $newFilter = substr($newFilter, 0, strlen($newFilter) - 1);
85
                }
86
87
                $this->removeParams([$this->getParamKey()]);
88
89
                $processedParams = array_merge($this->processedParams, $newParams);
90
                $processedFilter = str_replace(':'.$paramKey, $newFilter, $originalFilter);
91
92
                $this->setProcessedParams($processedParams);
93
                $this->setProcessedFilter($processedFilter);
94
95
                break;
96
        }
97
    }
98
99
    /**
100
     * Return the value of the current param to be processed.
101
     */
102
    public function getOriginalParamValue()
103
    {
104
        return $this->originalParams[$this->paramKey];
105
    }
106
107
    /**
108
     * Returns params after they have been processed.
109
     */
110
    public function getProcessedParams(): array
111
    {
112
        return $this->processedParams;
113
    }
114
115
    /**
116
     * Set processed params.
117
     *
118
     * @param array $params
119
     */
120
    public function setProcessedParams(array $params): void
121
    {
122
        $this->processedParams = $params;
123
    }
124
125
    /**
126
     * Return the filter of the current param after it has been processed.
127
     */
128
    public function getProcessedFilter(): string
129
    {
130
        return $this->processedFilter;
131
    }
132
133
    /**
134
     * Replaces the value of the current filter.
135
     *
136
     * @param string $filter
137
     */
138
    public function setProcessedFilter(string $filter): void
139
    {
140
        $this->processedFilter = $filter;
141
    }
142
143
    /**
144
     * Remove a param for the list of processed params.
145
     *
146
     * @param array $paramKeys
147
     */
148
    public function removeParams(array $paramKeys): void
149
    {
150
        foreach ($paramKeys as $paramKey) {
151
            unset($this->processedParams[$paramKey]);
152
        }
153
    }
154
155
    /**
156
     * Replaces the value of the current param.
157
     *
158
     * @param $newValue
159
     */
160
    public function replaceParamValue($newValue): void
161
    {
162
        $this->addOrReplaceParamValue($this->paramKey, $newValue);
163
    }
164
165
    /**
166
     * Adds or replaces the value of a para defined by $paramKey.
167
     *
168
     * @param string $paramKey
169
     * @param $newValue
170
     */
171
    public function addOrReplaceParamValue(string $paramKey, $newValue): void
172
    {
173
        $this->processedParams[$paramKey] = $newValue;
174
    }
175
176
    /**
177
     * Return the type of the param to be processed.
178
     */
179
    public function getFilterType(): ?string
180
    {
181
        return $this->filterType;
182
    }
183
184
    /**
185
     * Return the key of the param to be processed.
186
     */
187
    public function getParamKey(): string
188
    {
189
        return $this->paramKey;
190
    }
191
192
    /**
193
     * Return the key of the snnipet query key: products.base.
194
     */
195
    public function getSnippetKey()
196
    {
197
        return $this->snippetKey;
198
    }
199
200
    /**
201
     * Return the original params and values passed to the bundle.
202
     */
203
    public function getOriginalParams(): array
204
    {
205
        return $this->originalParams;
206
    }
207
208
    public function getOriginalFilter(): string
209
    {
210
        return $this->originalFilter;
211
    }
212
}
213