1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Inject.php |
5
|
|
|
* |
6
|
|
|
* Jaxon attribute. |
7
|
|
|
* Specifies attributes to inject into a callable object. |
8
|
|
|
* |
9
|
|
|
* @package jaxon-attributes |
10
|
|
|
* @author Thierry Feuzeu <[email protected]> |
11
|
|
|
* @copyright 2024 Thierry Feuzeu <[email protected]> |
12
|
|
|
* @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License |
13
|
|
|
* @link https://github.com/jaxon-php/jaxon-core |
14
|
|
|
*/ |
15
|
|
|
|
16
|
|
|
namespace Jaxon\Attributes\Attribute; |
17
|
|
|
|
18
|
|
|
use Jaxon\App\Metadata\Metadata; |
19
|
|
|
use Jaxon\Exception\SetupException; |
20
|
|
|
use Attribute; |
21
|
|
|
|
22
|
|
|
use function ltrim; |
23
|
|
|
|
24
|
|
|
#[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_PROPERTY | Attribute::TARGET_METHOD | Attribute::IS_REPEATABLE)] |
25
|
|
|
class Inject extends AbstractAttribute |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @var int |
29
|
|
|
*/ |
30
|
|
|
protected $nTarget; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var array |
34
|
|
|
*/ |
35
|
|
|
protected array $aTypes; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param string|null $type |
39
|
|
|
* @param string|null $attr |
40
|
|
|
*/ |
41
|
|
|
public function __construct(protected string|null $type = null, |
42
|
|
|
protected string|null $attr = null) |
43
|
|
|
{} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param int $nTarget |
47
|
|
|
* |
48
|
|
|
* @return void |
49
|
|
|
*/ |
50
|
|
|
public function setTarget(int $nTarget): void |
51
|
|
|
{ |
52
|
|
|
$this->nTarget = $nTarget; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param string $sAttr |
57
|
|
|
* |
58
|
|
|
* @return void |
59
|
|
|
*/ |
60
|
|
|
public function setAttr(string $sAttr): void |
61
|
|
|
{ |
62
|
|
|
$this->attr = $sAttr; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param array $aTypes |
67
|
|
|
* |
68
|
|
|
* @return void |
69
|
|
|
*/ |
70
|
|
|
public function setTypes(array $aTypes): void |
71
|
|
|
{ |
72
|
|
|
$this->aTypes = $aTypes; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @return void |
77
|
|
|
*/ |
78
|
|
|
public function validate(): void |
79
|
|
|
{ |
80
|
|
|
if($this->nTarget === Attribute::TARGET_CLASS) |
81
|
|
|
{ |
82
|
|
|
if(!$this->attr || !$this->type) |
83
|
|
|
{ |
84
|
|
|
throw new SetupException('When applied to a class, the Inject attribute requires two arguments.'); |
85
|
|
|
} |
86
|
|
|
return; |
87
|
|
|
} |
88
|
|
|
if($this->nTarget === Attribute::TARGET_METHOD) |
89
|
|
|
{ |
90
|
|
|
if(!$this->attr) |
91
|
|
|
{ |
92
|
|
|
throw new SetupException('When applied to a method, the Inject attribute requires the "attr" argument.'); |
93
|
|
|
} |
94
|
|
|
return; |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @return void |
100
|
|
|
*/ |
101
|
|
|
private function getFullClassName(): void |
102
|
|
|
{ |
103
|
|
|
if(!$this->type) |
104
|
|
|
{ |
105
|
|
|
// If no type is provided, take the attribute type. |
106
|
|
|
$this->type = $this->aTypes[$this->attr] ?? ''; |
107
|
|
|
return; |
108
|
|
|
} |
109
|
|
|
if($this->type[0] === '\\') |
110
|
|
|
{ |
111
|
|
|
$this->type = ltrim($this->type, '\\'); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @inheritDoc |
117
|
|
|
*/ |
118
|
|
|
public function saveValue(Metadata $xMetadata, string $sMethod = '*'): void |
119
|
|
|
{ |
120
|
|
|
$this->validate(); |
121
|
|
|
$this->getFullClassName(); |
122
|
|
|
$xMetadata->container($sMethod)->addValue($this->attr, $this->type); |
|
|
|
|
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|