Ubiquity 2.5.2
php rapid development framework
Loading...
Searching...
No Matches
Model.php
Go to the documentation of this file.
1<?php
3
7
18class Model extends HasUsesTrait {
19
20 private $simpleMembers;
21
22 private $members;
23
24 private $name;
25
26 private $table;
27
28 private $namespace;
29
30 private $database;
31
32 private $memberAccess;
33
34 private $annotsEngine;
35
36 private $annots;
37
38 private $classMerger;
39
40 public function __construct($annotsEngine, $name, $namespace = 'models', $memberAccess = 'private') {
41 $this->annotsEngine = $annotsEngine;
42 $this->table = $name;
43 $this->name = \ucfirst($name);
44 $this->members = array();
45 $this->namespace = $namespace;
46 $this->memberAccess = $memberAccess;
47 $this->uses = [];
48 }
49
54 public function getMembers(): array {
55 return $this->members;
56 }
57
58 public function getMethods() {
59 $methods = [
60 '__toString'
61 ];
62 if ($this->hasConstructor()) {
63 $methods[] = '__construct';
64 }
65 foreach ($this->members as $member) {
66 $methods = \array_merge($methods, $member->getMethods());
67 }
68 return $methods;
69 }
70
71 private function hasConstructor(): bool {
72 foreach ($this->members as $member) {
73 if ($member->isMany()) {
74 return true;
75 }
76 }
77 return false;
78 }
79
84 public function getAnnotsEngine() {
85 return $this->annotsEngine;
86 }
87
88 public function addManyToOne($member, $name, $className, $alternateName) {
89 $this->checkForUniqName($member, $alternateName);
90 $nullable = false;
91 if (\array_key_exists($member, $this->members) === false) {
92 $this->addMember(new Member($this, $this->annotsEngine, $member, $this->memberAccess));
93 if (isset($this->members[$name])) {
94 $nullable = $this->members[$name]->isNullable();
95 $this->removeMember($name);
96 }
97 }
98 $this->members[$member]->addManyToOne($name, $className, $nullable);
99 }
100
101 private function checkForUniqName(&$member, $alternateName) {
102 if (isset($this->members[$member]) && \array_search($member, $this->simpleMembers) === false) {
103 if ($alternateName != null) {
104 $this->checkForUniqName($alternateName, null);
105 $member = $alternateName;
106 } else {
107 $member = $this->generateUniqName($member);
108 }
109 }
110 }
111
112 private function generateUniqName($member) {
113 $i = 1;
114 do {
115 $name = $member . $i;
116 $i ++;
117 } while (isset($this->members[$name]));
118 return $name;
119 }
120
121 public function addMember(Member $member) {
122 $this->members[$member->getName()] = $member;
123 return $this;
124 }
125
126 public function removeMember($memberName) {
127 if (isset($this->members[$memberName]) && $this->members[$memberName]->isPrimary() === false)
128 unset($this->members[$memberName]);
129 }
130
131 public function removeOneToManyMemberByClassAssociation($className) {
132 $toDelete = [];
133 foreach ($this->members as $name => $member) {
134 $annotations = $member->getAnnotations();
135 foreach ($annotations as $annotation) {
136 if ($this->annotsEngine->is('oneToMany', $annotation)) {
137 if ($annotation->className === $className) {
138 $toDelete[] = $name;
139 break;
140 }
141 }
142 }
143 }
144 foreach ($toDelete as $name) {
145 unset($this->members[$name]);
146 }
147 }
148
149 public function addOneToMany($member, $mappedBy, $className, $alternateName) {
150 $this->checkForUniqName($member, $alternateName);
151 if (\array_key_exists($member, $this->members) === false) {
152 $this->addMember(new Member($this, $this->annotsEngine, $member, $this->memberAccess));
153 }
154 $this->members[$member]->addOneToMany($mappedBy, $className);
155 }
156
157 public function addManyToMany($member, $targetEntity, $inversedBy, $joinTable, $joinColumns = [], $inverseJoinColumns = [], $alternateName = null) {
158 $this->checkForUniqName($member, $alternateName);
159 if (\array_key_exists($member, $this->members) === false) {
160 $this->addMember(new Member($this, $this->annotsEngine, $member, $this->memberAccess));
161 }
162 $this->members[$member]->addManyToMany($targetEntity, $inversedBy, $joinTable, $joinColumns, $inverseJoinColumns);
163 return $member;
164 }
165
166 public function addMainAnnots() {
167 $annots = [];
168 if (\class_exists('\AllowDynamicProperties')) {
169 $annots[]='#[\AllowDynamicProperties()]';
170 }
171 if ($this->database != null && $this->database !== 'default') {
172 $annots[] = $this->annotsEngine->getAnnotation($this, 'database', [
173 'name' => $this->database
174 ]);
175 }
176 if ($this->table !== $this->name) {
177 $annots[] = $this->annotsEngine->getAnnotation($this, 'table', [
178 'name' => $this->table
179 ]);
180 }
181 $this->annots = $annots;
182 }
183
184 public function __toString() {
185 $cm = $this->getClassMerger();
186 $result = "<?php\n";
187 if ($this->namespace !== '' && $this->namespace !== null) {
188 $result .= 'namespace ' . \rtrim($this->namespace, '\\') . ";\n";
189 }
190
191 if (\count($this->uses) > 0) {
192 $result .= "\n" . $this->getUsesStr() . "\n";
193 }
194
195 if (\count($this->annots) > 0) {
196 $result .= $this->annotsEngine->getAnnotationsStr($this->annots, '') . "\n";
197 }
198 $result .= 'class ' . \ucfirst($this->name) . '{';
199 $members = $this->members;
200 \array_walk($members, function ($item) {
201 return $item . '';
202 });
203 $result .= \implode('', $members) . "\n";
204 $result .= $this->generateMethods($members, $cm);
205 $result .= "\n}";
206
207 return $result;
208 }
209
210 protected function getClassMerger(): ?ClassMerger {
211 if (\class_exists('\\' . $this->getName(), true)) {
212 if ($this->classMerger == null) {
213 $this->classMerger = new ClassMerger($this->getName(), $this);
214 $this->classMerger->merge();
215 }
216 return $this->classMerger;
217 }
218 return null;
219 }
220
221 public function getName() {
222 $namespace = '';
223 if ($this->namespace !== '' && $this->namespace !== null) {
224 $namespace = \rtrim($this->namespace, '\\') . '\\';
225 }
226 return $namespace . $this->name;
227 }
228
235 protected function generateMethods(array $members, ?ClassMerger $classMerger) {
236 $result = '';
237 if ($classMerger == null) {
238 if ($this->hasConstructor()) {
239 $result = $this->getConstructor() . PHP_EOL;
240 }
241 foreach ($members as $member) {
242 $result .= $member->getGetter() . PHP_EOL;
243 $result .= $member->getSetter() . PHP_EOL;
244 if ($member->isManyToMany()) {
245 $result .= $member->getAddInManyToManyMember() . PHP_EOL;
246 }
247 if ($member->isOneToMany()) {
248 $result .= $member->getAddInOneToManyMember() . PHP_EOL;
249 }
250 }
251 $result .= $this->getToString();
252 } else {
253 if ($this->hasConstructor()) {
254 $result = $classMerger->getMethodCode('__construct', $this->getConstructor()) . PHP_EOL;
255 }
256 foreach ($members as $member) {
257 $result .= $classMerger->getMethodCode($member->getGetterName(), $member->getGetter()) . PHP_EOL;
258 $result .= $classMerger->getMethodCode($member->getSetterName(), $member->getSetter()) . PHP_EOL;
259 if ($member->isManyToMany()) {
260 $result .= $classMerger->getMethodCode($member->getInManyToManyMemberName(), $member->getAddInManyToManyMember()) . PHP_EOL;
261 }
262 if ($member->isOneToMany()) {
263 $result .= $classMerger->getMethodCode($member->getInOneToManyMemberName(), $member->getAddInOneToManyMember()) . PHP_EOL;
264 }
265 }
266 $result .= $classMerger->getMethodCode('__toString', $this->getToString()) . PHP_EOL;
267 $result .= $classMerger->getExtCode();
268 }
269 return $result;
270 }
271
272 public function getConstructor() {
273 $initializes = [];
274 foreach ($this->members as $member) {
275 if ($member->isMany()) {
276 $initializes[] = "\t\t\$this->" . $member->getName() . ' = [];';
277 }
278 }
279 if (\count($initializes) > 0) {
280 $corps = \implode(PHP_EOL, $initializes);
281 $result = "\n\t public function __construct(){\n";
282 $result .= "$corps\n";
283 $result .= "\t}\n";
284 return $result;
285 }
286 return '';
287 }
288
289 public function getToString() {
290 $field = $this->getToStringField();
291 if (isset($field)) {
292 $corps = '($this->' . $field . "??'no value').''";
293 } elseif (($pkName = $this->getPkName()) !== null) {
294 $corps = '$this->' . $pkName . ".''";
295 } else {
296 $corps = '"' . $this->name . '@"' . '.\spl_object_hash($this)';
297 }
298 $result = "\n\t public function __toString(){\n";
299 $result .= "\t\t" . 'return ' . $corps . ";\n";
300 $result .= "\t}\n";
301 return $result;
302 }
303
304 private function getToStringField() {
305 $result = null;
306 foreach ($this->members as $member) {
307 if ($member->getDbType() !== 'mixed' && ! $member->isPrimary()) {
308 $memberName = $member->getName();
309 if (! $member->isPassword()) {
310 $result= $memberName;
311 if($member->isNullable()!==false){
312 return $result;
313 }
314 }
315 }
316 }
317 return $result;
318 }
319
320 public function getPkName() {
321 $pk = $this->getPrimaryKey();
322 if (isset($pk)) {
323 return $pk->getName();
324 }
325 return null;
326 }
327
328 public function getPrimaryKey() {
329 foreach ($this->members as $member) {
330 if ($member->isPrimary() === true) {
331 return $member;
332 }
333 }
334 return null;
335 }
336
337 public function getSimpleName() {
338 return $this->name;
339 }
340
341 public function setDatabase($database) {
342 $this->database = $database;
343 }
344
345 public function isAssociation() {
346 $count = 0;
347 foreach ($this->members as $member) {
348 if ($member->isManyToOne() === true || $member->isPrimary() === true) {
349 $count ++;
350 }
351 }
352 return $count == \count($this->members);
353 }
354
355 public function getDefaultFk() {
356 return 'id' . $this->name;
357 }
358
359 public function getManyToOneMembers() {
360 $result = [];
361 foreach ($this->members as $member) {
362 if ($member->isManyToOne() === true) {
363 $result[] = $member;
364 }
365 }
366 return $result;
367 }
368
369 public function setSimpleMembers($members) {
370 $this->simpleMembers = $members;
371 }
372
376 public function setTable($table): void {
377 $this->table = $table;
378 }
379
380}
Ubiquity\annotations$AnnotationsInterface This class is part of Ubiquity.