Completed
Push — master ( 782061...10806f )
by Jean-Christophe
01:37
created

Member::getGetter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Ubiquity\orm\creator;
4
5
use Ubiquity\annotations\IdAnnotation;
6
use Ubiquity\annotations\ManyToOneAnnotation;
7
use Ubiquity\annotations\OneToManyAnnotation;
8
use Ubiquity\annotations\ManyToManyAnnotation;
9
use Ubiquity\annotations\JoinTableAnnotation;
10
use Ubiquity\annotations\JoinColumnAnnotation;
11
use Ubiquity\annotations\ColumnAnnotation;
12
use Ubiquity\annotations\ValidatorAnnotation;
13
14
class Member {
15
	private $name;
16
	private $primary;
17
	private $manyToOne;
18
	private $annotations;
19
20
	public function __construct($name) {
21
		$this->name=$name;
22
		$this->annotations=array ();
23
		$this->primary=false;
24
		$this->manyToOne=false;
25
	}
26
27
	public function __toString() {
28
		$annotationsStr="";
29
		if (sizeof($this->annotations) > 0) {
30
			$annotationsStr="\n\t/**";
31
			$annotations=$this->annotations;
32
			\array_walk($annotations, function ($item) {
33
				return $item . "";
34
			});
35
			if (\sizeof($annotations) > 1) {
36
				$annotationsStr.="\n\t * " . implode("\n\t * ", $annotations);
37
			} else {
38
				$annotationsStr.="\n\t * " . \end($annotations);
39
			}
40
			$annotationsStr.="\n\t**/";
41
		}
42
		return $annotationsStr . "\n\tprivate $" . $this->name . ";\n";
43
	}
44
45
	public function setPrimary() {
46
		if ($this->primary === false) {
47
			$this->annotations[]=new IdAnnotation();
48
			$this->primary=true;
49
		}
50
	}
51
52
	public function setDbType($infos){
53
		$annot=new ColumnAnnotation();
54
		$annot->name=$this->name;
55
		$annot->dbType=$infos["Type"];
56
		$annot->nullable=(\strtolower($infos["Nullable"])==="yes");
57
		$this->annotations["column"]=$annot;
58
	}
59
60
	public function addManyToOne($name, $className, $nullable=false) {
61
		$this->annotations[]=new ManyToOneAnnotation();
62
		$joinColumn=new JoinColumnAnnotation();
63
		$joinColumn->name=$name;
64
		$joinColumn->className=$className;
65
		$joinColumn->nullable=$nullable;
66
		$this->annotations[]=$joinColumn;
67
		$this->manyToOne=true;
68
	}
69
70
	public function addOneToMany($mappedBy, $className) {
71
		$oneToMany=new OneToManyAnnotation();
72
		$oneToMany->mappedBy=$mappedBy;
73
		$oneToMany->className=$className;
74
		$this->annotations[]=$oneToMany;
75
	}
76
77
	public function addManyToMany($targetEntity, $inversedBy, $joinTable, $joinColumns=[], $inverseJoinColumns=[]) {
78
		$manyToMany=new ManyToManyAnnotation();
79
		$manyToMany->targetEntity=$targetEntity;
80
		$manyToMany->inversedBy=$inversedBy;
81
		$jt=new JoinTableAnnotation();
82
		$jt->name=$joinTable;
83
		if (\sizeof($joinColumns) == 2) {
84
			$jt->joinColumns=$joinColumns;
85
		}
86
		if (\sizeof($inverseJoinColumns) == 2) {
87
			$jt->inverseJoinColumns=$inverseJoinColumns;
88
		}
89
		$this->annotations[]=$manyToMany;
90
		$this->annotations[]=$jt;
91
	}
92
93
	public function getName() {
94
		return $this->name;
95
	}
96
97
	public function isManyToOne() {
98
		return $this->manyToOne;
99
	}
100
101
	public function getManyToOne() {
102
		foreach ( $this->annotations as $annotation ) {
103
			if ($annotation instanceof JoinColumnAnnotation) {
104
				return $annotation;
105
			}
106
		}
107
		return null;
108
	}
109
110
	public function isPrimary() {
111
		return $this->primary;
112
	}
113
114
	public function getGetter() {
115
		$result="\n\t public function get" . \ucfirst($this->name) . "(){\n";
116
		$result.="\t\t" . 'return $this->' . $this->name . ";\n";
117
		$result.="\t}\n";
118
		return $result;
119
	}
120
121
	public function getSetter() {
122
		$result="\n\t public function set" . \ucfirst($this->name) . '($' . $this->name . "){\n";
123
		$result.="\t\t" . '$this->' . $this->name . '=$' . $this->name . ";\n";
124
		$result.="\t}\n";
125
		return $result;
126
	}
127
128
	public function hasAnnotations(){
129
		return \count($this->annotations)>1;
130
	}
131
132
	public function isNullable(){
133
		if(isset($this->annotations["column"]))
134
			return $this->annotations["column"]->nullable;
135
		return false;
136
	}
137
138
	public function getDbType(){
139
		if(isset($this->annotations["column"]))
140
			return $this->annotations["column"]->dbType;
141
		return "mixed";
142
	}
143
	
144
	public function addValidators(){
145
		if($this->primary){
146
			$idValidator=ValidatorAnnotation::initializeFromModel("id", null,["notNull"=>false]);
147
			$this->annotations[]=$idValidator;
148
		}
149
		switch ($this->getDbType()){
150
			case "tinyint(1)":
151
				$validator=ValidatorAnnotation::initializeFromModel("type","boolean");
152
		}
153
		if(isset($validator)){
154
			$this->annotations[]=$validator;
155
		}
156
	}
157
}
158