1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* GpsLab component. |
5
|
|
|
* |
6
|
|
|
* @author Peter Gribanov <[email protected]> |
7
|
|
|
* @copyright Copyright (c) 2011, Peter Gribanov |
8
|
|
|
* @license http://opensource.org/licenses/MIT |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace GpsLab\Component\Payload; |
12
|
|
|
|
13
|
|
|
use GpsLab\Component\Payload\Exception\PropertyException; |
14
|
|
|
|
15
|
|
|
trait PayloadTrait |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var array |
19
|
|
|
*/ |
20
|
|
|
private $payload = []; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var string[] |
24
|
|
|
*/ |
25
|
|
|
private $properties = []; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var string[] |
29
|
|
|
*/ |
30
|
|
|
private $methods = []; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @return array |
34
|
|
|
*/ |
35
|
3 |
|
final public function payload() |
36
|
|
|
{ |
37
|
3 |
|
return $this->payload; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param array $payload |
42
|
|
|
* |
43
|
|
|
* @throws PropertyException |
44
|
|
|
*/ |
45
|
15 |
|
final protected function setPayload(array $payload) |
46
|
|
|
{ |
47
|
15 |
|
$this->analyze(); |
48
|
|
|
|
49
|
15 |
|
foreach ($payload as $name => $value) { |
50
|
13 |
|
$this->setProperty($name, $value); |
51
|
|
|
} |
52
|
|
|
|
53
|
8 |
|
$this->payload = $payload; |
54
|
8 |
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param string $name |
58
|
|
|
* @param mixed $value |
59
|
|
|
*/ |
60
|
13 |
|
private function setProperty($name, $value) |
61
|
|
|
{ |
62
|
13 |
|
if (in_array($name, $this->properties)) { |
63
|
5 |
|
$this->$name = $value; |
64
|
8 |
|
} elseif (($method = 'set'.ucfirst($name)) && in_array($method, $this->methods)) { |
65
|
1 |
|
$this->{$method}($value); |
66
|
|
|
} else { |
67
|
7 |
|
throw PropertyException::undefinedProperty($name, $this); |
68
|
|
|
} |
69
|
6 |
|
} |
70
|
|
|
|
71
|
15 |
|
private function analyze() |
72
|
|
|
{ |
73
|
15 |
|
if (!$this->properties && !$this->methods) { |
74
|
15 |
|
$ref = new \ReflectionClass($this); |
75
|
15 |
|
$this->properties = $this->getProperties($ref); |
76
|
15 |
|
$this->methods = $this->getMethods($ref); |
77
|
|
|
} |
78
|
15 |
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param \ReflectionClass $ref |
82
|
|
|
* |
83
|
|
|
* @return string[] |
84
|
|
|
*/ |
85
|
15 |
|
private function getProperties(\ReflectionClass $ref) |
86
|
|
|
{ |
87
|
15 |
|
$names = []; |
88
|
15 |
|
$properties = $ref->getProperties(\ReflectionProperty::IS_PUBLIC | \ReflectionProperty::IS_PROTECTED); |
89
|
15 |
|
foreach ($properties as $property) { |
90
|
13 |
|
$names[] = $property->name; |
91
|
|
|
} |
92
|
|
|
|
93
|
15 |
|
return $names; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param \ReflectionClass $ref |
98
|
|
|
* |
99
|
|
|
* @return string[] |
100
|
|
|
*/ |
101
|
15 |
|
private function getMethods(\ReflectionClass $ref) |
102
|
|
|
{ |
103
|
15 |
|
$names = []; |
104
|
15 |
|
$methods = $ref->getMethods(\ReflectionMethod::IS_PUBLIC | \ReflectionMethod::IS_PROTECTED); |
105
|
15 |
|
foreach ($methods as $method) { |
106
|
15 |
|
$names[] = $method->name; |
107
|
|
|
} |
108
|
|
|
|
109
|
15 |
|
return $names; |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|