Hoa central
Handler.php
Go to the documentation of this file.
1 <?php
2 
37 namespace Hoa\Praspel\Preambler;
38 
39 use Hoa\Core;
40 use Hoa\Praspel;
41 
50 class Handler
51 {
57  protected $__callable = null;
58 
59 
60 
67  public function __construct(Core\Consistency\Xcallable $callable)
68  {
69  $this->__setCallable($callable);
70 
71  return;
72  }
73 
81  public function __getReflectionObject(&$object)
82  {
83  static $_out = null;
84  static $_object = null;
85 
86  if (null === $_out) {
87  $callback = $this->__getCallable()->getValidCallback();
88 
89  if (!is_object($callback[0])) {
91  'Callable %s is not an object.',
92  0,
93  $this->__getCallable()
94  );
95  }
96 
97  $_object = $callback[0];
98  $_out = new \ReflectionObject($_object);
99  }
100 
101  $object = $_object;
102 
103  return $_out;
104  }
105 
114  public function __set($name, $value)
115  {
116  $reflectionObject = $this->__getReflectionObject($object);
117 
118  if (false === $reflectionObject->hasProperty($name)) {
119  throw new Praspel\Exception\Preambler(
120  'Attribute %s on object %s does not exist, cannot set it.',
121  1,
122  [$name, $reflectionObject->getName()]
123  );
124  }
125 
126  $attribute = $reflectionObject->getProperty($name);
127  $attribute->setAccessible(true);
128  $attribute->setValue($object, $value);
129 
130  return $this;
131  }
132 
140  public function __get($name)
141  {
142  $reflectionObject = $this->__getReflectionObject($object);
143 
144  if (false === $reflectionObject->hasProperty($name)) {
145  throw new Praspel\Exception\Preambler(
146  'Attribute %s on object %s does not exist, cannot get it.',
147  2,
148  [$name, $reflectionObject->getName()]
149  );
150  }
151 
152  $attribute = $reflectionObject->getProperty($name);
153  $attribute->setAccessible(true);
154 
155  return $attribute->getValue($object);
156  }
157 
164  public function __setCallable(Core\Consistency\Xcallable $callable)
165  {
166  $old = $this->__callable;
167  $this->__callable = $callable;
168 
169  return $old;
170  }
171 
177  public function __getCallable()
178  {
179  return $this->__callable;
180  }
181 }
__construct(Core\Consistency\Xcallable $callable)
Definition: Handler.php:67
__setCallable(Core\Consistency\Xcallable $callable)
Definition: Handler.php:164