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