|
1
|
|
|
<?php |
|
2
|
|
|
namespace Ajir\RabbitMqSqlBundle\DataValidator; |
|
3
|
|
|
|
|
4
|
|
|
use DateTime; |
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* Class DataValidator |
|
8
|
|
|
* |
|
9
|
|
|
* @author Florian Ajir <[email protected]> |
|
10
|
|
|
*/ |
|
11
|
|
|
class DataValidator implements DataValidatorInterface |
|
12
|
|
|
{ |
|
13
|
|
|
const DATA_TYPE_STRING = 'string'; |
|
14
|
|
|
const DATA_TYPE_INTEGER = 'int'; |
|
15
|
|
|
const DATA_TYPE_DECIMAL = 'decimal'; |
|
16
|
|
|
const DATA_TYPE_DATE = 'date'; |
|
17
|
|
|
const DATA_TYPE_DATETIME = 'datetime'; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Check if value exceed max length defined in mapping |
|
21
|
|
|
* |
|
22
|
|
|
* @param string $value |
|
23
|
|
|
* @param int $maxlength |
|
24
|
|
|
* |
|
25
|
|
|
* @return bool |
|
26
|
|
|
*/ |
|
27
|
6 |
|
public function validateLength($value, $maxlength) |
|
28
|
|
|
{ |
|
29
|
6 |
|
return strlen($value) <= $maxlength; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Check if property type correspond to which declared in mapping |
|
34
|
|
|
* |
|
35
|
|
|
* @param mixed $value |
|
36
|
|
|
* @param string $type |
|
37
|
|
|
* |
|
38
|
|
|
* @return bool true if valid value type or null value |
|
39
|
|
|
*/ |
|
40
|
7 |
|
public function validateType($value, $type) |
|
41
|
|
|
{ |
|
42
|
7 |
|
if (!is_null($value)) { |
|
43
|
|
|
switch ($type) { |
|
44
|
6 |
|
case self::DATA_TYPE_INTEGER: |
|
45
|
3 |
|
return $this->isInteger($value); |
|
46
|
4 |
|
case self::DATA_TYPE_DECIMAL: |
|
47
|
3 |
|
return $this->isFloat($value); |
|
48
|
2 |
|
case self::DATA_TYPE_DATETIME: |
|
49
|
1 |
|
return $this->validateDate($value, DateTime::ISO8601); |
|
50
|
2 |
|
case self::DATA_TYPE_DATE: |
|
51
|
1 |
|
return $this->validateDate($value, 'Y-m-d'); |
|
52
|
2 |
|
case self::DATA_TYPE_STRING: |
|
53
|
2 |
|
return is_string($value); |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
1 |
|
return true; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @param mixed $input |
|
62
|
|
|
* |
|
63
|
|
|
* @return bool |
|
64
|
|
|
*/ |
|
65
|
3 |
|
public function isInteger($input) |
|
66
|
|
|
{ |
|
67
|
3 |
|
return (ctype_digit(strval($input))); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* test if parameter is containing a float |
|
72
|
|
|
* |
|
73
|
|
|
* @param mixed $value |
|
74
|
|
|
* |
|
75
|
|
|
* @return bool |
|
76
|
|
|
*/ |
|
77
|
3 |
|
public function isFloat($value) |
|
78
|
|
|
{ |
|
79
|
3 |
|
return ($value == (string)(float)$value); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Check the validity of a date or datetime format |
|
84
|
|
|
* |
|
85
|
|
|
* @param string $date |
|
86
|
|
|
* @param string $format |
|
87
|
|
|
* |
|
88
|
|
|
* @return bool |
|
89
|
|
|
*/ |
|
90
|
5 |
|
public function validateDate($date, $format) |
|
91
|
|
|
{ |
|
92
|
5 |
|
$datetime = DateTime::createFromFormat($format, $date); |
|
93
|
|
|
|
|
94
|
5 |
|
return $datetime && $datetime->format($format) == $date; |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|