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