|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace gossi\codegen\model\parts; |
|
5
|
|
|
|
|
6
|
|
|
use gossi\codegen\model\PhpConstant; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Value part |
|
10
|
|
|
* |
|
11
|
|
|
* For all models that have a value (or expression) |
|
12
|
|
|
* |
|
13
|
|
|
* @author Thomas Gossmann |
|
14
|
|
|
*/ |
|
15
|
|
|
trait ValuePart { |
|
16
|
|
|
|
|
17
|
|
|
/** @var mixed */ |
|
18
|
|
|
private $value; |
|
19
|
|
|
|
|
20
|
|
|
/** @var bool */ |
|
21
|
|
|
private $hasValue = false; |
|
22
|
|
|
|
|
23
|
|
|
/** @var string */ |
|
24
|
|
|
private $expression; |
|
25
|
|
|
|
|
26
|
|
|
/** @var bool */ |
|
27
|
|
|
private $hasExpression = false; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @deprecated use `setValue()` instead |
|
31
|
|
|
* @param mixed $value |
|
32
|
|
|
* @return $this |
|
33
|
|
|
*/ |
|
34
|
|
|
public function setDefaultValue($value) { |
|
35
|
|
|
return $this->setValue($value); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @deprecated use `unsetValue()` instead |
|
40
|
|
|
* @return $this |
|
41
|
|
|
*/ |
|
42
|
|
|
public function unsetDefaultValue() { |
|
43
|
|
|
return $this->unsetValue(); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @deprecated use `getValue()` instead |
|
48
|
|
|
* @return mixed |
|
49
|
|
|
*/ |
|
50
|
|
|
public function getDefaultValue() { |
|
51
|
|
|
return $this->getValue(); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @deprecated use `hasValue()` instead |
|
56
|
|
|
* @return bool |
|
57
|
|
|
*/ |
|
58
|
|
|
public function hasDefaultValue(): bool { |
|
59
|
|
|
return $this->hasValue(); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Returns whether the given value is a primitive |
|
64
|
|
|
* |
|
65
|
|
|
* @param mixed $value |
|
66
|
|
|
* @return bool |
|
67
|
|
|
*/ |
|
68
|
24 |
|
private function isPrimitive($value): bool { |
|
69
|
24 |
|
return (is_string($value) |
|
70
|
20 |
|
|| is_int($value) |
|
71
|
20 |
|
|| is_float($value) |
|
72
|
20 |
|
|| is_bool($value) |
|
73
|
18 |
|
|| is_null($value) |
|
74
|
24 |
|
|| ($value instanceof PhpConstant)); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Sets the value |
|
79
|
|
|
* |
|
80
|
|
|
* @param string|int|float|bool|null|PhpConstant $value |
|
81
|
|
|
* @throws \InvalidArgumentException if the value is not an accepted primitve |
|
82
|
|
|
* @return $this |
|
83
|
|
|
*/ |
|
84
|
24 |
|
public function setValue($value) { |
|
85
|
24 |
|
if (!$this->isPrimitive($value)) { |
|
86
|
1 |
|
throw new \InvalidArgumentException('Use setValue() only for primitives and PhpConstant, anyway use setExpression() instead.'); |
|
87
|
|
|
} |
|
88
|
23 |
|
$this->value = $value; |
|
89
|
23 |
|
$this->hasValue = true; |
|
90
|
|
|
|
|
91
|
23 |
|
return $this; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Unsets the value |
|
96
|
|
|
* |
|
97
|
|
|
* @return $this |
|
98
|
|
|
*/ |
|
99
|
1 |
|
public function unsetValue() { |
|
100
|
1 |
|
$this->value = null; |
|
101
|
1 |
|
$this->hasValue = false; |
|
102
|
|
|
|
|
103
|
1 |
|
return $this; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Returns the value |
|
108
|
|
|
* |
|
109
|
|
|
* @return string|int|float|bool|null|PhpConstant |
|
110
|
|
|
*/ |
|
111
|
16 |
|
public function getValue() { |
|
112
|
16 |
|
return $this->value; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Checks whether a value or expression is set |
|
117
|
|
|
* |
|
118
|
|
|
* @return bool |
|
119
|
|
|
*/ |
|
120
|
22 |
|
public function hasValue(): bool { |
|
121
|
22 |
|
return $this->hasValue || $this->hasExpression; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Returns whether an expression is set |
|
126
|
|
|
* |
|
127
|
|
|
* @return bool |
|
128
|
|
|
*/ |
|
129
|
9 |
|
public function isExpression(): bool { |
|
130
|
9 |
|
return $this->hasExpression; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* Sets an expression |
|
135
|
|
|
* |
|
136
|
|
|
* @param string $expr |
|
137
|
|
|
* @return $this |
|
138
|
|
|
*/ |
|
139
|
10 |
|
public function setExpression(string $expr) { |
|
140
|
10 |
|
$this->expression = $expr; |
|
141
|
10 |
|
$this->hasExpression = true; |
|
142
|
|
|
|
|
143
|
10 |
|
return $this; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* Returns the expression |
|
148
|
|
|
* |
|
149
|
|
|
* @return string |
|
150
|
|
|
*/ |
|
151
|
8 |
|
public function getExpression(): ?string { |
|
152
|
8 |
|
return $this->expression; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* Unsets the expression |
|
157
|
|
|
* |
|
158
|
|
|
* @return $this |
|
159
|
|
|
*/ |
|
160
|
1 |
|
public function unsetExpression() { |
|
161
|
1 |
|
$this->expression = null; |
|
162
|
1 |
|
$this->hasExpression = false; |
|
163
|
|
|
|
|
164
|
1 |
|
return $this; |
|
165
|
|
|
} |
|
166
|
|
|
} |
|
167
|
|
|
|