Ubiquity 2.5.2
php rapid development framework
Loading...
Searching...
No Matches
OrmUtilsFieldsTrait.php
Go to the documentation of this file.
1<?php
2
3namespace Ubiquity\orm\traits;
4
14
15 abstract public static function getAnnotationInfo($class, $keyAnnotation);
16
17 abstract public static function getAnnotationInfoMember($class, $keyAnnotation, $member);
18 protected static $fieldNames = [ ];
19 protected static $propFirstKeys = [ ];
20 protected static $propKeys = [ ];
21 protected static $accessors = [ ];
22
23 public static function getFieldTypes($className) {
24 $fieldTypes = self::getAnnotationInfo ( $className, '#fieldTypes' );
25 if ($fieldTypes !== false) {
26 return $fieldTypes;
27 }
28 return [ ];
29 }
30
31 public static function getFieldType($className, $field) {
32 $types = self::getFieldTypes ( $className );
33 if (isset ( $types [$field] )) {
34 return $types [$field];
35 }
36 return 'int';
37 }
38
45 public static function getKeyFields($instance) {
46 if (! \is_string ( $instance )) {
47 $instance = \get_class ( $instance );
48 }
49 return self::getAnnotationInfo ( $instance, '#primaryKeys' );
50 }
51
58 public static function getKeyMembers($instance) {
59 if (! \is_string ( $instance )) {
60 $instance = \get_class ( $instance );
61 }
62 if($info=self::getAnnotationInfo ( $instance, '#primaryKeys' )){
63 return \array_keys ( $info );
64 }
65 return [];
66
67 }
68
69 public static function getFirstKey($class) {
70 $kf = self::getAnnotationInfo ( $class, '#primaryKeys' );
71 if($kf){
72 return \current ( $kf );
73 }
74 return '';
75 }
76
82 public static function getFirstPropKey($class) {
83 if (isset ( self::$propFirstKeys [$class] )) {
84 return self::$propFirstKeys [$class];
85 }
86 $prop = new \ReflectionProperty ( $class, \array_key_first ( self::getAnnotationInfo ( $class, '#primaryKeys' ) ) );
87 $prop->setAccessible ( true );
88 return self::$propFirstKeys [$class] = $prop;
89 }
90
91 public static function getPropKeys($class) {
92 if (isset ( self::$propKeys [$class] )) {
93 return self::$propKeys [$class];
94 }
95 $result = [ ];
96 $pkMembers = self::getAnnotationInfo ( $class, '#primaryKeys' );
97 foreach ( $pkMembers as $member => $_field ) {
98 $prop = new \ReflectionProperty ( $class, $member );
99 $prop->setAccessible ( true );
100 $result [] = $prop;
101 }
102 return self::$propKeys [$class] = $result;
103 }
104
105 public static function getAccessors($class, $members) {
106 if (isset ( self::$accessors [$class] )) {
107 return self::$accessors [$class];
108 }
109 $result = [ ];
110 foreach ( $members as $member => $field ) {
111 $accesseur = 'set' . \ucfirst ( $member );
112 if (! isset ( $result [$field] ) && method_exists ( $class, $accesseur )) {
113 $result [$field] = $accesseur;
114 }
115 }
116 return self::$accessors [$class] = $result;
117 }
118
119 public static function getAllFields($class) {
120 return \array_keys ( self::getAnnotationInfo ( $class, '#fieldNames' ) );
121 }
122
123 public static function getFieldNames($model) {
124 if (isset ( self::$fieldNames [$model] )) {
125 return self::$fieldNames [$model];
126 }
127 $fields = self::getAnnotationInfo ( $model, '#fieldNames' );
128 $result = [ ];
129 $serializables = self::getSerializableFields ( $model );
130 foreach ( $fields as $member => $field ) {
131 if (\array_search ( $member, $serializables ) !== false)
132 $result [$field] = $member;
133 }
134 return self::$fieldNames [$model] = $result;
135 }
136
137 public static function getSerializableFields($class) {
138 $notSerializable = self::getAnnotationInfo ( $class, '#notSerializable' );
139 $fieldNames = \array_values ( self::getAnnotationInfo ( $class, '#fieldNames' ) );
140 return \array_diff ( $fieldNames, $notSerializable );
141 }
142
143 public static function getNullableFields($class) {
144 return self::getAnnotationInfo ( $class, '#nullable' );
145 }
146
147 public static function getSerializableMembers($class) {
148 $notSerializable = self::getAnnotationInfo ( $class, '#notSerializable' );
149 $memberNames = \array_keys ( self::getAnnotationInfo ( $class, '#fieldNames' ) );
150 return \array_diff ( $memberNames, $notSerializable );
151 }
152
153 public static function getFormAllFields($class) {
154 $result = self::getSerializableMembers ( $class );
155 if ($manyToOne = self::getAnnotationInfo ( $class, '#manyToOne' )) {
156 foreach ( $manyToOne as $member ) {
157 $joinColumn = self::getAnnotationInfoMember ( $class, '#joinColumn', $member );
158 $result [] = $joinColumn ['name'];
159 }
160 }
161 if ($manyToMany = self::getAnnotationInfo ( $class, '#manyToMany' )) {
162 $manyToMany = \array_keys ( $manyToMany );
163 foreach ( $manyToMany as $member ) {
164 $result [] = $member . 'Ids';
165 }
166 }
167 return $result;
168 }
169}
170
Ubiquity\orm\traits$OrmUtilsFieldsTrait This class is part of Ubiquity.
static getAnnotationInfo($class, $keyAnnotation)
static getKeyFields($instance)
Return primary key fields from instance or model class.
static getKeyMembers($instance)
Return primary key members from instance or model class.
static getAnnotationInfoMember($class, $keyAnnotation, $member)
Ubiquity\orm\traits This class is part of Ubiquity.