Passed
Push — master ( 98954f...37aea2 )
by Andrea
16:13 queued 12s
created

getDescrizioneFiltroDate()   B

Complexity

Conditions 7
Paths 13

Size

Total Lines 29
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 7.457

Importance

Changes 0
Metric Value
cc 7
eloc 20
nc 13
nop 1
dl 0
loc 29
ccs 15
cts 19
cp 0.7895
crap 7.457
rs 8.6666
c 0
b 0
f 0
1
<?php
2
3
namespace Cdf\BiCoreBundle\Utils\Tabella;
4
5
use DateTime;
6
use Doctrine\Common\Collections\Expr\Comparison;
7
8
class BaseParametriQueryTabellaDecoder
9
{
10
11
    protected string $fieldname;
12
    protected string $fieldoperator;
13
14
    /**
15
     *
16
     * @var mixed
17
     */
18
    protected $fieldvalue;
19
    protected string $fieldqueryparameter;
20
21
    /**
22
     *
23
     * @var string|null
24
     */
25
    protected $criteria;
26
27
    /**
28
     *
29
     * @var array<mixed>
30
     */
31
    protected $parameters;
32
33
    /**
34
     *
35
     * @var array<mixed>
36
     */
37
    protected $fieldinfo;
38
39
    /**
40
     *
41
     * @param string $fieldname
42
     * @param string $fieldoperator
43
     * @param mixed $fieldvalue
44
     * @param string $fieldqueryparameter
45
     * @param array<mixed> $fieldinfo
46
     */
47 2
    public function __construct(string $fieldname, string $fieldoperator, $fieldvalue, string $fieldqueryparameter, $fieldinfo)
48
    {
49 2
        $this->fieldname = $fieldname;
50 2
        $this->fieldoperator = $fieldoperator;
51 2
        if (is_string($fieldvalue)) {
52 2
            $this->fieldvalue = urldecode($fieldvalue);
53
        } else {
54 2
            $this->fieldvalue = $fieldvalue;
55
        }
56 2
        $this->fieldqueryparameter = $fieldqueryparameter;
57 2
        $this->fieldinfo = $fieldinfo;
58 2
        $this->parameters = array();
59
        /** @phpstan-ignore-next-line */
60 2
        $this->buildQuery();
0 ignored issues
show
introduced by
The method buildQuery() does not exist on Cdf\BiCoreBundle\Utils\T...etriQueryTabellaDecoder. Maybe you want to declare this class abstract? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

60
        $this->/** @scrutinizer ignore-call */ 
61
               buildQuery();
Loading history...
61 2
    }
62
63
    /**
64
     *
65
     * @return string|null
66
     */
67 2
    public function getQueryCriteria()
68
    {
69 2
        return $this->criteria;
70
    }
71
72
    /**
73
     *
74
     * @return array<mixed>
75
     */
76 2
    public function getQueryParameters(): array
77
    {
78 2
        return $this->parameters;
79
    }
80
81 1
    public function getDescrizioneFiltro(): string
82
    {
83 1
        $descrizionevalore = '';
84
85
        switch (true) {
86 1
            case $this->getDescrizioneFiltroIsNull($descrizionevalore):
87 1
                break;
88 1
            case $this->getDescrizioneFiltroDecodifiche($descrizionevalore):
89
                break;
90 1
            case $this->getDescrizioneFiltroBoolean($descrizionevalore):
91
                break;
92 1
            case $this->getDescrizioneFiltroDate($descrizionevalore):
93 1
                break;
94 1
            case $this->getDescrizioneFiltroArray($descrizionevalore):
95 1
                break;
96 1
            case $this->getDescrizioneFiltroString($descrizionevalore):
97 1
                break;
98
            default:
99 1
                $this->getDescrizioneFiltroAltro($descrizionevalore);
100 1
                break;
101
        }
102 1
        $nomecampo = substr($this->fieldname, stripos($this->fieldname, '.') + 1);
103 1
        $filtro = $nomecampo . ' ' . $this->operatorToString($this->fieldoperator) . ' ' . $descrizionevalore;
104
105 1
        return $filtro;
106
    }
107
108 1
    protected function getDescrizioneFiltroAltro(string &$descrizionevalore): void
109
    {
110 1
        if ('' == $descrizionevalore) {
111 1
            $descrizionevalore = "'" . $this->fieldvalue . "'";
112
        }
113 1
    }
114
115 1
    protected function getDescrizioneFiltroDate(string &$descrizionevalore): bool
116
    {
117 1
        $trovato = false;
118 1
        if ('date' == $this->fieldinfo['tipocampo']) {
119 1
            if (is_a($this->fieldvalue, "\DateTime")) {
120
                $descrizionevalore = $this->fieldvalue->format('d/m/Y');
121
            } else {
122 1
                $dateraw = DateTime::createFromFormat('Y-m-d', $this->fieldvalue);
123 1
                if ($dateraw === false) {
124
                    return false;
125
                }
126 1
                $descrizionevalore = $dateraw->format('d/m/Y');
127
            }
128 1
            $trovato = true;
129
        }
130 1
        if ('datetime' == $this->fieldinfo['tipocampo']) {
131 1
            if (is_a($this->fieldvalue, "\DateTime")) {
132
                $descrizionevalore = $this->fieldvalue->format('d/m/Y H:i:s');
133
            } else {
134 1
                $dateraw = DateTime::createFromFormat('Y-m-d', $this->fieldvalue);
135 1
                if ($dateraw === false) {
136
                    return false;
137
                }
138 1
                $descrizionevalore = $dateraw->format('d/m/Y');
139
            }
140 1
            $trovato = true;
141
        }
142
143 1
        return $trovato;
144
    }
145
146 1
    protected function getDescrizioneFiltroString(string &$descrizionevalore): bool
147
    {
148 1
        $trovato = false;
149 1
        if (is_string($this->fieldvalue)) {
150 1
            $descrizionevalore = $descrizionevalore = "'" . $this->fieldvalue . "'";
151 1
            $trovato = true;
152
        }
153
154 1
        return $trovato;
155
    }
156
157 1
    protected function getDescrizioneFiltroDecodifiche(string &$descrizionevalore): bool
158
    {
159 1
        $trovato = false;
160 1
        if (isset($this->fieldinfo['decodifiche'])) {
161
            $decodifiche = $this->fieldinfo['decodifiche'];
162
            if ($decodifiche) {
163
                if (is_array($this->fieldvalue)) {
164
                    foreach ($this->fieldvalue as $value) {
165
                        $descrizionevalore = $descrizionevalore . "'" . $decodifiche[$value] . "', ";
166
                    }
167
                } else {
168
                    if (isset($decodifiche[$this->fieldvalue])) {
169
                        $descrizionevalore = $descrizionevalore = "'" . $decodifiche[$this->fieldvalue] . "'";
170
                    } else {
171
                        $descrizionevalore = $descrizionevalore = "'" . $this->fieldvalue . "'";
172
                    }
173
                }
174
                $trovato = true;
175
            }
176
        }
177
178 1
        return $trovato;
179
    }
180
181 1
    protected function getDescrizioneFiltroIsNull(string &$descrizionevalore): bool
182
    {
183 1
        $trovato = false;
184 1
        if (is_null($this->fieldvalue)) {
185 1
            $descrizionevalore = '(vuoto)';
186 1
            $trovato = true;
187
        }
188
189 1
        return $trovato;
190
    }
191
192 1
    protected function getDescrizioneFiltroBoolean(string &$descrizionevalore): bool
193
    {
194 1
        $trovato = false;
195 1
        if (is_bool($this->fieldvalue)) {
196
            $descrizionevalore = $this->fieldvalue ? 'SI' : 'NO';
197
            $trovato = true;
198
        }
199
200 1
        return $trovato;
201
    }
202
203 1
    protected function getDescrizioneFiltroArray(string &$descrizionevalore): bool
204
    {
205 1
        $trovato = false;
206 1
        if (is_array($this->fieldvalue)) {
207 1
            foreach ($this->fieldvalue as $value) {
208 1
                if (is_numeric($value)) {
209 1
                    $descrizionevalore = $descrizionevalore . $value . ', ';
210
                } else {
211
                    $descrizionevalore = $descrizionevalore . "'" . $value . "', ";
212
                }
213 1
                $trovato = true;
214
            }
215 1
            $descrizionevalore = substr($descrizionevalore, 0, -2);
216
        }
217
218 1
        return $trovato;
219
    }
220
221 1
    protected function operatorToString(string $operator): string
222
    {
223
        $decoder = array(
224 1
            Comparison::LT => 'minore di',
225
            Comparison::LTE => 'minore o uguale di',
226
            Comparison::GT => 'maggiore di',
227
            Comparison::GTE => 'maggiore o uguale di',
228
            Comparison::CONTAINS => 'contiene',
229
            Comparison::STARTS_WITH => 'inizia con',
230
            Comparison::ENDS_WITH => 'finisce con',
231
            Comparison::IN => 'compreso tra',
232
            Comparison::NIN => 'non compreso tra',
233
            Comparison::EQ => 'uguale a',
234
            Comparison::NEQ => 'diverso da',
235
        );
236
237 1
        return $decoder[$operator];
238
    }
239
}
240