|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Validation managment |
|
5
|
|
|
*/ |
|
6
|
|
|
namespace Ubiquity\contents\validation; |
|
7
|
|
|
|
|
8
|
|
|
use Ubiquity\annotations\ValidatorAnnotation; |
|
9
|
|
|
use Ubiquity\db\utils\DbTypes; |
|
10
|
|
|
use Ubiquity\contents\validation\validators\HasNotNullInterface; |
|
11
|
|
|
|
|
12
|
|
|
class ValidationModelGenerator { |
|
13
|
|
|
protected $type; |
|
14
|
|
|
protected $name; |
|
15
|
|
|
protected $notNull; |
|
16
|
|
|
protected $primary; |
|
17
|
|
|
|
|
18
|
2 |
|
public function __construct($type, $name, $notNull, $primary) { |
|
19
|
2 |
|
$this->type = $type; |
|
20
|
2 |
|
$this->name = $name; |
|
21
|
2 |
|
$this->notNull = $notNull; |
|
22
|
2 |
|
$this->primary = $primary; |
|
23
|
2 |
|
} |
|
24
|
|
|
|
|
25
|
2 |
|
protected function parseType($type, $size) { |
|
26
|
2 |
|
switch ($type) { |
|
27
|
2 |
|
case "tinyint" : |
|
28
|
2 |
|
if ($size == 1) { |
|
29
|
2 |
|
return ValidatorAnnotation::initializeFromModel ( "isBool" ); |
|
30
|
|
|
} |
|
31
|
|
|
break; |
|
32
|
2 |
|
case "date" : |
|
33
|
|
|
return ValidatorAnnotation::initializeFromModel ( "type", "date" ); |
|
34
|
2 |
|
case "datetime" : |
|
35
|
2 |
|
return ValidatorAnnotation::initializeFromModel ( "type", "dateTime" ); |
|
36
|
2 |
|
case "time" : |
|
37
|
|
|
return ValidatorAnnotation::initializeFromModel ( "type", "time" ); |
|
38
|
|
|
} |
|
39
|
2 |
|
return null; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
2 |
|
protected function parseSize($type, $size) { |
|
43
|
2 |
|
if (isset ( $size )) { |
|
44
|
2 |
|
if (DbTypes::isString ( $type )) { |
|
45
|
2 |
|
return ValidatorAnnotation::initializeFromModel ( "length", null, [ "max" => $size ] ); |
|
46
|
|
|
} |
|
47
|
|
|
} |
|
48
|
2 |
|
return null; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
2 |
|
protected function parseNotNull(&$validatorAnnots) { |
|
52
|
2 |
|
if ($this->notNull) { |
|
53
|
2 |
|
$notNullAffected = false; |
|
54
|
2 |
|
$size = sizeof ( $validatorAnnots ); |
|
55
|
2 |
|
$i = 0; |
|
56
|
2 |
|
while ( $i < $size && ! $notNullAffected ) { |
|
57
|
2 |
|
$validatorAnnot = $validatorAnnots [$i]; |
|
58
|
2 |
|
$validatorClass = ValidatorsManager::$validatorTypes [$validatorAnnot->type]; |
|
59
|
2 |
|
if (is_subclass_of ( $validatorClass, HasNotNullInterface::class, true )) { |
|
60
|
2 |
|
$validatorAnnots [$i]->constraints ["notNull"] = true; |
|
61
|
2 |
|
$notNullAffected = true; |
|
62
|
|
|
} |
|
63
|
2 |
|
$i ++; |
|
64
|
|
|
} |
|
65
|
2 |
|
if (! $notNullAffected) { |
|
66
|
2 |
|
$validatorAnnots [] = ValidatorAnnotation::initializeFromModel ( "notNull" ); |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
2 |
|
} |
|
70
|
|
|
|
|
71
|
2 |
|
protected function parseName() { |
|
72
|
2 |
|
switch ($this->name) { |
|
73
|
2 |
|
case "email" : |
|
74
|
2 |
|
case "mail" : |
|
75
|
2 |
|
return ValidatorAnnotation::initializeFromModel ( "email" ); |
|
76
|
2 |
|
case "url" : |
|
77
|
2 |
|
return ValidatorAnnotation::initializeFromModel ( "url" ); |
|
78
|
|
|
} |
|
79
|
2 |
|
return null; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
2 |
|
protected function scanType(&$type, &$size) { |
|
83
|
2 |
|
$type = DbTypes::getType ( $this->type ); |
|
84
|
2 |
|
$size = DbTypes::getSize ( $this->type ); |
|
85
|
2 |
|
} |
|
86
|
|
|
|
|
87
|
2 |
|
public function parse() { |
|
88
|
2 |
|
if ($this->primary && DbTypes::isInt ( $this->type )) { |
|
89
|
2 |
|
return [ ValidatorAnnotation::initializeFromModel ( "id", null, [ "autoinc" => true ] ) ]; |
|
90
|
|
|
} |
|
91
|
2 |
|
$validatorAnnot = $this->parseName (); |
|
92
|
2 |
|
$this->scanType ( $type, $size ); |
|
93
|
2 |
|
if (! isset ( $validatorAnnot )) { |
|
94
|
2 |
|
$validatorAnnot = $this->parseType ( $type, $size ); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
2 |
|
$result = [ ]; |
|
98
|
2 |
|
if (isset ( $validatorAnnot )) { |
|
99
|
2 |
|
$result [] = $validatorAnnot; |
|
100
|
|
|
} |
|
101
|
2 |
|
$validatorAnnot = $this->parseSize ( $type, $size ); |
|
102
|
2 |
|
if (isset ( $validatorAnnot )) { |
|
103
|
2 |
|
$result [] = $validatorAnnot; |
|
104
|
|
|
} |
|
105
|
2 |
|
$this->parseNotNull ( $result ); |
|
106
|
2 |
|
return $result; |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
|