Ubiquity 2.5.2
php rapid development framework
Loading...
Searching...
No Matches
OrmUtils.php
Go to the documentation of this file.
1<?php
2
3namespace Ubiquity\orm;
4
10
18
19 private static $modelsMetadatas;
20
21 public static function getModelMetadata($className) {
22 return self::$modelsMetadatas [$className] ??= CacheManager::getOrmModelCache($className);
23 }
24
25 public static function isSerializable($class, $member) {
26 return !self::_is($class, $member, '#notSerializable');
27 }
28
29 public static function isNullable($class, $member) {
30 return self::_is($class, $member, '#nullable');
31 }
32
33 protected static function _is($class, $member, $like) {
34 $ret = self::getAnnotationInfo($class, $like);
35 if ($ret !== false) {
36 return \array_search($member, $ret) !== false;
37 }
38 return false;
39 }
40
41 public static function getFieldName($class, $member) {
42 return (self::getAnnotationInfo($class, '#fieldNames') [$member]) ?? $member;
43 }
44
45 public static function getTableName($class) {
46 return self::getModelMetadata($class) ['#tableName'];
47 }
48
49 public static function getKeyFieldsAndValues($instance, $prefix = '') {
50 $class = \get_class($instance);
51 return self::getFieldsAndValues_($instance, self::getKeyMembers($class), $prefix);
52 }
53
54 public static function getFieldsAndValues_($instance, $members, $prefix = '') {
55 $ret = [];
56 $fieldnames = self::getAnnotationInfo(\get_class($instance), '#fieldNames');
57 foreach ($members as $member) {
58 $v = Reflexion::getMemberValue($instance, $member);
59 $ret [$prefix . ($fieldnames [$member] ?? $member)] = $v;
60 }
61 return $ret;
62 }
63
64 public static function getKeyPropsAndValues_($instance, $props) {
65 $ret = [];
66 foreach ($props as $prop) {
67 $v = Reflexion::getPropValue($instance, $prop);
68 $ret [$prop->getName()] = $v;
69 }
70 return $ret;
71 }
72
73 public static function getPrimaryKeys($className) {
74 return self::getAnnotationInfo($className, '#primaryKeys');
75 }
76
77 public static function getMembers($className) {
78 $fieldNames = self::getAnnotationInfo($className, '#fieldNames');
79 if ($fieldNames !== false) {
80 return \array_keys($fieldNames);
81 }
82 return [];
83 }
84
85 public static function getMembersAndValues($instance, $members = null) {
86 $ret = array();
87 $className = \get_class($instance);
88 if (\is_null($members))
89 $members = self::getMembers($className);
90 foreach ($members as $member) {
91 if (self::isSerializable($className, $member)) {
92 $v = Reflexion::getMemberValue($instance, $member);
93 if (self::isNotNullOrNullAccepted($v, $className, $member)) {
94 $name = self::getFieldName($className, $member);
95 $ret [$name] = $v;
96 }
97 }
98 }
99 return $ret;
100 }
101
102 public static function isNotNullOrNullAccepted($v, $className, $member) {
103 $notNull = (isset ($v) && null !== $v && '' !== $v);
104 return ($notNull) || (!$notNull && self::isNullable($className, $member));
105 }
106
107 public static function getFirstKeyValue($instance) {
108 $prop = OrmUtils::getFirstPropKey(\get_class($instance));
109 return $prop->getValue($instance);
110 }
111
112 public static function getFirstKeyValue_($instance, $members) {
113 return \current(self::getFieldsAndValues_($instance, $members));
114 }
115
116 public static function getKeyValues($instance) {
117 $fkv = self::getKeyFieldsAndValues($instance);
118 return \implode('_', $fkv);
119 }
120
121 public static function getPropKeyValues($instance, $props) {
122 $values = [];
123 foreach ($props as $prop) {
124 $values [] = $prop->getValue($instance);
125 }
126 return \implode('_', $values);
127 }
128
129 public static function getMembersWithAnnotation($class, $annotation) {
130 return (self::getModelMetadata($class) [$annotation]) ?? [];
131 }
132
140 public static function exists($instance, $memberKey, $array) {
141 $accessor = 'get' . \ucfirst($memberKey);
142 if (\method_exists($instance, $accessor)) {
143 foreach ($array as $value) {
144 if ($value->$accessor () == $instance->$accessor ())
145 return true;
146 }
147 }
148 return false;
149 }
150
151 public static function getAnnotationInfo($class, $keyAnnotation) {
152 return self::getModelMetadata($class) [$keyAnnotation] ?? false;
153 }
154
155 public static function getAnnotationInfoMember($class, $keyAnnotation, $member) {
156 $info = self::getAnnotationInfo($class, $keyAnnotation);
157 if ($info !== false) {
158 if (!isset ($info [0])) { // isAssociative
159 if (isset ($info [$member])) {
160 return $info [$member];
161 }
162 } else {
163 if (\array_search($member, $info) !== false) {
164 return $member;
165 }
166 }
167 }
168 return false;
169 }
170
171 public static function setFieldToMemberNames(&$fields, $relFields) {
172 foreach ($fields as $index => $field) {
173 if (isset ($relFields [$field])) {
174 $fields [$index] = $relFields [$field];
175 }
176 }
177 }
178
179 public static function objectAsJSON($instance) {
180 $formatter = new ResponseFormatter ();
181 $datas = $formatter->cleanRestObject($instance);
182 return $formatter->format(['pk' => self::getFirstKeyValue($instance), 'object' => $datas]);
183 }
184
185 public static function getTransformers($class) {
186 if (isset (self::getModelMetadata($class) ['#transformers']))
187 return self::getModelMetadata($class) ['#transformers'];
188 }
189
190 public static function getAccessors($class) {
191 if (isset (self::getModelMetadata($class) ['#accessors']))
192 return self::getModelMetadata($class) ['#accessors'];
193 }
194
195 public static function clearMetaDatas() {
196 self::$modelsMetadatas = [];
197 }
198
199 public static function hasAllMembersPublic($className) {
200 $members = self::getMembers($className);
201 foreach ($members as $memberName) {
202 $field = new \ReflectionProperty ($className, $memberName);
203 if (!$field->isPublic()) {
204 return false;
205 }
206 }
207 return true;
208 }
209}
Manager for caches (Router, Rest, models).
Object/relational mapping utilities.
Definition OrmUtils.php:17
static getAnnotationInfo($class, $keyAnnotation)
Definition OrmUtils.php:151
static getMembers($className)
Definition OrmUtils.php:77
static isSerializable($class, $member)
Definition OrmUtils.php:25
static getFirstKeyValue_($instance, $members)
Definition OrmUtils.php:112
static getTableName($class)
Definition OrmUtils.php:45
static getKeyPropsAndValues_($instance, $props)
Definition OrmUtils.php:64
static hasAllMembersPublic($className)
Definition OrmUtils.php:199
static getKeyValues($instance)
Definition OrmUtils.php:116
static getAccessors($class)
Definition OrmUtils.php:190
static isNotNullOrNullAccepted($v, $className, $member)
Definition OrmUtils.php:102
static exists($instance, $memberKey, $array)
Definition OrmUtils.php:140
static getPropKeyValues($instance, $props)
Definition OrmUtils.php:121
static getTransformers($class)
Definition OrmUtils.php:185
static objectAsJSON($instance)
Definition OrmUtils.php:179
static getPrimaryKeys($className)
Definition OrmUtils.php:73
static setFieldToMemberNames(&$fields, $relFields)
Definition OrmUtils.php:171
static getFieldName($class, $member)
Definition OrmUtils.php:41
static getKeyFieldsAndValues($instance, $prefix='')
Definition OrmUtils.php:49
static getFieldsAndValues_($instance, $members, $prefix='')
Definition OrmUtils.php:54
static getFirstKeyValue($instance)
Definition OrmUtils.php:107
static getModelMetadata($className)
Definition OrmUtils.php:21
static getMembersWithAnnotation($class, $annotation)
Definition OrmUtils.php:129
static isNullable($class, $member)
Definition OrmUtils.php:29
static getMembersAndValues($instance, $members=null)
Definition OrmUtils.php:85
static _is($class, $member, $like)
Definition OrmUtils.php:33
static getAnnotationInfoMember($class, $keyAnnotation, $member)
Definition OrmUtils.php:155
Reflection utilities in dev environment only.
Definition Reflexion.php:17
Ubiquity\orm\traits$OrmUtilsFieldsTrait This class is part of Ubiquity.