Ubiquity 2.5.2
php rapid development framework
Loading...
Searching...
No Matches
Member.php
Go to the documentation of this file.
1<?php
3
7
18class Member {
19
20 private $name;
21
22 private $primary;
23
24 private $transient;
25
26 private $manyToOne;
27
28 private $annotations;
29
30 private $access;
31
32 private $container;
33
38
39 public function __construct($container,$annotsEngine,$name, $access = 'private') {
40 $this->container=$container;
41 $this->annotsEngine=$annotsEngine;
42 $this->name = $name;
43 $this->annotations = [];
44 $this->primary = false;
45 $this->transient=false;
46 $this->manyToOne = false;
47 $this->access = $access;
48 }
49
50 public function __toString() {
51 $annotationsStr = '';
52 if (\count($this->annotations) > 0) {
53 $annotationsStr = "\n".$this->annotsEngine->getAnnotationsStr($this->annotations);
54 }
55 return $annotationsStr . "\n\t{$this->access} $" . $this->name . ";\n";
56 }
57
58 public function setPrimary() {
59 if ($this->primary === false) {
60 $this->annotations[] = $this->annotsEngine->getAnnotation($this->container,'id');
61 $this->primary = true;
62 }
63 }
64
65 public function setTransient() {
66 if ($this->transient === false) {
67 $this->annotations[] = $this->annotsEngine->getAnnotation($this->container,'transient');
68 $this->transient = true;
69 }
70 }
71
72 public function setDbType($infos) {
73 $annot = $this->annotsEngine->getAnnotation($this->container,'column',['name'=>$this->name,'dbType'=>$infos['Type'],'nullable'=>(\strtolower($infos['Nullable']) === 'yes')]);
74 $this->annotations['column'] = $annot;
75 }
76
77 public function addManyToOne($name, $className, $nullable = false) {
78 $this->annotations[] = $this->annotsEngine->getAnnotation($this->container,'manyToOne');
79 $joinColumn = $this->annotsEngine->getAnnotation($this->container,'joinColumn',['name'=>$name,'className'=>$className,'nullable'=>$nullable]);
80 $this->annotations[] = $joinColumn;
81 $this->manyToOne = true;
82 }
83
84 public function addOneToMany($mappedBy, $className) {
85 $this->annotations[] = $this->annotsEngine->getAnnotation($this->container,'oneToMany',['mappedBy'=>$mappedBy,'className'=>$className]);;
86 }
87
88 private function addTransformer($name) {
89 $this->annotations[] = $this->annotsEngine->getAnnotation($this->container,'transformer',['name'=>$name]);;
90 }
91
95 public function setTransformer() {
96 if ($this->isPassword()) {
97 $this->addTransformer('password');
98 } else {
99 $dbType = $this->getDbType();
100 if ($dbType == 'datetime') {
101 $this->addTransformer('datetime');
102 }
103 }
104 }
105
106 public function isPassword() {
107 // Array of multiple translations of the word "password" which could be taken as name of the table field in database
108 $pwArray = array(
109 'password',
110 'senha',
111 'lozinka',
112 'heslotajne',
113 'helslo_tajne',
114 'wachtwoord',
115 'contrasena',
116 'salasana',
117 'motdepasse',
118 'mot_de_passe',
119 'passwort',
120 'passord',
121 'haslo',
122 'senha',
123 'parola',
124 'naponb',
125 'contrasena',
126 'loesenord',
127 'losenord',
128 'sifre',
129 'naponb',
130 'matkhau',
131 'mat_khau'
132 );
133 return \in_array($this->name, $pwArray);
134 }
135
136 public function addManyToMany($targetEntity, $inversedBy, $joinTable, $joinColumns = [], $inverseJoinColumns = []) {
137 $manyToMany = $this->annotsEngine->getAnnotation($this->container,'manyToMany',\compact('targetEntity','inversedBy'));
138 $jtArray['name'] = $joinTable;
139 if (\count($joinColumns) == 2) {
140 $jtArray['joinColumns'] = $joinColumns;
141 }
142 if (\count($inverseJoinColumns) == 2) {
143 $jtArray['inverseJoinColumns'] = $inverseJoinColumns;
144 }
145 $this->annotations[] = $manyToMany;
146 $this->annotations[] = $this->annotsEngine->getAnnotation($this->container,'joinTable',$jtArray);
147 }
148
149 public function getName() {
150 return $this->name;
151 }
152
153 public function isManyToOne() {
154 return $this->manyToOne;
155 }
156
157 public function getManyToOne() {
158 foreach ($this->annotations as $annotation) {
159 if ($this->annotsEngine->isManyToOne($annotation)) {
160 return $annotation;
161 }
162 }
163 return null;
164 }
165
166 public function getOneToMany() {
167 foreach ($this->annotations as $annotation) {
168 if ($this->annotsEngine->isOneToMany($annotation)) {
169 return $annotation;
170 }
171 }
172 return null;
173 }
174
175 public function isPrimary() {
176 return $this->primary;
177 }
178
179 public function isAutoinc(){
180 return $this->primary && DbTypes::isInt ( $this->getDbType() );
181 }
182
183 public function getGetter() {
184 $result = "\n\tpublic function " . $this->getGetterName() . "(){\n";
185 $result .= "\t\t" . 'return $this->' . $this->name . ";\n";
186 $result .= "\t}\n";
187 return $result;
188 }
189
190 public function getGetterName(){
191 return 'get' . \ucfirst($this->name);
192 }
193
194 public function getSetter() {
195 $result = "\n\tpublic function " . $this->getSetterName() . '($' . $this->name . "){\n";
196 $result .= "\t\t" . '$this->' . $this->name . '=$' . $this->name . ";\n";
197 $result .= "\t}\n";
198 return $result;
199 }
200
201 public function getSetterName(){
202 return 'set' . \ucfirst($this->name);
203 }
204
205 public function getAddInManyToManyMember() {
206 $name = \trim($this->name,'s');
207 $result = "\n\t public function " . $this->getInManyToManyMemberName() . '($' . $name . "){\n";
208 $result .= "\t\t" . '$this->' . $this->name . '[]=$' . $name . ";\n";
209 $result .= "\t}\n";
210 return $result;
211 }
212
213 public function getAddInOneToManyMember() {
214 $name = \trim($this->name,'s');
215 $annot=$this->getOneToMany();
216 $mappedBy=$annot->mappedBy;
217 $result = "\n\t public function " . $this->getInOneToManyMemberName() . '($' . $name . "){\n";
218 $result .= "\t\t" . '$this->' . $this->name . '[]=$' . $name . ";\n";
219 $result .= "\t\t\$" . $name . '->set' .\ucfirst($mappedBy)."(\$this);\n";
220 $result .= "\t}\n";
221 return $result;
222 }
223
224 public function getInManyToManyMemberName(){
225 return 'add'.\ucfirst(\rtrim($this->name,'s'));
226 }
227
228 public function getInOneToManyMemberName(){
229 return 'addTo'.\ucfirst($this->name);
230 }
231
232 public function hasAnnotations() {
233 return \count($this->annotations) > 1;
234 }
235
236 public function isMany() {
237 foreach ($this->annotations as $annot) {
238 if ($this->annotsEngine->isMany($annot)) {
239 return true;
240 }
241 }
242 return false;
243 }
244
245 public function isManyToMany() {
246 foreach ($this->annotations as $annot) {
247 if ($this->annotsEngine->isManyToMany($annot)) {
248 return true;
249 }
250 }
251 return false;
252 }
253
254 public function isOneToMany() {
255 foreach ($this->annotations as $annot) {
256 if ($this->annotsEngine->isOneToMany($annot)) {
257 return true;
258 }
259 }
260 return false;
261 }
262
263 public function isNullable() {
264 if (isset($this->annotations['column']))
265 return $this->annotations['column']->nullable;
266 return false;
267 }
268
269 public function getDbType() {
270 if (isset($this->annotations['column']))
271 return $this->annotations['column']->dbType;
272 return 'mixed';
273 }
274
275 public function addValidators() {
276 $parser = new ValidationModelGenerator($this->container,$this->annotsEngine,$this->getDbType(), $this->name, ! $this->isNullable(), $this->primary);
277 $validators = $parser->parse();
278 if ($validators && \count($validators)) {
279 $this->annotations = \array_merge($this->annotations, $validators);
280 }
281 }
282
283 public function getAnnotations() {
284 return $this->annotations;
285 }
286
287 public function getMethods(){
288 $r=[$this->getGetterName(),$this->getSetterName()];
289 if($this->isManyToMany()){
290 $r[]=$this->getInManyToManyMemberName();
291 }
292 if($this->isOneToMany()){
293 $r[]=$this->getInOneToManyMemberName();
294 }
295 return $r;
296 }
297
298 public function addAnnotations(array $annotations){
299 $this->annotations=\array_merge($this->annotations,$annotations);
300 }
301}
Manage Databases types.
Definition DbTypes.php:14
addManyToOne($name, $className, $nullable=false)
Definition Member.php:77
__construct($container, $annotsEngine, $name, $access='private')
Definition Member.php:39
setTransformer()
Try to set a transformer to the member.
Definition Member.php:95
addOneToMany($mappedBy, $className)
Definition Member.php:84
addManyToMany($targetEntity, $inversedBy, $joinTable, $joinColumns=[], $inverseJoinColumns=[])
Definition Member.php:136
addAnnotations(array $annotations)
Definition Member.php:298
Ubiquity\annotations$AnnotationsInterface This class is part of Ubiquity.