Passed
Push — develop ( b344b2...f2254c )
by Andrea
62:59 queued 16s
created

getDescrizioneFiltroDate()   B

Complexity

Conditions 7
Paths 13

Size

Total Lines 29
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 7.0957

Importance

Changes 0
Metric Value
cc 7
eloc 20
nc 13
nop 1
dl 0
loc 29
ccs 14
cts 16
cp 0.875
crap 7.0957
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 2
     * @param mixed $fieldvalue
44
     * @param string $fieldqueryparameter
45 2
     * @param array<mixed> $fieldinfo
46 2
     */
47 2
    public function __construct(string $fieldname, string $fieldoperator, $fieldvalue, string $fieldqueryparameter, $fieldinfo)
48 2
    {
49
        $this->fieldname = $fieldname;
50 2
        $this->fieldoperator = $fieldoperator;
51
        if (is_string($fieldvalue)) {
52 2
            $this->fieldvalue = urldecode($fieldvalue);
53 2
        } else {
54 2
            $this->fieldvalue = $fieldvalue;
55
        }
56 2
        $this->fieldqueryparameter = $fieldqueryparameter;
57 2
        $this->fieldinfo = $fieldinfo;
58
        $this->parameters = array();
59
        /** @phpstan-ignore-next-line */
60
        $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
    }
62
63 2
    /**
64
     *
65 2
     * @return string|null
66
     */
67
    public function getQueryCriteria()
68
    {
69
        return $this->criteria;
70
    }
71
72 2
    /**
73
     *
74 2
     * @return array<mixed>
75
     */
76
    public function getQueryParameters(): array
77 1
    {
78
        return $this->parameters;
79 1
    }
80
81
    public function getDescrizioneFiltro(): string
82 1
    {
83 1
        $descrizionevalore = '';
84 1
85
        switch (true) {
86 1
            case $this->getDescrizioneFiltroIsNull($descrizionevalore):
87
                break;
88 1
            case $this->getDescrizioneFiltroDecodifiche($descrizionevalore):
89 1
                break;
90 1
            case $this->getDescrizioneFiltroBoolean($descrizionevalore):
91 1
                break;
92 1
            case $this->getDescrizioneFiltroDate($descrizionevalore):
93 1
                break;
94
            case $this->getDescrizioneFiltroArray($descrizionevalore):
95 1
                break;
96 1
            case $this->getDescrizioneFiltroString($descrizionevalore):
97
                break;
98 1
            default:
99 1
                $this->getDescrizioneFiltroAltro($descrizionevalore);
100
                break;
101 1
        }
102
        $nomecampo = substr($this->fieldname, stripos($this->fieldname, '.') + 1);
103
        $filtro = $nomecampo . ' ' . $this->operatorToString($this->fieldoperator) . ' ' . $descrizionevalore;
104 1
105
        return $filtro;
106 1
    }
107 1
108
    protected function getDescrizioneFiltroAltro(string &$descrizionevalore): void
109 1
    {
110
        if ('' == $descrizionevalore) {
111 1
            $descrizionevalore = "'" . $this->fieldvalue . "'";
112
        }
113 1
    }
114 1
115 1
    protected function getDescrizioneFiltroDate(string &$descrizionevalore): bool
116
    {
117
        $trovato = false;
118 1
        if ('date' == $this->fieldinfo['tipocampo']) {
119
            if (is_a($this->fieldvalue, "\DateTime")) {
120 1
                $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
        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
                if ($dateraw === false) {
136 1
                    return false;
137 1
                }
138 1
                $descrizionevalore = $dateraw->format('d/m/Y');
139 1
            }
140
            $trovato = true;
141
        }
142 1
143
        return $trovato;
144
    }
145 1
146
    protected function getDescrizioneFiltroString(string &$descrizionevalore): bool
147 1
    {
148 1
        $trovato = false;
149
        if (is_string($this->fieldvalue)) {
150
            $descrizionevalore = $descrizionevalore = "'" . $this->fieldvalue . "'";
151
            $trovato = true;
152
        }
153
154
        return $trovato;
155
    }
156
157
    protected function getDescrizioneFiltroDecodifiche(string &$descrizionevalore): bool
158
    {
159
        $trovato = false;
160
        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 1
                    }
167
                } else {
168
                    if (isset($decodifiche[$this->fieldvalue])) {
169 1
                        $descrizionevalore = $descrizionevalore = "'" . $decodifiche[$this->fieldvalue] . "'";
170
                    } else {
171 1
                        $descrizionevalore = $descrizionevalore = "'" . $this->fieldvalue . "'";
172 1
                    }
173 1
                }
174 1
                $trovato = true;
175
            }
176
        }
177 1
178
        return $trovato;
179
    }
180 1
181
    protected function getDescrizioneFiltroIsNull(string &$descrizionevalore): bool
182 1
    {
183 1
        $trovato = false;
184
        if (is_null($this->fieldvalue)) {
185
            $descrizionevalore = '(vuoto)';
186
            $trovato = true;
187
        }
188 1
189
        return $trovato;
190
    }
191 1
192
    protected function getDescrizioneFiltroBoolean(string &$descrizionevalore): bool
193 1
    {
194 1
        $trovato = false;
195 1
        if (is_bool($this->fieldvalue)) {
196 1
            $descrizionevalore = $this->fieldvalue ? 'SI' : 'NO';
197 1
            $trovato = true;
198
        }
199
200
        return $trovato;
201 1
    }
202
203 1
    protected function getDescrizioneFiltroArray(string &$descrizionevalore): bool
204
    {
205
        $trovato = false;
206 1
        if (is_array($this->fieldvalue)) {
207
            foreach ($this->fieldvalue as $value) {
208
                if (is_numeric($value)) {
209 1
                    $descrizionevalore = $descrizionevalore . $value . ', ';
210
                } else {
211
                    $descrizionevalore = $descrizionevalore . "'" . $value . "', ";
212 1
                }
213
                $trovato = true;
214
            }
215
            $descrizionevalore = substr($descrizionevalore, 0, -2);
216
        }
217
218
        return $trovato;
219
    }
220
221
    protected function operatorToString(string $operator): string
222
    {
223
        $decoder = array(
224
            Comparison::LT => 'minore di',
225 1
            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
        return $decoder[$operator];
238
    }
239
}
240