|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Murtukov\PHPCodeGenerator; |
|
6
|
|
|
|
|
7
|
|
|
class Argument extends DependencyAwareGenerator implements FunctionMemberInterface |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* Special value to represent that there is no argument passed. |
|
11
|
|
|
*/ |
|
12
|
|
|
public const NO_PARAM = INF; |
|
13
|
|
|
|
|
14
|
|
|
private string $type; |
|
15
|
|
|
private string $name; |
|
16
|
|
|
private bool $isSpread = false; |
|
17
|
|
|
private bool $isByReference = false; |
|
18
|
|
|
private bool $isNullable = false; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var mixed |
|
22
|
|
|
*/ |
|
23
|
|
|
private $defaultValue; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Argument constructor. |
|
27
|
|
|
* |
|
28
|
|
|
* @param mixed $defaultValue |
|
29
|
|
|
*/ |
|
30
|
7 |
|
public function __construct(string $name, string $type = '', $defaultValue = self::NO_PARAM) |
|
31
|
|
|
{ |
|
32
|
7 |
|
$this->name = $name; |
|
33
|
7 |
|
$this->type = $this->resolveQualifier($type); |
|
34
|
|
|
|
|
35
|
7 |
|
if (INF !== $defaultValue) { |
|
36
|
6 |
|
$this->defaultValue = Utils::stringify($defaultValue); |
|
37
|
|
|
} |
|
38
|
7 |
|
} |
|
39
|
|
|
|
|
40
|
4 |
|
public static function new(string $name, string $type = '', $defaultValue = self::NO_PARAM): self |
|
41
|
|
|
{ |
|
42
|
4 |
|
return new self($name, $type, $defaultValue); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
10 |
|
public function generate(): string |
|
46
|
|
|
{ |
|
47
|
10 |
|
$code = ''; |
|
48
|
|
|
|
|
49
|
10 |
|
if ($this->type) { |
|
50
|
9 |
|
if ($this->isNullable && '?' !== $this->type[0]) { |
|
51
|
4 |
|
$code .= '?'; |
|
52
|
|
|
} |
|
53
|
9 |
|
$code .= $this->type.' '; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
10 |
|
if ($this->isByReference) { |
|
57
|
3 |
|
$code .= '&'; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
10 |
|
if ($this->isSpread) { |
|
61
|
3 |
|
$code .= '...'; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
10 |
|
$code .= ('$' === $this->name[0]) ? $this->name : "$$this->name"; |
|
65
|
|
|
|
|
66
|
10 |
|
if ($this->defaultValue) { |
|
67
|
9 |
|
$code .= " = $this->defaultValue"; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
10 |
|
return $code; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
9 |
|
public function __toString(): string |
|
74
|
|
|
{ |
|
75
|
9 |
|
return $this->generate(); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
1 |
|
public function isSpread(): bool |
|
79
|
|
|
{ |
|
80
|
1 |
|
return $this->isSpread; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
2 |
|
public function setSpread(): self |
|
84
|
|
|
{ |
|
85
|
2 |
|
$this->isSpread = true; |
|
86
|
|
|
|
|
87
|
2 |
|
return $this; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
1 |
|
public function unsetSpread(): self |
|
91
|
|
|
{ |
|
92
|
1 |
|
$this->isSpread = false; |
|
93
|
|
|
|
|
94
|
1 |
|
return $this; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
1 |
|
public function isByReference(): bool |
|
98
|
|
|
{ |
|
99
|
1 |
|
return $this->isByReference; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
2 |
|
public function setByReference(): self |
|
103
|
|
|
{ |
|
104
|
2 |
|
$this->isByReference = true; |
|
105
|
|
|
|
|
106
|
2 |
|
return $this; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
1 |
|
public function unsetByReference(): self |
|
110
|
|
|
{ |
|
111
|
1 |
|
$this->isByReference = false; |
|
112
|
|
|
|
|
113
|
1 |
|
return $this; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
2 |
|
public function setType(string $type): self |
|
117
|
|
|
{ |
|
118
|
2 |
|
$this->type = $type; |
|
119
|
|
|
|
|
120
|
2 |
|
return $this; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
2 |
|
public function setDefaultValue($value): self |
|
124
|
|
|
{ |
|
125
|
2 |
|
if (INF !== $value) { |
|
126
|
1 |
|
$this->defaultValue = Utils::stringify($value); |
|
127
|
|
|
} else { |
|
128
|
1 |
|
$this->defaultValue = ''; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
2 |
|
return $this; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
1 |
|
public function unsetNullable(): self |
|
135
|
|
|
{ |
|
136
|
1 |
|
$this->isNullable = false; |
|
137
|
|
|
|
|
138
|
1 |
|
return $this; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
3 |
|
public function setNullable(): self |
|
142
|
|
|
{ |
|
143
|
3 |
|
$this->isNullable = true; |
|
144
|
|
|
|
|
145
|
3 |
|
return $this; |
|
146
|
|
|
} |
|
147
|
|
|
} |
|
148
|
|
|
|