1 : <?php
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 : class PHPeas_PeaUtils
33 : {
34 :
35 : const DESCRIBE_CLASS_KEY = '!class';
36 : const METHOD_PREFIX_READ = 'get';
37 : const METHOD_PREFIX_READ_BOOL = 'is';
38 : const METHOD_PREFIX_WRITE = 'set';
39 :
40 :
41 :
42 :
43 :
44 :
45 :
46 :
47 :
48 :
49 :
50 :
51 : public static function clonePea( $peaIn )
52 : {
53 : if ( !( self::isValidPea( $peaIn ) ) )
54 : {
55 10 : throw new ReflectionException( 'Could not clone given pea since given pea is invalid' );
56 : }
57 :
58 10 : $peaDescription = self::describe( $peaIn );
59 :
60 10 : unset( $peaDescription[ self::DESCRIBE_CLASS_KEY ] );
61 :
62 10 : $peaClassName = get_class( $peaIn );
63 :
64 10 : $clonePea = new $peaClassName();
65 :
66 10 : self::populate( $clonePea, $peaDescription );
67 :
68 10 : return $clonePea;
69 : }
70 :
71 :
72 :
73 :
74 :
75 :
76 :
77 :
78 :
79 :
80 :
81 :
82 :
83 :
84 :
85 :
86 :
87 : public static function copyProperties( $srcPeaIn, $destPeaIn )
88 : {
89 : if ( !( self::isValidPea( $srcPeaIn ) ) )
90 : {
91 9 : throw new ReflectionException( 'Could not copy properties since given source pea is invalid' );
92 : }
93 :
94 : if ( !( self::isValidPea( $destPeaIn ) ) )
95 : {
96 9 : throw new ReflectionException( 'Could not copy properties since given destination pea is invalid' );
97 : }
98 :
99 9 : $srcPeaDescription = self::describe( $srcPeaIn );
100 :
101 9 : unset( $srcPeaDescription[ self::DESCRIBE_CLASS_KEY ] );
102 :
103 9 : foreach ( $srcPeaDescription as $propertyName => $propertyValue )
104 : {
105 : $destWriteMethod = self::getWriteMethod(
106 : $destPeaIn,
107 : $propertyName
108 : );
109 :
110 : if ( null != $destWriteMethod )
111 : {
112 : $destPeaIn->$destWriteMethod( $propertyValue );
113 : }
114 : }
115 9 : }
116 :
117 :
118 :
119 :
120 :
121 :
122 :
123 :
124 :
125 :
126 :
127 :
128 :
129 :
130 :
131 :
132 : public static function describe( $peaIn )
133 : {
134 1 : if ( !( self::isValidPea( $peaIn ) ) )
135 : {
136 8 : throw new ReflectionException( 'Could not describe given pea since given pea is invalid' );
137 : }
138 :
139 1 : $peaDesc = array();
140 :
141 1 : $peaDesc[ self::DESCRIBE_CLASS_KEY ] = 'class ' . get_class( $peaIn );
142 :
143 1 : $reflectedPea = new ReflectionClass( get_class( $peaIn ) );
144 :
145 1 : $readMethodPattern = '/^(' . self::METHOD_PREFIX_READ_BOOL . '|' .
146 : self::METHOD_PREFIX_READ . ')/';
147 :
148 1 : foreach ( $reflectedPea->getMethods() as $reflectedMethod )
149 : {
150 : if ( $reflectedMethod->isPublic() &&
151 : preg_match( $readMethodPattern, $reflectedMethod->getName() ) )
152 : {
153 :
154 : $propertyName = $reflectedMethod->getName();
155 :
156 : $propertyName = preg_replace( $readMethodPattern, '', $propertyName );
157 :
158 : $firstLetter = $propertyName{ 0 };
159 :
160 : $propertyName = strtolower( $firstLetter ) . substr( $propertyName, 1 );
161 :
162 : $peaDesc[ $propertyName ] = $reflectedMethod->invoke( $peaIn, null );
163 : }
164 : }
165 :
166 1 : $reflectedPea = null;
167 :
168 1 : return $peaDesc;
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 :
216 :
217 :
218 :
219 :
220 :
221 :
222 :
223 : public static function getReadMethod( $peaIn, $propertyNameIn )
224 : {
225 : if ( !( self::isValidPea( $peaIn ) ) )
226 : {
227 : throw new ReflectionException( 'No read methods can be found since given pea is invalid' );
228 : }
229 :
230 : if ( null == $propertyNameIn || '' == $propertyNameIn )
231 : {
232 : throw new ReflectionException( 'No read methods can be found since given property name is invalid' );
233 : }
234 :
235 : $readMethod = null;
236 :
237 : $possibleReadMethods = array();
238 :
239 : $possibleReadMethods[] = self::METHOD_PREFIX_READ_BOOL . ucfirst( $propertyNameIn );
240 : $possibleReadMethods[] = self::METHOD_PREFIX_READ . ucfirst( $propertyNameIn );
241 :
242 : foreach ( $possibleReadMethods as $inferredMethodName )
243 : {
244 : if ( self::publicMethodExists( $peaIn, $inferredMethodName ) )
245 : {
246 : $readMethod = $inferredMethodName;
247 :
248 : break;
249 : }
250 : }
251 :
252 : return $readMethod;
253 : }
254 :
255 :
256 :
257 :
258 :
259 :
260 :
261 :
262 :
263 :
264 :
265 : public static function getWriteMethod( $peaIn, $propertyNameIn )
266 : {
267 : if ( !( self::isValidPea( $peaIn ) ) )
268 : {
269 : throw new ReflectionException( 'No write methods can be found since given pea is invalid' );
270 : }
271 :
272 : if ( null == $propertyNameIn || '' == $propertyNameIn )
273 : {
274 : throw new ReflectionException( 'No write methods can be found since given property name is invalid' );
275 : }
276 :
277 : $writeMethod = null;
278 :
279 : $inferredMethodName = self::METHOD_PREFIX_WRITE . ucfirst( $propertyNameIn );
280 :
281 : if ( self::publicMethodExists( $peaIn, $inferredMethodName ) )
282 : {
283 : $writeMethod = $inferredMethodName;
284 : }
285 :
286 : return $writeMethod;
287 : }
288 :
289 :
290 :
291 :
292 :
293 :
294 :
295 :
296 :
297 :
298 : public static function invokeMethod( $peaIn, $methodNameIn, $argsIn = null )
299 : {
300 : if ( !( self::isValidPea( $peaIn ) ) )
301 : {
302 5 : throw new ReflectionException( 'Could not invoke method on given pea since given pea is invalid' );
303 : }
304 :
305 : if ( null == $methodNameIn || '' == $methodNameIn )
306 : {
307 5 : throw new ReflectionException( 'Could not invoke method on given pea since given method name is invalid' );
308 : }
309 :
310 : $reflectedPea = new ReflectionClass( get_class( $peaIn ) );
311 :
312 : try
313 : {
314 : $reflectedMethod = $reflectedPea->getMethod( $methodNameIn );
315 : }
316 : catch ( ReflectionException $re )
317 : {
318 5 : throw new ReflectionException( 'Could not invoke method since given method name does not exist in given pea' );
319 : }
320 :
321 : return $reflectedMethod->invoke( $peaIn, $argsIn );
322 : }
323 :
324 :
325 :
326 :
327 :
328 :
329 :
330 :
331 :
332 :
333 :
334 : public static function invokeStaticMethod( $peaIn, $methodNameIn, $argsIn = null )
335 : {
336 :
337 : if ( !( self::isValidPea( $peaIn ) ) )
338 : {
339 4 : throw new ReflectionException( 'Could not invoke static method on given pea since given pea is invalid' );
340 : }
341 :
342 : if ( null == $methodNameIn || '' == $methodNameIn )
343 : {
344 4 : throw new ReflectionException( 'Could not invoke static method on given pea since given method name is invalid' );
345 : }
346 :
347 : $reflectedPea = new ReflectionClass( get_class( $peaIn ) );
348 :
349 : try
350 : {
351 : $reflectedMethod = $reflectedPea->getMethod( $methodNameIn );
352 : }
353 : catch ( ReflectionException $re )
354 : {
355 4 : throw new ReflectionException( 'Could not invoke static method since given method name does ' .
356 : 'not exist in given pea' );
357 : }
358 :
359 4 : if ( !( $reflectedMethod->isStatic() ) )
360 : {
361 4 : throw new ReflectionException( 'Could not invoke static method since given method is not actually static' );
362 : }
363 :
364 : return $reflectedMethod->invoke( null, $argsIn );
365 : }
366 :
367 :
368 :
369 :
370 :
371 :
372 :
373 :
374 :
375 :
376 : public static function populate( $peaIn, $propertiesIn )
377 : {
378 7 : if ( !( self::isValidPea( $peaIn ) ) )
379 : {
380 3 : throw new ReflectionException( 'Could not populate given pea since given pea is invalid' );
381 : }
382 :
383 7 : if ( null == $propertiesIn || !( is_array( $propertiesIn ) ) )
384 : {
385 : throw new ReflectionException( 'Could not populate given pea since given properties array is invalid' );
386 : }
387 :
388 7 : foreach ( $propertiesIn as $propertyName => $propertyValue )
389 : {
390 : $methodName = self::getWriteMethod( $peaIn, $propertyName );
391 :
392 : if ( null != $methodName )
393 : {
394 : $peaIn->$methodName( $propertyValue );
395 : }
396 : }
397 7 : }
398 :
399 :
400 :
401 :
402 :
403 :
404 :
405 :
406 :
407 :
408 :
409 : public static function isValidPea( $peaIn )
410 : {
411 : $validPea = false;
412 :
413 : if ( null != $peaIn && is_object( $peaIn ) )
414 : {
415 : $class = new ReflectionClass( get_class( $peaIn ) );
416 :
417 : $constructor = $class->getConstructor();
418 :
419 : if ( ( null == $constructor ) ||
420 : ( $constructor->isConstructor() &&
421 : $constructor->isPublic() &&
422 : 0 == $constructor->getNumberOfRequiredParameters() ) )
423 : {
424 : $validPea = true;
425 : }
426 : }
427 :
428 : return $validPea;
429 : }
430 :
431 :
432 :
433 :
434 :
435 :
436 :
437 :
438 :
439 :
440 : public static function publicMethodExists( $peaIn, $methodNameIn )
441 : {
442 : $result = false;
443 :
444 : $reflectedClass = new ReflectionClass( get_class( $peaIn ) );
445 :
446 : $reflectedMethod = null;
447 :
448 : try
449 : {
450 : $reflectedMethod = $reflectedClass->getMethod( $methodNameIn );
451 : }
452 : catch ( ReflectionException $re )
453 : {
454 : $reflectedMethod = null;
455 : }
456 :
457 : if ( null != $reflectedMethod && $reflectedMethod->isPublic() )
458 : {
459 : $result = true;
460 : }
461 :
462 : $reflectedMethod = null;
463 : $reflectedClass = null;
464 :
465 : return $result;
466 : }
467 : }
468 :
469 : ?>
|