1 <?php
2
3 4 5 6 7 8 9 10
11
12 namespace Grido\PropertyAccessors;
13
14 15 16 17 18 19 20
21 class ArrayObjectAccessor implements IPropertyAccessor
22 {
23 24 25 26 27 28 29
30 public function getProperty($object, $name)
31 {
32 if (is_array($object)) {
33 if (!array_key_exists($name, $object)) {
34 throw new PropertyAccessorException("Property with name '$name' does not exists in datasource.");
35 }
36
37 return $object[$name];
38
39 } elseif (is_object($object)) {
40 if (\Nette\Utils\Strings::contains($name, '.')) {
41 $parts = explode('.', $name);
42 foreach ($parts as $item) {
43 if (is_object($object)) {
44 $object = $object->$item;
45 }
46 }
47
48 return $object;
49 }
50
51 return $object->$name;
52
53 } else {
54 throw new \InvalidArgumentException('Please implement your own property accessor.');
55 }
56 }
57
58 59 60 61 62 63
64 public function setProperty($object, $name, $value)
65 {
66 if (is_array($object)) {
67 $object[$name] = $value;
68 } elseif (is_object($object)) {
69 $object->$name = $value;
70 } else {
71 throw new \InvalidArgumentException('Please implement your own property accessor.');
72 }
73 }
74 }
75