1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Cerbero\Dto\Manipulators; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* The DTO listener. |
7
|
|
|
* |
8
|
|
|
*/ |
9
|
|
|
class Listener |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* The listener instance. |
13
|
|
|
* |
14
|
|
|
* @var self |
15
|
|
|
*/ |
16
|
|
|
protected static $instance; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* The listeners map. |
20
|
|
|
* |
21
|
|
|
* @var array |
22
|
|
|
*/ |
23
|
|
|
protected $listenersMap = []; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* The cached DTO listeners. |
27
|
|
|
* |
28
|
|
|
* @var array |
29
|
|
|
*/ |
30
|
|
|
protected $cachedListeners = []; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Instantiate the class |
34
|
|
|
*/ |
35
|
6 |
|
protected function __construct() |
36
|
|
|
{ |
37
|
|
|
// |
38
|
6 |
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Retrieve the listener instance |
42
|
|
|
* |
43
|
|
|
* @return self |
44
|
|
|
*/ |
45
|
36 |
|
public static function instance(): self |
46
|
|
|
{ |
47
|
36 |
|
return static::$instance = static::$instance ?: new static(); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Set the listeners map |
52
|
|
|
* |
53
|
|
|
* @param array $listenersMap |
54
|
|
|
* @return self |
55
|
|
|
*/ |
56
|
5 |
|
public function listen(array $listenersMap): self |
57
|
|
|
{ |
58
|
5 |
|
$this->listenersMap = $listenersMap; |
59
|
|
|
|
60
|
5 |
|
return $this; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Add a listener for the given DTO |
65
|
|
|
* |
66
|
|
|
* @param string $dtoClass |
67
|
|
|
* @param string $listener |
68
|
|
|
* @return self |
69
|
|
|
*/ |
70
|
1 |
|
public function addListener(string $dtoClass, string $listener): self |
71
|
|
|
{ |
72
|
1 |
|
$this->listenersMap[$dtoClass] = $listener; |
73
|
|
|
|
74
|
1 |
|
return $this; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Remove the listener of the given DTO |
79
|
|
|
* |
80
|
|
|
* @param string $dtoClass |
81
|
|
|
* @return self |
82
|
|
|
*/ |
83
|
1 |
|
public function removeListener(string $dtoClass): self |
84
|
|
|
{ |
85
|
1 |
|
unset($this->listenersMap[$dtoClass]); |
86
|
|
|
|
87
|
1 |
|
return $this; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Retrieve the listeners map |
92
|
|
|
* |
93
|
|
|
* @return array |
94
|
|
|
*/ |
95
|
1 |
|
public function getListeners(): array |
96
|
|
|
{ |
97
|
1 |
|
return $this->listenersMap; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Call the DTO listener when retrieving a value |
102
|
|
|
* |
103
|
|
|
* @param string $dtoClass |
104
|
|
|
* @param string $property |
105
|
|
|
* @param mixed $value |
106
|
|
|
* @return mixed |
107
|
|
|
*/ |
108
|
28 |
|
public function getting(string $dtoClass, string $property, $value) |
109
|
|
|
{ |
110
|
28 |
|
$method = 'get' . str_replace('_', '', ucwords($property, '_')); |
111
|
|
|
|
112
|
28 |
|
return $this->callListenerOrReturnValue($dtoClass, $method, $value); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Retrieve the result of the given listener method or the provided value |
117
|
|
|
* |
118
|
|
|
* @param string $dtoClass |
119
|
|
|
* @param string $method |
120
|
|
|
* @param mixed $value |
121
|
|
|
* @return mixed |
122
|
|
|
*/ |
123
|
34 |
|
protected function callListenerOrReturnValue(string $dtoClass, string $method, $value) |
124
|
|
|
{ |
125
|
34 |
|
$listener = $this->listenersMap[$dtoClass] ?? new \stdClass(); |
126
|
|
|
|
127
|
34 |
|
if (!method_exists($listener, $method)) { |
128
|
30 |
|
return $value; |
129
|
4 |
|
} elseif (empty($this->cachedListeners[$dtoClass])) { |
130
|
3 |
|
$this->cachedListeners[$dtoClass] = $this->resolveListener($listener); |
|
|
|
|
131
|
|
|
} |
132
|
|
|
|
133
|
4 |
|
return $this->cachedListeners[$dtoClass]->$method($value); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Retrieve the instance of the given listener |
138
|
|
|
* |
139
|
|
|
* @param string $listener |
140
|
|
|
* @return mixed |
141
|
|
|
*/ |
142
|
3 |
|
protected function resolveListener(string $listener) |
143
|
|
|
{ |
144
|
3 |
|
return new $listener(); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Call the DTO listener when setting a value |
149
|
|
|
* |
150
|
|
|
* @param string $dtoClass |
151
|
|
|
* @param string $property |
152
|
|
|
* @param mixed $value |
153
|
|
|
* @return mixed |
154
|
|
|
*/ |
155
|
15 |
|
public function setting(string $dtoClass, string $property, $value) |
156
|
|
|
{ |
157
|
15 |
|
$method = 'set' . str_replace('_', '', ucwords($property, '_')); |
158
|
|
|
|
159
|
15 |
|
return $this->callListenerOrReturnValue($dtoClass, $method, $value); |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|