Completed
Push — master ( bfe5cd...459442 )
by Lorenzo
03:32
created

ValidateHelper::checkNumericField()   B

Complexity

Conditions 8
Paths 8

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 8

Importance

Changes 1
Bugs 1 Features 1
Metric Value
c 1
b 1
f 1
dl 0
loc 19
ccs 12
cts 12
cp 1
rs 7.7778
cc 8
eloc 12
nc 8
nop 3
crap 8
1
<?php
2
namespace Padosoft\TesseraSanitaria;
3
4
/**
5
 * Class ValidateHelper
6
 * @package Padosoft\TesseraSanitaria
7
 */
8
class ValidateHelper
9
{
10
	use traits\Errorable;
11
12
    private $objPartitaIVA = null;
13
    private $objCFChecker = null;
14
15
	/**+
16
	 * ValidateHelper constructor.
17
	 *
18
	 * @param \fdisotto\PartitaIVA   $objPartitaIVA
19
	 * @param \CodiceFiscale\Checker $objCFChecker
20
	 */
21 54
	public function __construct(\fdisotto\PartitaIVA $objPartitaIVA, \CodiceFiscale\Checker $objCFChecker)
22
	{
23 54
		$this->arrErrors = array();
24 54
		$this->objPartitaIVA = $objPartitaIVA;
25 54
		$this->objCFChecker = $objCFChecker;
26 54
	}
27
28
	/**
29
	 * @param $codiceRegione
30
	 */
31 3
	public function checkCodiceRegione($codiceRegione)
32
	{
33 3
        if (!CodiceRegione::isValidValue($codiceRegione)) {
34 3
            $this->addError("<b>".$codiceRegione."</b> - Codice regione (codiceRegione) non valido. Codici validi: ".CodiceRegione::getCostantsValues());
35 3
        }
36 3
	}
37
38
	/**
39
	 * @param $codiceSSA
40
	 */
41 3
	public function checkCodiceSSA($codiceSSA)
42
	{
43 3
        if (!CodiceSSA::isValidValue($codiceSSA)) {
44 3
            $this->addError("<b>".$codiceSSA."</b> - Codice SSA (codiceSSA) non valido. Codici validi: ".CodiceSSA::getCostantsValues());
45 3
        }
46 3
	}
47
48
	/**
49
	 * @param $dateStr
50
	 *
51
	 * @return bool
52
	 */
53 9
	public function isoDateValidate($dateStr)
54
	{
55 9
		if (preg_match('/^([\+-]?\d{4}(?!\d{2}\b))((-?)((0[1-9]|1[0-2])(\3([12]\d|0[1-9]|3[01]))?|W([0-4]\d|5[0-2])(-?[1-7])?|(00[1-9]|0[1-9]\d|[12]\d{2}|3([0-5]\d|6[1-6])))([T\s]((([01]\d|2[0-3])((:?)[0-5]\d)?|24\:?00)([\.,]\d+(?!:))?)?(\17[0-5]\d([\.,]\d+)?)?([zZ]|([\+-])([01]\d|2[0-3]):?([0-5]\d)?)?)?)?$/', $dateStr) > 0){
56 9
			return TRUE;
57
		}else{
58 9
			return FALSE;
59
		}
60
	}
61
62
	/**+
63
	 * @param $arrVociSpesa
64
	 */
65
	public function checkArrVociSpesa($arrVociSpesa)
66
	{
67
	    if(empty($arrVociSpesa)){
68
69
			$this->addError("Voci spesa mancanti");
70
		}else{
71
72
			foreach ($arrVociSpesa as $rigaVociSpesa){
73
				foreach ($rigaVociSpesa as $colonnaVociSpesa){
74
					foreach($colonnaVociSpesa as $campo=>$valore){
75
76
                        if ($campo == "tipoSpesa"){
77
78
                            $this->checkTipoSpesa($valore);
79
                        }elseif ($campo == "importo") {
80
81
                            $this->checkImporto($valore);
82
                        }
83
                    }
84
				}
85
			}
86
		}
87
	}
88
89
	/**
90
	 * @param $arrSpesa
91
	 */
92
	public function checkArrSpesa($arrSpesa)
93
	{
94
		if(empty($arrSpesa)){
95
			$this->addError("Dati spesa mancanti");
96
		}else{
97
98
			// Controllo interno array spesa
99
			foreach($arrSpesa as $rigaSpesa){
100
101
                $this->checkRigaSpesa($rigaSpesa);
102
			}
103
		}
104
	}
105
106
	/**
107
	 * @param $pIva
108
	 */
109 3
	public function checkPIva($pIva)
110
	{
111 3
		if(empty($pIva)){
112 3
			$this->addError("Partita IVA mancante");
113 3
		}else{
114
			// Verifica formale della partita IVA
115 3
			if(!$this->objPartitaIVA->check($pIva)){
116 3
				$this->addError("<b>".$pIva."</b> - Partita IVA formalmente non corretta");
117 3
			}
118
		}
119 3
	}
120
121
	/**
122
	 * @param $cfProprietario
123
	 */
124 3
	public function checkCfProprietario($cfProprietario)
125
	{
126 3
		if(empty($cfProprietario)){
127 3
			$this->addError("Codice fiscale proprietario (cfProprietario) mancante");
128 3
		}else{
129
			// Verifica formale del codice fiscale
130 3
			if (!$this->objCFChecker->isFormallyCorrect($cfProprietario)){
131 3
				$this->addError("<b>".$cfProprietario."</b> - Codice fiscale proprietario (cfProprietario) formalmente non corretto");
132 3
			}
133
		}
134 3
	}
135
136
	/**
137
	 * @param $codiceAsl
138
	 */
139 3
	public function checkCodiceAsl($codiceAsl)
140
	{
141 3
        if (!CodiceAsl::isValidValue($codiceAsl)) {
142 3
            $this->addError("<b>".$codiceAsl."</b> - Codice ASL (codiceAsl) non valido. Codici validi: ".CodiceAsl::getCostantsValues());
143 3
        }
144 3
	}
145
146
    /**
147
     * @param $campo
148
     * @param $valore
149
     */
150 6
    private function checkDataValida($campo, $valore)
151
    {
152 6
        if (!$this->isoDateValidate($valore)) {
153 6
            $this->addError("<b>" . $valore . "</b> - $campo non valida. La data deve essere nel formato ISO Es.: 2015-08-01");
154 6
        }
155
156 6
        if ($valore < "2015-01-01") {
157 6
            $this->addError("<b>" . $valore . "</b> - $campo deve essere successiva al 01/01/2015");
158 6
        }
159 6
    }
160
161
    /**
162
     * @param $valore
163
     */
164 3
    public function checkTipoSpesa($valore)
165
    {
166 3
        if (!TipoSpesa::isValidValue($valore)) {
167 3
            $this->addError("<b>" . $valore . "</b> - Codice tipo spesa (tipoSpesa) non valido. Codici validi: " . TipoSpesa::getCostantsValues());
168 3
        }
169 3
    }
170
171
    /**
172
     * @param $valore
173
     */
174 3
    private function checkImporto($valore)
175
    {
176 3
        if (!is_numeric($valore)) {
177 3
            $this->addError("<b>" . $valore . "</b> - Importo (importo) non numerico");
178 3
        }
179 3
    }
180
181
    /**
182
     * @param $valore
183
     * @param $campo
184
     */
185 3
    private function checkRequiredField($valore, $campo)
186
    {
187 3
        if ($valore == "") {
188 3
            $this->addError("Dato spesa mancante campo: " . $campo);
189 3
        }
190 3
    }
191
192
    /**
193
     * @param $campo
194
     * @param $valore
195
     */
196 3
    private function checkDataEmissione($campo, $valore)
197
    {
198 3
        $this->checkDataValida($campo, $valore);
199 3
    }
200
201
    /**
202
     * @param $valore
203
     */
204 3
    public function checkFlagOperazione($valore)
205
    {
206 3
        if (!FlagOperazione::isValidValue($valore)) {
207 3
            $this->addError("<b>" . $valore . "</b> - Flag Operazione (flagOperazione) non valido. Codici validi: " . FlagOperazione::getCostantsValues());
208 3
        }
209 3
    }
210
211
    /**
212
     * @param $valore
213
     */
214 3
    private function checkCfCittadino($valore)
215
    {
216 3
        if (!$this->objCFChecker->isFormallyCorrect($valore)) {
217 3
            $this->addError("<b>" . $valore . "</b> - Codice fiscale (cfCittadino) cittadino non valido");
218 3
        }
219 3
    }
220
221
    /**
222
     * @param $valore
223
     */
224 3
    public function checkDispositivo($valore)
225
    {
226 3
        if (!$this->checkNumericField($valore, 3, true)) {
227 3
            $this->addError("<b>" . $valore . "</b> - Codice dispositivo (dispositivo) non valido: deve essere numerico, al massimo di 3 cifre");
228 3
        }
229 3
    }
230
231
    /**
232
     * @param $valore
233
     */
234 3
    public function checkNumDocumento($valore)
235
    {
236 3
        if (!$this->checkNumericField($valore, 20)) {
237 3
            $this->addError("<b>" . $valore . "</b> - Numero documento (numDocumento) non valido: deve essere numerico, al massimo di 20 cifre");
238 3
        }
239 3
    }
240
241
    /**
242
     * @param $rigaSpesa
243
     */
244
    private function checkRigaSpesa($rigaSpesa)
245
    {
246
        if(count($rigaSpesa)<6){
247
            $this->addError("Dati spesa incompleti");
248
        }
249
250
        foreach($rigaSpesa as $campo => $valore) {
251
252
            if ($campo != "flagPagamentoAnticipato") { // flagPagamentoAnticipato e' facoltativo
253
254
                $this->checkRequiredField($valore, $campo);
255
            }
256
257
            if ($campo == "dataEmissione" || $campo == "dataPagamento") {
258
259
                $this->checkDataEmissione($campo, $valore);
260
            }elseif ($campo == "flagOperazione") {
261
262
                $this->checkFlagOperazione($valore);
263
            }elseif ($campo == "cfCittadino") {
264
265
                $this->checkCfCittadino($valore);
266
            }elseif ($campo == "dispositivo") {
267
268
                $this->checkDispositivo($valore);
269
            }elseif ($campo == "numDocumento") {
270
271
                $this->checkNumDocumento($valore);
272
            }
273
        }
274
    }
275
276
    /**
277
     * @param            $valore
278
     * @param int        $maxLen
279
     * @param bool|false $zeroFilled
280
     *
281
     * @return bool
282
     */
283 9
    public function checkNumericField($valore, $maxLen=0, $zeroFilled=false)
284
    {
285 9
        if($zeroFilled && $valore!=''){
286 3
            $valore = ltrim(trim($valore), '0');
287 3
        }
288 9
        if(!is_numeric($valore)){
289 9
            return false;
290
        }
291 9
        if(strlen($valore)>1 && substr($valore, 0,2)=='00'){ //'00123' passed is_numeric()!
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
292 3
            return false;
293
        }
294
        //$valore = (int)$valore;
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
295 9
        if(is_numeric($maxLen) && $maxLen>0){
296 9
            $maxNumber = pow(10, $maxLen);
297 9
            return !( $valore>=$maxNumber );
298
        }else{
299 3
            return true;
300
        }
301
    }
302
}
303