83bfd8f8ad5b9f818a0fc09c67d41e1c54abdb77
[WP-Plugins/glm-member-db.git] /
1 <?php
2 namespace PayPal\Common;
3 class PPReflectionUtil {
4         
5         
6         /**
7          * @var array|ReflectionMethod[]
8          */
9         private static $propertiesRefl = array();
10         
11         /**
12          * @var array|string[]
13          */
14         private static $propertiesType = array();
15         
16         
17         /**
18          * 
19          * @param string $class
20          * @param string $propertyName
21          */
22         public static function getPropertyClass($class, $propertyName) {
23                 
24                 if (($annotations = self::propertyAnnotations($class, $propertyName)) && isset($annotations['return'])) {               
25 //                      if (substr($annotations['param'], -2) === '[]') {
26 //                              $param = substr($annotations['param'], 0, -2);
27 //                      }
28                         $param = $annotations['return'];
29                 }
30                 
31                 if(isset($param)) {
32                         $anno = explode(' ', $param);
33                         return $anno[0];
34                 } else {
35                         return 'string';
36                 }
37         }
38         
39         
40         /**
41          * @param string $class
42          * @param string $propertyName
43          * @throws RuntimeException
44          * @return string
45          */
46         public static function propertyAnnotations($class, $propertyName)
47         {
48                 $class = is_object($class) ? get_class($class) : $class;
49                 if (!class_exists('ReflectionProperty')) {
50                         throw new \RuntimeException("Property type of " . $class . "::{$propertyName} cannot be resolved");
51                 }
52         
53                 if ($annotations =& self::$propertiesType[$class][$propertyName]) {
54                         return $annotations;
55                 }
56                 
57                 if (!($refl =& self::$propertiesRefl[$class][$propertyName])) {
58                         $getter = method_exists($class, "get" . ucfirst($propertyName)) ? "get". ucfirst($propertyName)
59                                 : "get". preg_replace("/([_-\s]?([a-z0-9]+))/e", "ucwords('\\2')", $propertyName);
60                         $refl = new \ReflectionMethod($class, $getter);
61                         self::$propertiesRefl[$class][$propertyName] = $refl;
62                 }
63         
64                 // todo: smarter regexp
65                 if (!preg_match_all('~\@([^\s@\(]+)[\t ]*(?:\(?([^\n@]+)\)?)?~i', $refl->getDocComment(), $annots, PREG_PATTERN_ORDER)) {
66                         return NULL;
67                 }
68                 foreach ($annots[1] as $i => $annot) {
69                         $annotations[strtolower($annot)] = empty($annots[2][$i]) ? TRUE : rtrim($annots[2][$i], " \t\n\r)");
70                 }
71         
72                 return $annotations;
73         }
74 }