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 * Symfony property accessor.
16 *
17 * @package Grido
18 * @subpackage PropertyAccessors
19 * @author Josef Kříž <pepakriz@gmail.com>
20 * @link http://symfony.com/doc/current/components/property_access/introduction.html
21 */
22 class SymfonyPropertyAccessor implements IPropertyAccessor
23 {
24 /** @var \Symfony\Component\PropertyAccess\PropertyAccessor */
25 private $propertyAccessor;
26
27 /**
28 * @param mixed $object
29 * @param string $name
30 * @return mixed
31 * @throws PropertyAccessorException
32 */
33 public function getProperty($object, $name)
34 {
35 try {
36 return $this->getPropertyAccessor()->getValue($object, $name);
37 } catch (\Exception $e) {
38 throw new PropertyAccessorException("Property with name '$name' does not exists in datasource.", 0, $e);
39 }
40 }
41
42 /**
43 * @param mixed $object
44 * @param string $name
45 * @param mixed $value
46 * @throws PropertyAccessorException
47 */
48 public function setProperty($object, $name, $value)
49 {
50 try {
51 $this->getPropertyAccessor()->setValue($object, $name, $value);
52 } catch (\Exception $e) {
53 throw new PropertyAccessorException("Property with name '$name' does not exists in datasource.", 0, $e);
54 }
55 }
56
57 /**
58 * @return \Symfony\Component\PropertyAccess\PropertyAccessor
59 */
60 private function getPropertyAccessor()
61 {
62 if ($this->propertyAccessor === NULL) {
63 $this->propertyAccessor = new \Symfony\Component\PropertyAccess\PropertyAccessor(TRUE, TRUE);
64 }
65
66 return $this->propertyAccessor;
67 }
68 }
69