This repository has been archived by the owner on Feb 13, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
RTTI.php
215 lines (177 loc) · 6.18 KB
/
RTTI.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
<?php
/* THIS FILE:
* a) BELONGS TO THE 'PHP-UTIL' LIBRARY:
* https://github.com/thiagodp/php-util
*
* b) IS DISTRIBUTED UNDER THE CREATIVE COMMONS LICENCE (CC BY 3.0):
* http://creativecommons.org/licenses/by/3.0/
*
* USE IT AT YOUR OWN RISK!
*/
/**
* Run time type information utilities.
*
* @author Thiago Delgado Pinto
* @version 3.1
*/
class RTTI {
const IS_PRIVATE = ReflectionProperty::IS_PRIVATE;
const IS_PROTECTED = ReflectionProperty::IS_PROTECTED;
const IS_PUBLIC = ReflectionProperty::IS_PUBLIC;
/**
* Return all the visibility flags.
* @return int
*/
static function allFlags() {
return ReflectionProperty::IS_PRIVATE | ReflectionProperty::IS_PROTECTED | ReflectionProperty::IS_PUBLIC;
}
/**
* Just a synonym to allFlags()
* @return int
*/
static function anyVisibility() {
return self::allFlags();
}
/**
* Retrieve names and values from the attributes of a object, as a map.
*
* @param object $obj The object.
*
* @param int $visibilityFlags Filter visibility flags. Can be added.
* Example: RTTI::IS_PRIVATE | RTTI::IS_PROTECTED
*
* @param string $getterPrefix The prefix for getter public methods (defaults to 'get').
*
* @param bool $useCamelCase If true, private and protected attributes will be accessed
* by camelCase public methods (default true).
*
* @return array
*/
static function getAttributes( $obj, $visibilityFlags, $getterPrefix = 'get', $useCamelCase = true ) {
if ( ! isset( $obj ) ) {
return array();
}
$attributes = array();
$reflectionObject = new ReflectionObject( $obj );
$currentClass = new ReflectionClass( $obj );
while ( $currentClass !== false && ! $currentClass->isInterface() ) {
$properties = $currentClass->getProperties( $visibilityFlags );
foreach ( $properties as $property ) {
$attributeName = $property->getName();
$methodName = $getterPrefix . ( $useCamelCase ? ucfirst( $attributeName ) : $attributeName );
if ( $property->isPrivate() || $property->isProtected() ) {
if ( $reflectionObject->hasMethod( $methodName ) ) {
$method = $reflectionObject->getMethod( $methodName );
if ( $method->isPublic() ) {
$attributes[ $attributeName ] = $method->invoke( $obj );
}
}
} else { // public
try {
$attributes[ $attributeName ] = $obj->{ $attributeName };
} catch (Exception $e) {
// Ignore
}
}
}
// No properties? -> try to retrieve only public properties
if ( count( $properties ) < 1 ) {
$properties = get_object_vars( $obj );
foreach ( $properties as $k => $v ) {
$attributes[ $k ] = $v;
}
}
$currentClass = $currentClass->getParentClass();
}
return $attributes;
}
/**
* Retrieve names and values from the private attributes of a object, as a map.
* This method has been kept for backward compatibility.
*
* @param object $obj The object.
* @param string $getterPrefix The prefix for getter public methods (defaults to 'get').
* @param bool $useCamelCase If true, private attributes will be accessed by camelCase public methods (default true).
* @return array
*/
static function getPrivateAttributes( $obj, $getterPrefix = 'get', $useCamelCase = true ) {
return self::getAttributes( $obj, self::IS_PRIVATE, $getterPrefix, $useCamelCase );
}
/**
* Set the attribute values of a object.
*
* @param array $map A map with the attribute names and values to be changed.
*
* @param object $obj The object to be changed.
*
* @param int $visibilityFlags Filter visibility flags. Can be added.
* Example: RTTI::IS_PRIVATE | RTTI::IS_PROTECTED
*
* @param string $setterPrefix The prefix for setter public methods (defaults to 'set').
*
* @param bool $useCamelCase If true, private and protected attributes will be set by
* camelCase public methods (default true).
*/
static function setAttributes(
array $map, &$obj, $visibilityFlags, $setterPrefix = 'set', $useCamelCase = true
) {
$reflectionObject = new ReflectionObject( $obj );
$currentClass = new ReflectionClass( $obj );
while ( $currentClass !== false && ! $currentClass->isInterface() ) {
$properties = $currentClass->getProperties( $visibilityFlags );
foreach ( $properties as $property ) {
$attributeName = $property->getName();
$methodName = $setterPrefix . ( $useCamelCase ? ucfirst( $attributeName ) : $attributeName );
if ( $property->isPrivate() || $property->isProtected() ) {
if ( $reflectionObject->hasMethod( $methodName ) ) {
$method = $reflectionObject->getMethod( $methodName );
if ( $method->isPublic() && array_key_exists( $attributeName, $map ) ) {
$method->invoke( $obj, $map[ $attributeName ] );
}
}
} else { // public
try {
if ( array_key_exists( $attributeName, $map ) ) {
$obj->{ $attributeName } = $map[ $attributeName ];
}
} catch (Exception $e) {
// Ignore
}
}
}
// No properties? -> try to retrieve only public properties
if ( count( $properties ) < 1 ) {
$properties = get_object_vars( $obj );
foreach ( $properties as $attributeName => $v ) {
if ( array_key_exists( $attributeName, $map ) ) {
try {
$obj->{ $attributeName } = $map[ $attributeName ];
} catch (Exception $e) {
// Ignore
}
}
}
}
$currentClass = $currentClass->getParentClass();
}
}
/**
* Set the attribute values of a object.
* This method has been kept for backward compatibility.
*
* @param array $map A map with the attribute names and values to be changed.
*
* @param object $obj The object to be changed.
*
* @param string $setterPrefix The prefix for setter public methods (defaults to 'set').
*
* @param bool $useCamelCase If true, private and protected attributes will be set by
* camelCase public methods (default true).
*/
static function setPrivateAttributes(
array $map, &$obj, $setterPrefix = 'set', $useCamelCase = true
) {
self::setAttributes( $map, $obj, RTTI::IS_PRIVATE, $setterPrefix, $useCamelCase );
}
}
?>