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 |
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 |
|
* Get the type. |
48
|
2 |
|
* |
49
|
|
|
* @return string |
50
|
3 |
|
*/ |
51
|
|
|
public function getType(): string |
52
|
|
|
{ |
53
|
|
|
return $this->type; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Defines a method to be invoked on the subject object. |
58
|
|
|
* |
59
|
|
|
* @param string $name |
60
|
9 |
|
* @param array $args |
61
|
|
|
* |
62
|
9 |
|
* @return self |
63
|
|
|
*/ |
64
|
9 |
|
public function invokeMethod(string $name, array $args): self |
65
|
|
|
{ |
66
|
|
|
$this->methods[$name] = $args; |
67
|
|
|
|
68
|
|
|
return $this; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Defines multiple methods to be invoked on the subject object. |
73
|
3 |
|
* |
74
|
|
|
* @param array $methods |
75
|
3 |
|
* |
76
|
3 |
|
* @return self |
77
|
2 |
|
*/ |
78
|
|
|
public function invokeMethods(array $methods): self |
79
|
3 |
|
{ |
80
|
|
|
foreach ($methods as $name => $args) { |
81
|
|
|
$this->invokeMethod($name, $args); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return $this; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
9 |
|
* Defines a property to be set on the subject object. |
89
|
|
|
* |
90
|
9 |
|
* @param string $property |
91
|
9 |
|
* @param mixed $value |
92
|
|
|
* |
93
|
9 |
|
* @return self |
94
|
6 |
|
*/ |
95
|
6 |
|
public function setProperty(string $property, $value): self |
96
|
|
|
{ |
97
|
9 |
|
$this->properties[$property] = $value; |
98
|
3 |
|
|
99
|
|
|
return $this; |
100
|
3 |
|
} |
101
|
6 |
|
|
102
|
9 |
|
/** |
103
|
|
|
* Defines multiple properties to be set on the subject object. |
104
|
|
|
* |
105
|
|
|
* @param array $properties |
106
|
|
|
* |
107
|
|
|
* @return self |
108
|
|
|
*/ |
109
|
|
|
public function setProperties(array $properties): self |
110
|
|
|
{ |
111
|
|
|
foreach ($properties as $property => $value) { |
112
|
|
|
$this->setProperty($property, $value); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
return $this; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Apply inflections to an object. |
120
|
|
|
* |
121
|
|
|
* @param object $object |
122
|
|
|
* |
123
|
|
|
* @return void |
124
|
|
|
*/ |
125
|
|
|
public function inflect($object) |
126
|
|
|
{ |
127
|
|
|
$properties = $this->resolveArguments(array_values($this->properties)); |
128
|
|
|
$properties = array_combine(array_keys($this->properties), $properties); |
129
|
|
|
|
130
|
|
|
foreach ($properties as $property => $value) { |
131
|
|
|
$object->{$property} = $value; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
foreach ($this->methods as $method => $args) { |
135
|
|
|
$args = $this->resolveArguments($args); |
136
|
|
|
|
137
|
|
|
call_user_func_array([$object, $method], $args); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
if (! is_null($this->callback)) { |
141
|
|
|
call_user_func_array($this->callback, [$object]); |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|