Completed
Push — master ( 8c9cd6...ef07c2 )
by Adam
08:13
created

AbstractReflection::getAccessibleProperty()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 2
cts 2
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
crap 1
1
<?php
2
3
namespace BestServedCold\PhalueObjects\Utility\Reflection;
4
5
/**
6
 * Class AbstractReflection
7
 *
8
 * @package   BestServedCold\PhalueObjects\Utility\Reflection
9
 * @author    Adam Lewis <[email protected]>
10
 * @copyright Copyright (c) 2015 Best Served Cold Media Limited
11
 * @license	  http://http://opensource.org/licenses/GPL-3.0 GPL License
12
 * @link	  http://bestservedcold.com
13
 * @since	  0.0.1-alpha
14
 * @version   0.0.2-alpha
15
 */
16
abstract class AbstractReflection
17
{
18
    /**
19
     * @var string
20
     */
21
    protected $class;
22
23
    /**
24
     * @var object
25
     */
26
    protected $object;
27
28
    /**
29
     * __get.
30
     *
31
     * Overloading method to get any $key property from $this->class or if not
32
     * static, from $this->object.
33
     *
34
     * @param  $key
35
     * @return \ReflectionProperty
36
     */
37 3
    public function __get($key)
38
    {
39 3
        $property = $this->getAccessibleProperty($key);
40 3
        return $property->getValue($property->isStatic() ?: $this->object);
41
    }
42 3
43 3
    /**
44 3
     * __set.
45
     *
46
     * Overloading method to set any $key property to $this->class or if not static,
47
     * to $this->object.
48
     *
49
     * @param  $key
50
     * @param  $value
51
     * @return $this
52
     */
53
    public function __set($key, $value)
54
    {
55
        $property = $this->getAccessibleProperty($key);
56
        $property->isStatic()
57 3
            ? $property->setValue($value)
58
            : $property->setValue($this->object, $value);
59 3
60 3
        return $this;
61
    }
62 3
63 3
    /**
64 3
     * __call.
65
     *
66 3
     * Overloading method to call the $name method from $this->class and if not
67
     * static, using $this->object.
68
     *
69
     * @param  $name
70
     * @param  $args
71
     *
72
     * @return \ReflectionMethod
73
     */
74
    public function __call($name, $args)
75
    {
76
        $method = $this->getAccessibleMethod($name);
77
        return $method->invokeArgs($method->isStatic() ? null : $this->object, $args);
78
    }
79
80 2
    /**
81
     * Get Accessible Property
82 2
     *
83 2
     * @param $key
84
     * @return \ReflectionProperty
85 2
     */
86 2
    private function getAccessibleProperty($key)
87 2
    {
88
        $property = new \ReflectionProperty($this->class, $key);
89
        $property->setAccessible(true);
90
        return $property;
91
    }
92
93
    /**
94
     * Get Accessible Method
95
     *
96
     * @param $key
97
     * @return \ReflectionMethod
98
     */
99
    private function getAccessibleMethod($key)
100
    {
101
        $method = new \ReflectionMethod($this->class, $key);
102
        $method->setAccessible(true);
103
        return $method;
104
    }
105
106
}
107