2 namespace PayPal\Common;
3 class PPReflectionUtil {
7 * @var array|ReflectionMethod[]
9 private static $propertiesRefl = array();
14 private static $propertiesType = array();
19 * @param string $class
20 * @param string $propertyName
22 public static function getPropertyClass($class, $propertyName) {
24 if (($annotations = self::propertyAnnotations($class, $propertyName)) && isset($annotations['return'])) {
25 // if (substr($annotations['param'], -2) === '[]') {
26 // $param = substr($annotations['param'], 0, -2);
28 $param = $annotations['return'];
32 $anno = explode(' ', $param);
41 * @param string $class
42 * @param string $propertyName
43 * @throws RuntimeException
46 public static function propertyAnnotations($class, $propertyName)
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");
53 if ($annotations =& self::$propertiesType[$class][$propertyName]) {
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;
64 // todo: smarter regexp
65 if (!preg_match_all('~\@([^\s@\(]+)[\t ]*(?:\(?([^\n@]+)\)?)?~i', $refl->getDocComment(), $annots, PREG_PATTERN_ORDER)) {
68 foreach ($annots[1] as $i => $annot) {
69 $annotations[strtolower($annot)] = empty($annots[2][$i]) ? TRUE : rtrim($annots[2][$i], " \t\n\r)");