|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace League\Container\Inflector; |
|
6
|
|
|
|
|
7
|
|
|
use League\Container\Argument\ArgumentResolverInterface; |
|
8
|
|
|
use League\Container\Argument\ArgumentResolverTrait; |
|
9
|
|
|
use League\Container\ContainerAwareTrait; |
|
10
|
|
|
|
|
11
|
|
|
class Inflector implements ArgumentResolverInterface, InflectorInterface |
|
12
|
|
|
{ |
|
13
|
|
|
use ArgumentResolverTrait; |
|
14
|
|
|
use ContainerAwareTrait; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @var string |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $type; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var callable|null |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $callback; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var array |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $methods = []; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var array |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $properties = []; |
|
35
|
|
|
|
|
36
|
27 |
|
public function __construct(string $type, ?callable $callback = null) |
|
37
|
|
|
{ |
|
38
|
27 |
|
$this->type = $type; |
|
39
|
27 |
|
$this->callback = $callback; |
|
40
|
27 |
|
} |
|
41
|
|
|
|
|
42
|
9 |
|
public function getType(): string |
|
43
|
|
|
{ |
|
44
|
9 |
|
return $this->type; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
9 |
|
public function invokeMethod(string $name, array $args): InflectorInterface |
|
48
|
|
|
{ |
|
49
|
9 |
|
$this->methods[$name] = $args; |
|
50
|
9 |
|
return $this; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
3 |
|
public function invokeMethods(array $methods): InflectorInterface |
|
54
|
|
|
{ |
|
55
|
3 |
|
foreach ($methods as $name => $args) { |
|
56
|
3 |
|
$this->invokeMethod($name, $args); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
3 |
|
return $this; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
9 |
|
public function setProperty(string $property, $value): InflectorInterface |
|
63
|
|
|
{ |
|
64
|
9 |
|
$this->properties[$property] = $this->resolveArguments([$value])[0]; |
|
65
|
9 |
|
return $this; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
3 |
|
public function setProperties(array $properties): InflectorInterface |
|
69
|
|
|
{ |
|
70
|
3 |
|
foreach ($properties as $property => $value) { |
|
71
|
3 |
|
$this->setProperty($property, $value); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
3 |
|
return $this; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
15 |
|
public function inflect(object $object): void |
|
78
|
|
|
{ |
|
79
|
15 |
|
$properties = $this->resolveArguments(array_values($this->properties)); |
|
80
|
15 |
|
$properties = array_combine(array_keys($this->properties), $properties); |
|
81
|
|
|
|
|
82
|
|
|
// array_combine() can technically return false |
|
83
|
15 |
|
foreach ($properties ?: [] as $property => $value) { |
|
84
|
6 |
|
$object->{$property} = $value; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
15 |
|
foreach ($this->methods as $method => $args) { |
|
88
|
6 |
|
$args = $this->resolveArguments($args); |
|
89
|
6 |
|
$callable = [$object, $method]; |
|
90
|
6 |
|
call_user_func_array($callable, $args); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
15 |
|
if ($this->callback !== null) { |
|
94
|
3 |
|
call_user_func($this->callback, $object); |
|
95
|
|
|
} |
|
96
|
15 |
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|