Completed
Push — master ( fcf532...996d51 )
by Jean-Christophe
05:34
created

ValidationModelGenerator   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Test Coverage

Coverage 95.38%

Importance

Changes 0
Metric Value
wmc 27
eloc 60
dl 0
loc 94
ccs 62
cts 65
cp 0.9538
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A parse() 0 20 6
A parseName() 0 8 4
A parseType() 0 15 6
A parseSize() 0 7 3
A parseNotNull() 0 16 6
A __construct() 0 5 1
A scanType() 0 3 1
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 1
	public function __construct($type,$name,$notNull,$primary){
16 1
		$this->type=$type;
17 1
		$this->name=$name;
18 1
		$this->notNull=$notNull;
19 1
		$this->primary=$primary;
20 1
	}
21
	
22 1
	protected function parseType($type,$size){
23 1
		switch ($type){
24 1
			case "tinyint":
25 1
				if($size==1){
26 1
					return ValidatorAnnotation::initializeFromModel("isBool");
27
				}
28
				break;
29 1
			case "date":
30
				return ValidatorAnnotation::initializeFromModel("type","date");
31 1
			case "datetime":
32 1
				return ValidatorAnnotation::initializeFromModel("type","dateTime");
33 1
			case "time":
34
				return ValidatorAnnotation::initializeFromModel("type","time");
35
		}
36 1
		return null;
37
	}
38
	
39 1
	protected function parseSize($type,$size){
40 1
		if(isset($size)){
41 1
			if(DbTypes::isString($type)){
42 1
				return ValidatorAnnotation::initializeFromModel("length",null,["max"=>$size]);
43
			}
44
		}
45 1
		return null;
46
	}
47
	
48 1
	protected function parseNotNull(&$validatorAnnots){
49 1
		if($this->notNull){
50 1
			$notNullAffected=false;
51 1
			$size=sizeof($validatorAnnots);
52 1
			$i=0;
53 1
			while($i<$size && !$notNullAffected){
54 1
				$validatorAnnot=$validatorAnnots[$i];
55 1
				$validatorClass=ValidatorsManager::$validatorTypes[$validatorAnnot->type];
56 1
				if(is_subclass_of($validatorClass, HasNotNullInterface::class,true)){
57 1
					$validatorAnnots[$i]->constraints["notNull"]=true;
58 1
					$notNullAffected=true;
59
				}
60 1
				$i++;
61
			}
62 1
			if(!$notNullAffected){
63 1
				$validatorAnnots[]=ValidatorAnnotation::initializeFromModel("notNull");
64
			}
65
		}
66 1
	}
67
	
68 1
	protected function parseName(){
69 1
		switch ($this->name){
70 1
			case "email": case "mail":
71 1
				return ValidatorAnnotation::initializeFromModel("email");
72 1
			case "url":
73 1
				return ValidatorAnnotation::initializeFromModel("url");
74
		}
75 1
		return null;
76
	}
77
	
78 1
	protected function scanType(&$type,&$size){
79 1
		$type=DbTypes::getType($this->type);
80 1
		$size=DbTypes::getSize($this->type);
81 1
	}
82
	
83 1
	public function parse(){
84 1
		if($this->primary && DbTypes::isInt($this->type)){
85 1
			return [ValidatorAnnotation::initializeFromModel("id", null,["autoinc"=>true])];
86
		}
87 1
		$validatorAnnot=$this->parseName();
88 1
		$this->scanType($type, $size);
89 1
		if(!isset($validatorAnnot)){
90 1
			$validatorAnnot=$this->parseType($type,$size);
91
		}
92
		
93 1
		$result=[];
94 1
		if(isset($validatorAnnot)){
95 1
			$result[]=$validatorAnnot;
96
		}
97 1
		$validatorAnnot= $this->parseSize($type,$size);
98 1
		if(isset($validatorAnnot)){
99 1
			$result[]=$validatorAnnot;
100
		}
101 1
		$this->parseNotNull($result);
102 1
		return $result;
103
	}
104
}
105
106