1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21:
22:
23: namespace picon;
24:
25: 26: 27: 28: 29: 30:
31: class PropertyResolver
32: {
33: private function __construct()
34: {
35:
36: }
37:
38: public static function hasProperty($object, $propertyName, $reflection = null)
39: {
40: try
41: {
42: $target = self::resolveObject($object, $propertyName);
43: $property = self::getPropertyForTarget($target, $propertyName);
44: }
45: catch(Exception $ex)
46: {
47: return false;
48: }
49: return true;
50: }
51:
52: public static function get($object, $propertyName)
53: {
54: $target = self::resolveObject($object, $propertyName);
55: $property = self::getPropertyForTarget($target, $propertyName);
56: return self::getValue($property, $target);
57: }
58:
59: public static function set($object, $propertyName, &$value)
60: {
61: $target = self::resolveObject($object, $propertyName);
62: $property = self::getPropertyForTarget($target, $propertyName);
63:
64: if(method_exists($target, '__set'))
65: {
66: $name = $property->getName();
67: $target->$name = $value;
68: }
69: $property->setAccessible(true);
70:
71: return $property->setValue($target, $value);
72: }
73:
74: private static function resolveActualProperty($object, $propertyName, $reflection = null)
75: {
76: if($reflection==null)
77: {
78: $reflection = new \ReflectionClass($object);
79: }
80:
81: if($reflection->hasProperty($propertyName))
82: {
83: return $reflection->getProperty($propertyName);
84: }
85:
86: $parent = $reflection->getParentClass();
87: if($parent!=null)
88: {
89: return self::resolveActualProperty($object, $propertyName, $parent);
90: }
91: return null;
92: }
93:
94: private static function getValue(\ReflectionProperty $property, $object)
95: {
96: if(method_exists($object, '__get'))
97: {
98: $name = $property->getName();
99: return $object->$name;
100: }
101: $property->setAccessible(true);
102: return $property->getValue($object);
103: }
104:
105: private static function resolveObject($object, $propertyName)
106: {
107: $target = $object;
108: $properties = explode('.', $propertyName);
109:
110: if(count($properties)==1)
111: {
112: return $target;
113: }
114:
115: $current = 0;
116: while($current<count($properties)-1)
117: {
118: $property = self::resolveActualProperty($target, $properties[$current]);
119: $oldTarget = $target;
120: $target = self::getValue($property, $target);
121:
122: if($target==null)
123: {
124: throw new \InvalidArgumentException(sprintf("Property %s does not exist in class %s", $properties[$current], get_class($oldTarget)));
125: }
126: $current++;
127: }
128: return $target;
129: }
130:
131: private static function getPropertyForTarget($target, $propertyName)
132: {
133: $properties = explode('.', $propertyName);
134: $targetProperty = $properties[count($properties)-1];
135: $property = self::resolveActualProperty($target, $targetProperty);
136:
137: if($property==null)
138: {
139: throw new \InvalidArgumentException(sprintf("Property %s does not exist in class %s", $targetProperty, get_class($target)));
140: }
141: return $property;
142: }
143: }
144:
145: ?>
146: