Completed
Push — master ( 10806f...3a0750 )
by Jean-Christophe
01:35
created

ValidationModelGenerator::parse()   B

Complexity

Conditions 6
Paths 9

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 8.9617
c 0
b 0
f 0
cc 6
nc 9
nop 0
1
<?php
2
3
namespace Ubiquity\contents\validation;
4
5
use Ubiquity\annotations\ValidatorAnnotation;
6
use Ubiquity\db\utils\DbTypes;
7
use Ubiquity\contents\validation\validators\HasNotNullInterface;
8
9
class ValidationModelGenerator {
10
	protected $type;
11
	protected $name;
12
	protected $notNull;
13
	protected $primary;
14
	
15
	public function __construct($type,$name,$notNull,$primary){
16
		$this->type=$type;
17
		$this->name=$name;
18
		$this->notNull=$notNull;
19
		$this->primary=$primary;
20
	}
21
	
22
	protected function parseType($type,$size){
23
		switch ($type){
24
			case "tinyint":
25
				if($size==1){
26
					return ValidatorAnnotation::initializeFromModel("isBool");
27
				}
28
				break;
29
			case "date":
30
				return ValidatorAnnotation::initializeFromModel("type","date");
31
			case "datetime":
32
				return ValidatorAnnotation::initializeFromModel("type","dateTime");
33
			case "time":
34
				return ValidatorAnnotation::initializeFromModel("type","time");
35
		}
36
		return null;
37
	}
38
	
39
	protected function parseSize($type,$size){
40
		if(isset($size)){
41
			if(DbTypes::isString($type)){
42
				return ValidatorAnnotation::initializeFromModel("length",null,["max"=>$size]);
43
			}
44
		}
45
		return null;
46
	}
47
	
48
	protected function parseNotNull(&$validatorAnnots){
49
		if($this->notNull){
50
			$notNullAffected=false;
51
			$size=sizeof($validatorAnnots);
52
			$i=0;
53
			while($i<$size && !$notNullAffected){
54
				$validatorAnnot=$validatorAnnots[$i];
55
				$validatorClass=ValidatorsManager::$validatorTypes[$validatorAnnot->type];
56
				if(is_subclass_of($validatorClass, HasNotNullInterface::class,true)){
1 ignored issue
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of might return inconsistent results on some PHP versions if \Ubiquity\contents\valid...NotNullInterface::class can be an interface. If so, you could instead use ReflectionClass::implementsInterface.
Loading history...
57
					$validatorAnnots[$i]->constraints["notNull"]=true;
58
					$notNullAffected=true;
59
				}
60
				$i++;
61
			}
62
			if(!$notNullAffected){
63
				$validatorAnnots[]=ValidatorAnnotation::initializeFromModel("notNull");
64
			}
65
		}
66
	}
67
	
68
	protected function parseName(){
69
		switch ($this->name){
70
			case "email": case "mail":
71
				return ValidatorAnnotation::initializeFromModel("email");
72
			case "url":
73
				return ValidatorAnnotation::initializeFromModel("url");
74
		}
75
		return null;
76
	}
77
	
78
	protected function scanType(&$type,&$size){
79
		$type=DbTypes::getType($this->type);
80
		$size=DbTypes::getSize($this->type);
81
	}
82
	
83
	public function parse(){
84
		if($this->primary && DbTypes::isInt($this->type)){
85
			return [ValidatorAnnotation::initializeFromModel("id", null,["autoinc"=>true])];
86
		}
87
		$validatorAnnot=$this->parseName();
88
		$this->scanType($type, $size);
89
		if(!isset($validatorAnnot)){
90
			$validatorAnnot=$this->parseType($type,$size);
91
		}
92
		
93
		$result=[];
94
		if(isset($validatorAnnot)){
95
			$result[]=$validatorAnnot;
96
		}
97
		$validatorAnnot= $this->parseSize($type,$size);
98
		if(isset($validatorAnnot)){
99
			$result[]=$validatorAnnot;
100
		}
101
		$this->parseNotNull($result);
102
		return $result;
103
	}
104
}
105
106