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
|
|
|
* @param string[] $required |
43
|
|
|
* |
44
|
|
|
* @throws PropertyException |
45
|
|
|
*/ |
46
|
12 |
|
final protected function setPayload(array $payload, array $required = []) |
47
|
|
|
{ |
48
|
12 |
|
if ($lost = $this->lostProperties($payload, $required)) { |
49
|
2 |
|
throw PropertyException::noRequiredProperties($lost, $this); |
50
|
|
|
} |
51
|
|
|
|
52
|
10 |
|
if (!$this->properties && !$this->methods) { |
53
|
10 |
|
$ref = new \ReflectionClass($this); |
54
|
10 |
|
$this->properties = $this->getProperties($ref); |
55
|
10 |
|
$this->methods = $this->getMethods($ref); |
56
|
10 |
|
} |
57
|
|
|
|
58
|
10 |
|
foreach ($payload as $name => $value) { |
59
|
10 |
|
$this->setProperty($name, $value); |
60
|
4 |
|
} |
61
|
|
|
|
62
|
4 |
|
$this->payload = $payload; |
63
|
4 |
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param array $payload |
67
|
|
|
* @param string[] $required |
68
|
|
|
* |
69
|
|
|
* @return string[] |
70
|
|
|
*/ |
71
|
12 |
|
private function lostProperties(array $payload, array $required = []) |
72
|
|
|
{ |
73
|
12 |
|
return !empty($required) ? array_diff($required, array_keys($payload)) : []; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param string $name |
78
|
|
|
* @param mixed $value |
79
|
|
|
*/ |
80
|
10 |
|
private function setProperty($name, $value) |
81
|
|
|
{ |
82
|
10 |
|
if (in_array($name, $this->properties)) { |
83
|
3 |
|
$this->$name = $value; |
84
|
10 |
|
} elseif (($method = 'set'.ucfirst($name)) && in_array($method, $this->methods)) { |
85
|
1 |
|
$this->{$method}($value); |
86
|
1 |
|
} else { |
87
|
6 |
|
throw PropertyException::undefinedProperty($name, $this); |
88
|
|
|
} |
89
|
4 |
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param \ReflectionClass $ref |
93
|
|
|
* |
94
|
|
|
* @return string[] |
95
|
|
|
*/ |
96
|
10 |
|
private function getProperties(\ReflectionClass $ref) |
97
|
|
|
{ |
98
|
10 |
|
$names = []; |
99
|
10 |
|
$properties = $ref->getProperties(\ReflectionProperty::IS_PUBLIC | \ReflectionProperty::IS_PROTECTED); |
100
|
10 |
|
foreach ($properties as $property) { |
101
|
9 |
|
$names[] = $property->name; |
102
|
10 |
|
} |
103
|
|
|
|
104
|
10 |
|
return $names; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param \ReflectionClass $ref |
109
|
|
|
* |
110
|
|
|
* @return string[] |
111
|
|
|
*/ |
112
|
10 |
|
private function getMethods(\ReflectionClass $ref) |
113
|
|
|
{ |
114
|
10 |
|
$names = []; |
115
|
10 |
|
$methods = $ref->getMethods(\ReflectionMethod::IS_PUBLIC | \ReflectionMethod::IS_PROTECTED); |
116
|
10 |
|
foreach ($methods as $method) { |
117
|
10 |
|
$names[] = $method->name; |
118
|
10 |
|
} |
119
|
|
|
|
120
|
10 |
|
return $names; |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|