|
1
|
|
|
<?php namespace Unit\Chekote\Phake\Proxies; |
|
2
|
|
|
|
|
3
|
|
|
use InvalidArgumentException; |
|
4
|
|
|
use Phake_Proxies_VisibilityProxy; |
|
5
|
|
|
use ReflectionProperty; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Extends the Phake_Proxies_VisibilityProxy to allow accessing private or protected properties. |
|
9
|
|
|
*/ |
|
10
|
|
|
class VisibilityProxy extends Phake_Proxies_VisibilityProxy |
|
11
|
|
|
{ |
|
12
|
|
|
/** @var object the object being proxied */ |
|
13
|
|
|
protected $proxied; |
|
14
|
|
|
|
|
15
|
|
|
public function __construct($proxied) |
|
16
|
|
|
{ |
|
17
|
|
|
parent::__construct($proxied); |
|
18
|
|
|
|
|
19
|
|
|
$this->proxied = $proxied; |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Attempts to get the value of the specified property. |
|
24
|
|
|
* |
|
25
|
|
|
* @param string $property the name of the property to get. |
|
26
|
|
|
* @throws InvalidArgumentException if the specified property does not exist on the proxied class. |
|
27
|
|
|
* @return mixed the value of the property. |
|
28
|
|
|
*/ |
|
29
|
|
|
public function __get($property) |
|
30
|
|
|
{ |
|
31
|
|
|
return $this->makePropertyAccessible($property)->getValue($this->proxied); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Attempts to set the property name to the specified value. |
|
36
|
|
|
* |
|
37
|
|
|
* @param string $property the name of the property to set |
|
38
|
|
|
* @param mixed $value the value to set |
|
39
|
|
|
* @throws InvalidArgumentException if the specified property does not exist on the proxied class. |
|
40
|
|
|
*/ |
|
41
|
|
|
public function __set($property, $value) |
|
42
|
|
|
{ |
|
43
|
|
|
$this->makePropertyAccessible($property)->setValue($this->proxied, $value); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Ensures that the specified property on the proxied class is accessible. |
|
48
|
|
|
* |
|
49
|
|
|
* @param string $name the property to make accessible. |
|
50
|
|
|
* @throws InvalidArgumentException if the specified property does not exist on the proxied class. |
|
51
|
|
|
* @return ReflectionProperty the property. |
|
52
|
|
|
*/ |
|
53
|
|
|
protected function makePropertyAccessible($name) |
|
54
|
|
|
{ |
|
55
|
|
|
if (!property_exists($this->proxied, $name)) { |
|
56
|
|
|
throw new InvalidArgumentException( |
|
57
|
|
|
"Property $name does not exist on " . get_class($this->proxied) . '.' |
|
58
|
|
|
); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
$property = new ReflectionProperty($this->proxied, $name); |
|
62
|
|
|
$property->setAccessible(true); |
|
63
|
|
|
|
|
64
|
|
|
return $property; |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|