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