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