1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of DivineNii opensource projects. |
7
|
|
|
* |
8
|
|
|
* PHP version 7.4 and above required |
9
|
|
|
* |
10
|
|
|
* @author Divine Niiquaye Ibok <[email protected]> |
11
|
|
|
* @copyright 2021 DivineNii (https://divinenii.com/) |
12
|
|
|
* @license https://opensource.org/licenses/BSD-3-Clause License |
13
|
|
|
* |
14
|
|
|
* For the full copyright and license information, please view the LICENSE |
15
|
|
|
* file that was distributed with this source code. |
16
|
|
|
*/ |
17
|
|
|
|
18
|
|
|
namespace Rade\DI\Definitions\Traits; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* This trait adds arguments functionality to the service definition. |
22
|
|
|
* |
23
|
|
|
* @author Divine Niiquaye Ibok <[email protected]> |
24
|
|
|
*/ |
25
|
|
|
trait ParameterTrait |
26
|
|
|
{ |
27
|
|
|
/** @var array<int|string,mixed> */ |
28
|
|
|
private array $arguments = []; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Sets/Replace one argument to pass to the service constructor/factory method. |
32
|
|
|
* |
33
|
|
|
* @param int|string $name |
34
|
|
|
* @param mixed $value |
35
|
|
|
* |
36
|
|
|
* @return $this |
37
|
|
|
*/ |
38
|
|
|
public function arg($name, $value) |
39
|
|
|
{ |
40
|
|
|
$this->arguments[$name] = $value; |
41
|
|
|
|
42
|
|
|
return $this; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Sets the arguments to pass to the service constructor/factory method. |
47
|
|
|
* |
48
|
|
|
* @param array<int|string,mixed> $arguments |
49
|
|
|
* |
50
|
|
|
* @return $this |
51
|
|
|
*/ |
52
|
|
|
public function args(array $arguments) |
53
|
|
|
{ |
54
|
|
|
foreach ($arguments as $name => $value) { |
55
|
|
|
$this->arguments[$name] = $value; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
return $this; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Whether this definition has constructor/factory arguments. |
63
|
|
|
*/ |
64
|
|
|
public function hasArguments(): bool |
65
|
|
|
{ |
66
|
|
|
return !empty($this->arguments); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Get the definition's arguments. |
71
|
|
|
* |
72
|
|
|
* @return array<int|string,mixed> |
73
|
|
|
*/ |
74
|
|
|
public function getArguments(): array |
75
|
|
|
{ |
76
|
|
|
return $this->arguments; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|