1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spekkionu\PropertyAccess; |
4
|
|
|
|
5
|
|
|
use OutOfBoundsException; |
6
|
|
|
|
7
|
|
|
trait PropertyAccessTrait |
8
|
|
|
{ |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Magic Getter |
12
|
|
|
* |
13
|
|
|
* @param string $name |
14
|
|
|
* |
15
|
|
|
* @return mixed |
16
|
|
|
*/ |
17
|
6 |
|
public function __get($name) |
18
|
|
|
{ |
19
|
6 |
|
if (!property_exists($this, $name)) { |
20
|
1 |
|
throw new OutOfBoundsException("Property {$name} does not exist"); |
21
|
|
|
} |
22
|
5 |
|
return $this->getValue($name); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Returns the value of a property |
27
|
|
|
* |
28
|
|
|
* @param string $name |
29
|
|
|
* |
30
|
|
|
* @return mixed |
31
|
|
|
*/ |
32
|
5 |
|
private function getValue($name) |
33
|
|
|
{ |
34
|
5 |
|
$getter = $this->getGetterMethod($name); |
35
|
5 |
|
if (method_exists($this, $getter)) { |
36
|
4 |
|
return $this->$getter(); |
37
|
|
|
} else { |
38
|
2 |
|
return $this->$name; |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Magic setter |
44
|
|
|
* |
45
|
|
|
* @param string $name |
46
|
|
|
* @param mixed $value |
47
|
|
|
*/ |
48
|
4 |
|
public function __set($name, $value) |
49
|
|
|
{ |
50
|
4 |
|
if (!property_exists($this, $name)) { |
51
|
1 |
|
throw new OutOfBoundsException("Property {$name} does not exist"); |
52
|
|
|
} |
53
|
3 |
|
$this->setValue($name, $value); |
54
|
3 |
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Sets the value of a property |
58
|
|
|
* |
59
|
|
|
* @param string $name |
60
|
|
|
* @param mixed $value |
61
|
|
|
*/ |
62
|
4 |
|
private function setValue($name, $value) |
63
|
|
|
{ |
64
|
4 |
|
$setter = $this->getSetterMethod($name); |
65
|
4 |
|
if (method_exists($this, $setter)) { |
66
|
2 |
|
$this->$setter($value); |
67
|
|
|
} else { |
68
|
3 |
|
$this->$name = $value; |
69
|
|
|
} |
70
|
4 |
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Sets properties from array |
74
|
|
|
* |
75
|
|
|
* @param array $values |
76
|
|
|
*/ |
77
|
1 |
|
public function fill(array $values) |
78
|
|
|
{ |
79
|
1 |
|
foreach ($values as $name => $value) { |
80
|
1 |
|
if (property_exists($this, $name)) { |
81
|
1 |
|
$this->setValue($name, $value); |
82
|
|
|
} |
83
|
|
|
} |
84
|
1 |
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Returns estimated getter method |
88
|
|
|
* |
89
|
|
|
* @param string $name |
90
|
|
|
* |
91
|
|
|
* @return string |
92
|
|
|
*/ |
93
|
4 |
|
protected function getSetterMethod($name) |
94
|
|
|
{ |
95
|
4 |
|
return 'set' . $this->studly($name); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Returns estimated setter method |
100
|
|
|
* |
101
|
|
|
* @param string $name |
102
|
|
|
* |
103
|
|
|
* @return string |
104
|
|
|
*/ |
105
|
5 |
|
protected function getGetterMethod($name) |
106
|
|
|
{ |
107
|
5 |
|
return 'get' . $this->studly($name); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Convert a value to studly caps case. |
112
|
|
|
* |
113
|
|
|
* @param string $value |
114
|
|
|
* |
115
|
|
|
* @return string |
116
|
|
|
*/ |
117
|
5 |
|
private function studly($value) |
118
|
|
|
{ |
119
|
5 |
|
$value = ucwords(str_replace(array('-', '_'), ' ', $value)); |
120
|
5 |
|
return str_replace(' ', '', $value); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|