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