1 | <?php |
||
11 | class Mutator implements MutatorContract |
||
12 | { |
||
13 | use Macroable; |
||
14 | |||
15 | /** |
||
16 | * Mutate value using provided methods. |
||
17 | * |
||
18 | * @param mixed $value |
||
19 | * @param string|array $callables |
||
20 | * @return mixed |
||
21 | * |
||
22 | * @throws \LogicException |
||
23 | */ |
||
24 | public function mutate($value, $callables) |
||
25 | { |
||
26 | if (!is_array($callables)) { |
||
27 | $callables = explode('|', $callables); |
||
28 | } |
||
29 | |||
30 | foreach ($callables as $callable) { |
||
31 | list($callable, $args) = $this->parse(trim($callable)); |
||
32 | |||
33 | $value = call_user_func_array($callable, array_merge([$value], $args)); |
||
34 | } |
||
35 | |||
36 | return $value; |
||
37 | } |
||
38 | |||
39 | /** |
||
40 | * Parse provided mutator functions. |
||
41 | * |
||
42 | * @param string $callable |
||
43 | * @return array |
||
44 | * |
||
45 | * @throws \Sofa\Eloquence\Mutator\InvalidCallableException |
||
46 | */ |
||
47 | protected function parse($callable) |
||
63 | |||
64 | /** |
||
65 | * Determine whether callable is a class method. |
||
66 | * |
||
67 | * @param string $callable |
||
68 | * @return boolean |
||
69 | */ |
||
70 | protected function isClassMethod($callable) |
||
74 | |||
75 | /** |
||
76 | * Determine whether callable is available on this instance. |
||
77 | * |
||
78 | * @param string $callable |
||
79 | * @return boolean |
||
80 | */ |
||
81 | protected function isMutatorMethod($callable) |
||
85 | |||
86 | /** |
||
87 | * Split provided string into callable and arguments. |
||
88 | * |
||
89 | * @param string $callable |
||
90 | * @return array |
||
91 | */ |
||
92 | protected function parseArgs($callable) |
||
104 | |||
105 | /** |
||
106 | * Extract and validate class method. |
||
107 | * |
||
108 | * @param string $userCallable |
||
109 | * @return callable |
||
110 | * |
||
111 | * @throws \Sofa\Eloquence\Mutator\InvalidCallableException |
||
112 | */ |
||
113 | protected function parseClassMethod($userCallable) |
||
129 | |||
130 | /** |
||
131 | * Get instance callable. |
||
132 | * |
||
133 | * @param \ReflectionMethod $method |
||
134 | * @return callable |
||
135 | * |
||
136 | * @throws \Sofa\Eloquence\Mutator\InvalidCallableException |
||
137 | */ |
||
138 | protected function getInstanceMethod(ReflectionClass $class, ReflectionMethod $method) |
||
150 | |||
151 | /** |
||
152 | * Determine whether instance can be instantiated. |
||
153 | * |
||
154 | * @param \ReflectionClass $class |
||
155 | * @return boolean |
||
156 | */ |
||
157 | protected function canInstantiate(ReflectionClass $class) |
||
167 | } |
||
168 |