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
|
2 |
|
/** |
19
|
2 |
|
* @var AnnotationsEngineInterface |
20
|
2 |
|
*/ |
21
|
2 |
|
protected $annotsEngine; |
22
|
2 |
|
|
23
|
2 |
|
public function __construct($container,$annotsEngine,$type, $name, $notNull, $primary) { |
24
|
|
|
$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
|
|
|
} |
31
|
|
|
|
32
|
2 |
|
protected function parseType($type, $size) { |
33
|
|
|
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
|
|
|
case 'datetime' : |
42
|
2 |
|
return $this->getValidatorAnnotFromModel ( 'type', 'dateTime' ); |
43
|
2 |
|
case 'time' : |
44
|
2 |
|
return $this->getValidatorAnnotFromModel ( 'type', 'time' ); |
45
|
2 |
|
} |
46
|
|
|
return null; |
47
|
|
|
} |
48
|
2 |
|
|
49
|
|
|
protected function getValidatorAnnotFromModel($type, $ref = null, $constraints = []){ |
50
|
|
|
if (! is_array ( $constraints )) { |
51
|
2 |
|
$constraints = [ ]; |
52
|
2 |
|
} |
53
|
2 |
|
if (isset ( $ref )) { |
54
|
2 |
|
$constraints ["ref"] = $ref; |
55
|
2 |
|
} |
56
|
2 |
|
return $this->annotsEngine->getAnnotation($this->modelContainer,'validator',\compact('type','constraints')); |
57
|
2 |
|
} |
58
|
2 |
|
|
59
|
2 |
|
protected function parseSize($type, $size) { |
60
|
2 |
|
if (isset ( $size )) { |
61
|
2 |
|
if (DbTypes::isString ( $type )) { |
62
|
|
|
return $this->getValidatorAnnotFromModel ( 'length', null, [ 'max' => $size ] ); |
63
|
2 |
|
} |
64
|
|
|
} |
65
|
2 |
|
return null; |
66
|
2 |
|
} |
67
|
|
|
|
68
|
|
|
protected function parseNotNull(&$validatorAnnots) { |
69
|
2 |
|
if ($this->notNull) { |
70
|
|
|
$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
|
|
|
$notNullAffected = true; |
79
|
2 |
|
} |
80
|
|
|
$i ++; |
81
|
|
|
} |
82
|
2 |
|
if (! $notNullAffected) { |
83
|
2 |
|
$validatorAnnots [] = $this->getValidatorAnnotFromModel ( 'notNull' ); |
84
|
2 |
|
} |
85
|
2 |
|
} |
86
|
|
|
} |
87
|
2 |
|
|
88
|
2 |
|
protected function parseName() { |
89
|
2 |
|
switch ($this->name) { |
90
|
|
|
case 'email' : |
91
|
2 |
|
case 'mail' : |
92
|
2 |
|
return $this->getValidatorAnnotFromModel ( 'email' ); |
93
|
2 |
|
case 'url' : |
94
|
2 |
|
return $this->getValidatorAnnotFromModel ( 'url' ); |
95
|
|
|
} |
96
|
|
|
return null; |
97
|
2 |
|
} |
98
|
2 |
|
|
99
|
2 |
|
protected function scanType(&$type, &$size) { |
100
|
|
|
$type = DbTypes::getType ( $this->type ); |
101
|
2 |
|
$size = DbTypes::getSize ( $this->type ); |
102
|
2 |
|
} |
103
|
2 |
|
|
104
|
|
|
public function parse() { |
105
|
2 |
|
if ($this->primary && DbTypes::isInt ( $this->type )) { |
106
|
2 |
|
return [ $this->getValidatorAnnotFromModel ( 'id', null, [ 'autoinc' => true ] ) ]; |
107
|
|
|
} |
108
|
|
|
$validatorAnnot = $this->parseName (); |
109
|
|
|
$this->scanType ( $type, $size ); |
110
|
|
|
if (! isset ( $validatorAnnot )) { |
111
|
|
|
$validatorAnnot = $this->parseType ( $type, $size ); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
$result = [ ]; |
115
|
|
|
if (isset ( $validatorAnnot )) { |
116
|
|
|
$result [] = $validatorAnnot; |
117
|
|
|
} |
118
|
|
|
$validatorAnnot = $this->parseSize ( $type, $size ); |
119
|
|
|
if (isset ( $validatorAnnot )) { |
120
|
|
|
$result [] = $validatorAnnot; |
121
|
|
|
} |
122
|
|
|
$this->parseNotNull ( $result ); |
123
|
|
|
return $result; |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
|