1 | <?php |
||
11 | class MutationHandler implements MutationHandlerInterface |
||
12 | { |
||
13 | /** |
||
14 | * Mutator name. |
||
15 | * |
||
16 | * @var string |
||
17 | */ |
||
18 | protected $name; |
||
19 | |||
20 | /** |
||
21 | * Related model. |
||
22 | * |
||
23 | * @var Model |
||
24 | */ |
||
25 | protected $model; |
||
26 | |||
27 | /** |
||
28 | * Mutator settings. |
||
29 | * |
||
30 | * @var array |
||
31 | */ |
||
32 | protected $mutators = []; |
||
33 | |||
34 | /** |
||
35 | * Mutation handler constructor. |
||
36 | * |
||
37 | * @param string $name |
||
38 | */ |
||
39 | public function __construct(string $name, array $getMutator = [], array $setMutator = []) |
||
51 | |||
52 | /** |
||
53 | * Set related model. |
||
54 | * |
||
55 | * @param $model |
||
56 | * |
||
57 | * @return MutationHandler |
||
58 | */ |
||
59 | public function setModel(&$model): self |
||
65 | |||
66 | /** |
||
67 | * Get mutation handler name. |
||
68 | * |
||
69 | * @return string |
||
70 | */ |
||
71 | public function getName(): string |
||
75 | |||
76 | /** |
||
77 | * Get mutator config. |
||
78 | * |
||
79 | * @param string $operator |
||
80 | * @param string $key |
||
81 | * |
||
82 | * @return mixed |
||
83 | */ |
||
84 | protected function config(string $operator, string $key = null) |
||
90 | |||
91 | /** |
||
92 | * Register mutator. |
||
93 | * |
||
94 | * @param string $operator |
||
95 | * @param callable|string $callable |
||
96 | * @param bool $stack |
||
97 | * @param string|null $property |
||
98 | * |
||
99 | * @return MutationHandler |
||
100 | */ |
||
101 | protected function registerMutator(string $operator, $callable, bool $stack = false, string $property = null): self |
||
111 | |||
112 | /** |
||
113 | * Register set mutator. |
||
114 | * |
||
115 | * @param callable|string $callable |
||
116 | * @param bool $stack |
||
117 | * @param string|null $property |
||
118 | * |
||
119 | * @return MutationHandler |
||
120 | */ |
||
121 | public function registerSetMutator($callable, bool $stack = false, string $property = null): self |
||
125 | |||
126 | /** |
||
127 | * Register get mutator. |
||
128 | * |
||
129 | * @param callable|string $callable |
||
130 | * @param bool $stack |
||
131 | * @param string|null $property |
||
132 | * |
||
133 | * @return MutationHandler |
||
134 | */ |
||
135 | public function registerGetMutator($callable, bool $stack = false, string $property = null): self |
||
139 | |||
140 | /** |
||
141 | * Return a valid mutator name or throw an exceptions. |
||
142 | * |
||
143 | * @param string $name |
||
144 | * @param string $operator |
||
145 | * |
||
146 | * @throws DynamicMutatorNotDefinedException |
||
147 | * |
||
148 | * @return string |
||
149 | */ |
||
150 | protected function mutatorNameOrFail(string $name, string $operator): string |
||
162 | |||
163 | /** |
||
164 | * Get callable or throw an exception. |
||
165 | * |
||
166 | * @param $callable |
||
167 | * |
||
168 | * @throws DynamicMutatorNotCallableException |
||
169 | * |
||
170 | * @return callable |
||
171 | */ |
||
172 | protected function callableOrFail(string $operator): callable |
||
189 | |||
190 | /** |
||
191 | * Determinate if a mutator exists. |
||
192 | * |
||
193 | * @param string $name |
||
194 | * @param string $operator |
||
195 | * |
||
196 | * @return bool |
||
197 | */ |
||
198 | protected function hasMutator(string $name, string $operator): bool |
||
203 | |||
204 | /** |
||
205 | * Determinate if a get mutator exists. |
||
206 | * |
||
207 | * @param string $name |
||
208 | * |
||
209 | * @return bool |
||
210 | */ |
||
211 | public function hasGetMutator(string $name): bool |
||
215 | |||
216 | /** |
||
217 | * Determinate if a set mutator exists. |
||
218 | * |
||
219 | * @param string $name |
||
220 | * |
||
221 | * @return bool |
||
222 | */ |
||
223 | public function hasSetMutator(string $name): bool |
||
227 | |||
228 | protected function getMutatorProperties(string $operator): Collection |
||
232 | |||
233 | public function callGetMutator(string $name) |
||
243 | |||
244 | public function callSetMutator(string $name, $value) |
||
255 | |||
256 | /** |
||
257 | * Determinate if the next mutator should be called. |
||
258 | * |
||
259 | * @param $operator |
||
260 | * |
||
261 | * @return bool |
||
262 | */ |
||
263 | public function shouldStack($operator): bool |
||
267 | } |
||
268 |