ReflectionPropertyAccess::set()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
c 0
b 0
f 0
rs 10
ccs 2
cts 2
cp 1
cc 1
eloc 1
nc 1
nop 2
crap 1
1
<?php
2
/**
3
 * Created by gerk on 13.11.17 05:50
4
 */
5
6
namespace PeekAndPoke\Component\PropertyAccess;
7
8
/**
9
 * This implementation accesses properties through reflection.
10
 *
11
 * This is a medium speed accessor which less limitation. It will also be the most used one since most
12
 * class properties are protected or private.
13
 *
14
 *
15
 * What can it do?
16
 *
17
 * - access public properties (you should use PublicPropertyAccess instead for performance)
18
 *
19
 * - access protected properties that are visible on the subject class
20
 *
21
 * - access private properties declared on the subject class
22
 *
23
 * What can it NOT do?
24
 *
25
 * - access private properties that are declared on a base class of the subject class
26
 *  -> use ScopedPropertyAccess then which is the slowest accessor
27
 *
28
 *
29
 * @author Karsten J. Gerber <[email protected]>
30
 */
31
class ReflectionPropertyAccess implements PropertyAccess
32
{
33
    /** @var string */
34
    private $className;
35
    /** @var string */
36
    private $propertyName;
37
38
    /** @var \ReflectionProperty */
39
    private $_prop;
40
41
    /**
42
     * @param \ReflectionClass $class
43
     * @param string           $propertyName The name of the property
44
     *
45
     * @return ReflectionPropertyAccess
46
     */
47 49
    public static function create(\ReflectionClass $class, $propertyName)
48
    {
49 49
        $ret               = new self;
50 49
        $ret->className    = $class->name;
51 49
        $ret->propertyName = $propertyName;
52
53 49
        $ret->__wakeup();
54
55 49
        return $ret;
56
    }
57
58
    /**
59
     * Make use of the static create method
60
     */
61
    private function __construct() { }
62
63
    /**
64
     * @return array
65
     */
66 1
    public function __sleep()
67
    {
68 1
        return ['className', 'propertyName'];
69
    }
70
71
    /**
72
     *
73
     */
74 49
    public function __wakeup()
75
    {
76
        // little bit of caching
77 49
        $class = new \ReflectionClass($this->className);
78
79 49
        $this->_prop = $class->getProperty($this->propertyName);
80 49
        $this->_prop->setAccessible(true);
81 49
    }
82
83
    /**
84
     * Get the value of the property
85
     *
86
     * @param mixed $subject The object to get the value from
87
     *
88
     * @return mixed
89
     */
90 14
    public function get($subject)
91
    {
92 14
        return $this->_prop->getValue($subject);
93
    }
94
95
    /**
96
     * Set the value of the property
97
     *
98
     * @param mixed $subject The object to set the value to
99
     * @param mixed $value
100
     */
101 11
    public function set($subject, $value)
102
    {
103 11
        $this->_prop->setValue($subject, $value);
104 11
    }
105
}
106