1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Mindtwo\DynamicMutators\Traits; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Str; |
6
|
|
|
use Mindtwo\DynamicMutators\Exceptions\InvalidParameterException; |
7
|
|
|
use Mindtwo\DynamicMutators\Handler\MutationHandler; |
8
|
|
|
|
9
|
|
|
trait HasMakeMapper |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* Get the registered name of the component. |
13
|
|
|
* |
14
|
|
|
* @return string |
15
|
|
|
*/ |
16
|
|
|
protected static function getFacadeAccessor() |
17
|
|
|
{ |
18
|
|
|
return MutationHandler::class; |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Make new instance. |
23
|
|
|
* |
24
|
|
|
* @param array $arguments |
25
|
|
|
* |
26
|
|
|
* @throws InvalidParameterException |
27
|
|
|
* |
28
|
|
|
* @return object |
29
|
|
|
*/ |
30
|
|
|
public static function make(array $arguments = []) |
31
|
|
|
{ |
32
|
|
|
$class = static::getFacadeAccessor(); |
33
|
|
|
|
34
|
|
|
try { |
35
|
|
|
$reflection = new \ReflectionClass(static::getFacadeAccessor()); |
36
|
|
|
$parameters = $reflection->getConstructor()->getParameters(); |
37
|
|
|
} catch (\Throwable $error) { |
38
|
|
|
throw new \Exception('Wrong parameter', 0, $error); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
return new $class(...static::composeMakeParameters($parameters, $arguments)); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Compose make() parameters. |
46
|
|
|
* |
47
|
|
|
* @param array $parameters |
48
|
|
|
* @param array $arguments |
49
|
|
|
* |
50
|
|
|
* @throws InvalidParameterException |
51
|
|
|
* |
52
|
|
|
* @return array |
53
|
|
|
*/ |
54
|
|
|
protected static function composeMakeParameters(array $parameters, array $arguments = []): array |
55
|
|
|
{ |
56
|
|
|
foreach ($parameters as $key=>$parameter) { |
57
|
|
|
$name = $parameter->getName(); |
58
|
|
|
|
59
|
|
|
if (array_key_exists($name, $arguments)) { |
60
|
|
|
$value = $arguments[$name]; |
61
|
|
|
} elseif (array_key_exists(Str::snake($name), $arguments)) { |
62
|
|
|
$value = $arguments[Str::snake($name)]; |
63
|
|
|
} elseif ($parameter->isDefaultValueAvailable()) { |
64
|
|
|
$value = $parameter->getDefaultValue(); |
65
|
|
|
} else { |
66
|
|
|
throw new InvalidParameterException('Invalid parameters for make()!', 0, null, [ |
67
|
|
|
'name' => $name, |
68
|
|
|
'accessor' => static::getFacadeAccessor(), |
69
|
|
|
'parameters' => $parameters, |
70
|
|
|
'arguments' => $arguments, |
71
|
|
|
]); |
72
|
|
|
} |
73
|
|
|
$result[$parameter->getPosition()] = $value; |
|
|
|
|
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return $result ?? []; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.