Ubiquity 2.5.2
php rapid development framework
Loading...
Searching...
No Matches
ValidationModelGenerator.php
Go to the documentation of this file.
1<?php
2
7
11
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
__construct($container, $annotsEngine, $type, $name, $notNull, $primary)
Manage Databases types.
Definition DbTypes.php:14
Ubiquity\annotations$AnnotationsInterface This class is part of Ubiquity.
Validation managment.