Grido@master
  • Namespace
  • Class
  • Tree
  • Deprecated
  • Todo
  • Download

Namespaces

  • Grido
    • Components
      • Actions
      • Columns
      • Filters
    • DataSources
    • PropertyAccessors
    • Translations

Classes

  • Grido\Translations\FileTranslator
 1 <?php
 2 
 3 /**
 4  * This file is part of the Grido (https://github.com/o5/grido)
 5  *
 6  * Copyright (c) 2011 Petr Bugyík (http://petr.bugyik.cz)
 7  *
 8  * For the full copyright and license information, please view
 9  * the file LICENSE.md that was distributed with this source code.
10  */
11 
12 namespace Grido\PropertyAccessors;
13 
14 /**
15  * Accessor for array and object structures.
16  *
17  * @package     Grido
18  * @subpackage  PropertyAccessors
19  * @author      Josef Kříž <pepakriz@gmail.com>
20  */
21 class ArrayObjectAccessor implements IPropertyAccessor
22 {
23     /**
24      * @param mixed $object
25      * @param string $name
26      * @throws PropertyAccessorException
27      * @throws \InvalidArgumentException
28      * @return mixed
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      * @param mixed $object
60      * @param string $name
61      * @param mixed $value
62      * @throws \InvalidArgumentException
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 
Grido@master API documentation generated by ApiGen