1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Mindtwo\DynamicMutators\Traits; |
4
|
|
|
|
5
|
|
|
use Mindtwo\DynamicMutators\Exceptions\InvalidParameterException; |
6
|
|
|
use Mindtwo\DynamicMutators\Handler\MutationHandler; |
7
|
|
|
|
8
|
|
|
trait HasMakeMapper |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* Get the registered name of the component. |
12
|
|
|
* |
13
|
|
|
* @return string |
14
|
|
|
*/ |
15
|
|
|
protected static function getFacadeAccessor() |
16
|
|
|
{ |
17
|
|
|
return MutationHandler::class; |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Make new instance. |
22
|
|
|
* |
23
|
|
|
* @param array $arguments |
24
|
|
|
* |
25
|
|
|
* @throws InvalidParameterException |
26
|
|
|
* |
27
|
|
|
* @return object |
28
|
|
|
*/ |
29
|
|
|
public static function make(array $arguments = []) |
30
|
|
|
{ |
31
|
|
|
$class = static::getFacadeAccessor(); |
32
|
|
|
|
33
|
|
|
try { |
34
|
|
|
$reflection = new \ReflectionClass(static::getFacadeAccessor()); |
35
|
|
|
$parameters = $reflection->getConstructor()->getParameters(); |
36
|
|
|
} catch (\Throwable $error) { |
37
|
|
|
throw new \Exception('Wrong parameter', 0, $error); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
return new $class(...static::composeMakeParameters($parameters, $arguments)); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Compose make() parameters. |
45
|
|
|
* |
46
|
|
|
* @param array $parameters |
47
|
|
|
* @param array $arguments |
48
|
|
|
* |
49
|
|
|
* @throws InvalidParameterException |
50
|
|
|
* |
51
|
|
|
* @return array |
52
|
|
|
*/ |
53
|
|
|
protected static function composeMakeParameters(array $parameters, array $arguments = []): array |
54
|
|
|
{ |
55
|
|
|
foreach ($parameters as $key=>$parameter) { |
56
|
|
|
$name = $parameter->getName(); |
57
|
|
|
|
58
|
|
|
if (array_key_exists($name, $arguments)) { |
59
|
|
|
$value = $arguments[$name]; |
60
|
|
|
} elseif (array_key_exists(snake_case($name), $arguments)) { |
61
|
|
|
$value = $arguments[snake_case($name)]; |
62
|
|
|
} elseif ($parameter->isDefaultValueAvailable()) { |
63
|
|
|
$value = $parameter->getDefaultValue(); |
64
|
|
|
} else { |
65
|
|
|
throw new InvalidParameterException('Invalid parameters for make()!', 0, null, [ |
66
|
|
|
'name' => $name, |
67
|
|
|
'accessor' => static::getFacadeAccessor(), |
68
|
|
|
'parameters' => $parameters, |
69
|
|
|
'arguments' => $arguments, |
70
|
|
|
]); |
71
|
|
|
} |
72
|
|
|
$result[$parameter->getPosition()] = $value; |
|
|
|
|
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return $result ?? []; |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
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.