Ubiquity
2.5.2
php rapid development framework
Loading...
Searching...
No Matches
ValidationModelGenerator.php
Go to the documentation of this file.
1
<?php
2
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
;
21
protected
$annotsEngine
;
22
23
public
function
__construct
($container,
$annotsEngine
,
$type
,
$name
,
$notNull
,
$primary
) {
24
$this->modelContainer=$container;
25
$this->annotsEngine=
$annotsEngine
;
26
$this->type =
$type
;
27
$this->name =
$name
;
28
$this->notNull =
$notNull
;
29
$this->primary =
$primary
;
30
}
31
32
protected
function
parseType
(
$type
, $size) {
33
switch
(
$type
) {
34
case
'tinyint'
:
35
if
($size == 1) {
36
return
$this->
getValidatorAnnotFromModel
(
'isBool'
);
37
}
38
break
;
39
case
'boolean'
:
case
'bool'
:
40
return
$this->
getValidatorAnnotFromModel
(
'isBool'
);
41
case
'date'
:
42
return
$this->
getValidatorAnnotFromModel
(
'type'
,
'date'
);
43
case
'datetime'
:
44
return
$this->
getValidatorAnnotFromModel
(
'type'
,
'dateTime'
);
45
case
'time'
:
46
return
$this->
getValidatorAnnotFromModel
(
'type'
,
'time'
);
47
}
48
return
null
;
49
}
50
51
protected
function
getValidatorAnnotFromModel
(
$type
, $ref =
null
, $constraints = []){
52
if
(! is_array ( $constraints )) {
53
$constraints = [ ];
54
}
55
if
(isset ( $ref )) {
56
$constraints [
"ref"
] = $ref;
57
}
58
return
$this->annotsEngine->getAnnotation($this->modelContainer,
'validator'
,\compact(
'type'
,
'constraints'
));
59
}
60
61
protected
function
parseSize
(
$type
, $size) {
62
if
(isset ( $size )) {
63
if
(DbTypes::isString (
$type
)) {
64
return
$this->
getValidatorAnnotFromModel
(
'length'
,
null
, [
'max'
=> $size ] );
65
}
66
}
67
return
null
;
68
}
69
70
protected
function
parseNotNull
(&$validatorAnnots) {
71
if
($this->notNull) {
72
$notNullAffected =
false
;
73
$size = \count ( $validatorAnnots );
74
$i = 0;
75
while
( $i < $size && ! $notNullAffected ) {
76
$validatorAnnot = $validatorAnnots [$i];
77
$validatorClass =
ValidatorsManager::$validatorTypes
[$validatorAnnot->type];
78
if
(is_subclass_of ( $validatorClass, HasNotNullInterface::class,
true
)) {
79
$validatorAnnots [$i]->constraints [
"notNull"
] =
true
;
80
$notNullAffected =
true
;
81
}
82
$i ++;
83
}
84
if
(! $notNullAffected) {
85
$validatorAnnots [] = $this->
getValidatorAnnotFromModel
(
'notNull'
);
86
}
87
}
88
}
89
90
protected
function
parseName
() {
91
switch
($this->name) {
92
case
'email'
:
93
case
'mail'
:
94
return
$this->
getValidatorAnnotFromModel
(
'email'
);
95
case
'url'
:
96
return
$this->
getValidatorAnnotFromModel
(
'url'
);
97
}
98
return
null
;
99
}
100
101
protected
function
scanType
(&
$type
, &$size) {
102
$type
= DbTypes::getType ( $this->type );
103
$size = DbTypes::getSize ( $this->type );
104
}
105
106
public
function
parse
() {
107
if
($this->primary && DbTypes::isInt ( $this->type )) {
108
return
[ $this->
getValidatorAnnotFromModel
(
'id'
,
null
, [
'autoinc'
=>
true
] ) ];
109
}
110
$validatorAnnot = $this->
parseName
();
111
$this->
scanType
(
$type
, $size );
112
if
(! isset ( $validatorAnnot )) {
113
$validatorAnnot = $this->
parseType
(
$type
, $size );
114
}
115
116
$result = [ ];
117
if
(isset ( $validatorAnnot )) {
118
$result [] = $validatorAnnot;
119
}
120
$validatorAnnot = $this->
parseSize
(
$type
, $size );
121
if
(isset ( $validatorAnnot )) {
122
$result [] = $validatorAnnot;
123
}
124
$this->
parseNotNull
( $result );
125
return
$result;
126
}
127
}
128
Ubiquity\contents\validation\ValidationModelGenerator
Definition
ValidationModelGenerator.php:12
Ubiquity\contents\validation\ValidationModelGenerator\$primary
$primary
Definition
ValidationModelGenerator.php:16
Ubiquity\contents\validation\ValidationModelGenerator\parseName
parseName()
Definition
ValidationModelGenerator.php:90
Ubiquity\contents\validation\ValidationModelGenerator\$notNull
$notNull
Definition
ValidationModelGenerator.php:15
Ubiquity\contents\validation\ValidationModelGenerator\parse
parse()
Definition
ValidationModelGenerator.php:106
Ubiquity\contents\validation\ValidationModelGenerator\$annotsEngine
$annotsEngine
Definition
ValidationModelGenerator.php:21
Ubiquity\contents\validation\ValidationModelGenerator\$modelContainer
$modelContainer
Definition
ValidationModelGenerator.php:17
Ubiquity\contents\validation\ValidationModelGenerator\parseNotNull
parseNotNull(&$validatorAnnots)
Definition
ValidationModelGenerator.php:70
Ubiquity\contents\validation\ValidationModelGenerator\__construct
__construct($container, $annotsEngine, $type, $name, $notNull, $primary)
Definition
ValidationModelGenerator.php:23
Ubiquity\contents\validation\ValidationModelGenerator\parseSize
parseSize($type, $size)
Definition
ValidationModelGenerator.php:61
Ubiquity\contents\validation\ValidationModelGenerator\$type
$type
Definition
ValidationModelGenerator.php:13
Ubiquity\contents\validation\ValidationModelGenerator\$name
$name
Definition
ValidationModelGenerator.php:14
Ubiquity\contents\validation\ValidationModelGenerator\getValidatorAnnotFromModel
getValidatorAnnotFromModel($type, $ref=null, $constraints=[])
Definition
ValidationModelGenerator.php:51
Ubiquity\contents\validation\ValidationModelGenerator\scanType
scanType(&$type, &$size)
Definition
ValidationModelGenerator.php:101
Ubiquity\contents\validation\ValidationModelGenerator\parseType
parseType($type, $size)
Definition
ValidationModelGenerator.php:32
Ubiquity\contents\validation\ValidatorsManager\$validatorTypes
static $validatorTypes
Definition
ValidatorsManager.php:51
Ubiquity\db\utils\DbTypes
Manage Databases types.
Definition
DbTypes.php:14
Ubiquity\annotations\AnnotationsEngineInterface
Ubiquity\annotations$AnnotationsInterface This class is part of Ubiquity.
Definition
AnnotationsEngineInterface.php:12
Ubiquity\contents\validation\validators\HasNotNullInterface
Definition
HasNotNullInterface.php:5
Ubiquity\contents\validation
Validation managment.
C:
Users
myadd
Documents
GitHub
ubiquity-doc-api
vendor
phpmv
ubiquity
src
Ubiquity
contents
validation
ValidationModelGenerator.php
Generated on Sun Dec 10 2023 15:04:14 for
Ubiquity
by
doxygen
1.9.8