Ubiquity 2.5.2
php rapid development framework
Loading...
Searching...
No Matches
ModelParser.php
Go to the documentation of this file.
1<?php
3
4use Ubiquity\utils\base\UArray;
8
20
21 protected $global;
22
23 protected $primaryKeys;
24
26
28
30
32
34
35 protected $nullableMembers = [];
36
38
39 protected $fieldNames;
40
41 protected $memberNames;
42
43 protected $fieldTypes = [];
44
45 protected $transformers = [];
46
47 protected $accessors = [];
48
49 protected $yuml;
50
51 protected $swapClasses = [];
52
53 protected function initSwapClasses($className) {
54 if (\class_exists('\\Ubiquity\\security\\acl\\AclManager', true)) {
55 if (\Ubiquity\security\acl\AclManager::isStarted()) {
56 $swapClasses = \Ubiquity\security\acl\AclManager::getModelClassesSwap();
57 $this->swapClasses += $swapClasses[$className] ?? [];
58 }
59 }
60 }
61
62 protected function swapValues($arrayOrValue) {
63 $result = $arrayOrValue;
64 foreach ($this->swapClasses as $original => $replacement) {
65 if (\is_array($arrayOrValue)) {
66 $result = $this->swapArrayValues($result, $original, $replacement);
67 } else {
68 $result = $this->swapValue($result, $original, $replacement);
69 }
70 }
71 return $result;
72 }
73
74 protected function swapArrayValues(array $values, $original, $replacement) {
75 $result = [];
76 foreach ($values as $k => $v) {
77 if (\is_array($v)) {
78 $result[$k] = $this->swapArrayValues($v, $original, $replacement);
79 } else {
80 $result[$k] = $this->swapValue($v, $original, $replacement);
81 }
82 }
83 return $result;
84 }
85
86 protected function swapValue($value, $original, $replacement) {
87 return \str_replace($original, $replacement, $value);
88 }
89
90 public function parse($modelClass) {
91 $instance = new $modelClass();
92 $this->initSwapClasses($modelClass);
94 $this->oneToManyMembers = Reflexion::getMembersAnnotationWithAnnotation($modelClass, 'oneToMany');
95 $this->manytoOneMembers = Reflexion::getMembersNameWithAnnotation($modelClass, 'manyToOne');
96 $this->manyToManyMembers = Reflexion::getMembersAnnotationWithAnnotation($modelClass, 'manyToMany');
97 $this->joinColumnMembers = Reflexion::getMembersAnnotationWithAnnotation($modelClass, 'joinColumn');
98 $this->joinTableMembers = Reflexion::getMembersAnnotationWithAnnotation($modelClass, 'joinTable');
99 $this->transformers = Reflexion::getMembersAnnotationWithAnnotation($modelClass, 'transformer');
100 $yuml = Reflexion::getAnnotationClass($modelClass, 'yuml');
101 if (\sizeof($yuml) > 0) {
102 $this->yuml = $yuml[0];
103 }
104 $properties = Reflexion::getProperties($instance);
105 foreach ($properties as $property) {
106 $propName = $property->getName();
107 $fieldName = Reflexion::getFieldName($modelClass, $propName);
108 $this->fieldNames[$propName] = $fieldName;
109 $this->memberNames[$fieldName] = $propName;
110 $nullable = Reflexion::isNullable($modelClass, $propName);
111
112 $serializable = Reflexion::isSerializable($modelClass, $propName);
113
114 if (! $serializable) {
115 $this->notSerializableMembers[] = $propName;
116 }
117 $type = Reflexion::getDbType($modelClass, $propName);
118 if ($type == null) {
119 $type = 'mixed';
120 }
121
122 if (\array_search($fieldName, $primaryKeys) !== false && DbTypes::isInt($type)) {
123 $nullable = true;
124 }
125
126 if ($nullable) {
127 $this->nullableMembers[] = $propName;
128 }
129 $this->fieldTypes[$propName] = $type;
130 $accesseur = 'set' . \ucfirst($propName);
131 if (! isset($this->accessors[$fieldName]) && \method_exists($modelClass, $accesseur)) {
132 $this->accessors[$fieldName] = $accesseur;
133 }
134 }
135 foreach ($primaryKeys as $pk) {
136 $this->primaryKeys[$pk] = $this->fieldNames[$pk] ?? $pk;
137 }
138
139 $this->global['#tableName'] = Reflexion::getTableName($modelClass);
140 }
141
142 public function asArray() {
143 $result = $this->global;
144 $result['#primaryKeys'] = $this->primaryKeys;
145 $result['#manyToOne'] = $this->manytoOneMembers;
146 $result['#fieldNames'] = $this->fieldNames;
147 $result['#memberNames'] = $this->memberNames;
148 $result['#fieldTypes'] = $this->fieldTypes;
149 $result['#nullable'] = $this->nullableMembers;
150 $result['#notSerializable'] = $this->notSerializableMembers;
151 $result['#transformers'] = [];
152 $result['#accessors'] = $this->accessors;
153 if (isset($this->yuml))
154 $result['#yuml'] = $this->yuml->getPropertiesAndValues();
155 foreach ($this->oneToManyMembers as $member => $annotation) {
156 $result['#oneToMany'][$member] = $annotation->getPropertiesAndValues();
157 }
158 foreach ($this->manyToManyMembers as $member => $annotation) {
159 $result['#manyToMany'][$member] = $annotation->getPropertiesAndValues();
160 }
161
162 foreach ($this->joinTableMembers as $member => $annotation) {
163 $result['#joinTable'][$member] = $annotation->getPropertiesAndValues();
164 }
165
166 if (\class_exists('Ubiquity\\contents\\transformation\\TransformersManager')) {
168 foreach ($this->transformers as $member => $annotation) {
169 $goodTransformer = false;
170 if (\array_search($member, $this->notSerializableMembers, false) !== false) {
171 throw new TransformerException(sprintf('%s member is not serializable and does not supports transformers!', $member));
172 }
173 $trans = TransformersManager::getTransformerClass($annotation->name);
174 if ($trans == null) {
175 throw new TransformerException(sprintf('%s value is not a declared transformer.', $annotation->name));
176 } else {
177 foreach (TransformersManager::TRANSFORMER_TYPES as $transType => $transInterface) {
178 if (\is_subclass_of($trans, $transInterface, true)) {
179 $goodTransformer = true;
180 $result['#transformers'][$transType][$member] = $trans;
181 }
182 }
183 if (! $goodTransformer) {
184 throw new TransformerException(sprintf('%s does not implements %s', $trans, 'TransformerInterfaces'));
185 }
186 }
187 }
188 }
189
190 foreach ($this->joinColumnMembers as $member => $annotation) {
191 $result['#joinColumn'][$member] = $this->swapValues($annotation->getPropertiesAndValues());
192 $result['#invertedJoinColumn'][$annotation->name] = [
193 'member' => $member,
194 'className' => $this->swapValues($annotation->className)
195 ];
196 }
197 return $result;
198 }
199
200 public function __toString() {
201 return 'return ' . UArray::asPhpArray($this->asArray(), 'array') . ';';
202 }
203}
static getTransformerClass(string $transformer)
Return the class from a transformer name.
Manage Databases types.
Definition DbTypes.php:14
swapValue($value, $original, $replacement)
swapArrayValues(array $values, $original, $replacement)
static getMembersAnnotationWithAnnotation($class, $annotation)
static getAnnotationClass($class, $annotation)
Definition Reflexion.php:85
static getKeyFields($instance)
Definition Reflexion.php:26
static getMembersNameWithAnnotation($class, $annotation)
Class Configuration \config.