|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is part of the php-code-generator package. |
|
4
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
5
|
|
|
* file that was distributed with this source code. |
|
6
|
|
|
* |
|
7
|
|
|
* @license Apache-2.0 |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace gossi\codegen\model\parts; |
|
11
|
|
|
|
|
12
|
|
|
use gossi\codegen\model\PhpConstant; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Value part |
|
16
|
|
|
* |
|
17
|
|
|
* For all models that have a value (or expression) |
|
18
|
|
|
* |
|
19
|
|
|
* @author Thomas Gossmann |
|
20
|
|
|
*/ |
|
21
|
|
|
trait ValuePart { |
|
22
|
|
|
private string|int|float|null|bool|PhpConstant $value = null; |
|
|
|
|
|
|
23
|
|
|
|
|
24
|
|
|
/** @var bool */ |
|
25
|
|
|
private bool $hasValue = false; |
|
26
|
|
|
|
|
27
|
|
|
/** @var mixed */ |
|
28
|
|
|
private mixed $expression = null; |
|
29
|
|
|
|
|
30
|
|
|
/** @var bool */ |
|
31
|
|
|
private bool $hasExpression = false; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Sets the value |
|
35
|
|
|
* |
|
36
|
|
|
* @param string|int|float|bool|null|PhpConstant $value |
|
37
|
|
|
* |
|
38
|
|
|
* @throws \InvalidArgumentException if the value is not an accepted primitve |
|
39
|
|
|
* |
|
40
|
|
|
* @return $this |
|
41
|
|
|
*/ |
|
42
|
|
|
public function setValue(string|int|float|null|bool|PhpConstant $value): self { |
|
43
|
|
|
$this->value = $value; |
|
44
|
|
|
$this->hasValue = true; |
|
45
|
|
|
|
|
46
|
|
|
return $this; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Unsets the value |
|
51
|
|
|
* |
|
52
|
|
|
* @return $this |
|
53
|
|
|
*/ |
|
54
|
|
|
public function unsetValue(): self { |
|
55
|
|
|
$this->value = null; |
|
56
|
|
|
$this->hasValue = false; |
|
57
|
|
|
|
|
58
|
|
|
return $this; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Returns the value |
|
63
|
|
|
* |
|
64
|
|
|
* @return string|int|float|bool|null|PhpConstant |
|
65
|
|
|
*/ |
|
66
|
|
|
public function getValue(): string|int|float|bool|null|PhpConstant { |
|
67
|
|
|
return $this->value; |
|
68
|
24 |
|
} |
|
69
|
24 |
|
|
|
70
|
20 |
|
/** |
|
71
|
20 |
|
* Checks whether a value or expression is set |
|
72
|
20 |
|
* |
|
73
|
18 |
|
* @return bool |
|
74
|
24 |
|
*/ |
|
75
|
|
|
public function hasValue(): bool { |
|
76
|
|
|
return $this->hasValue || $this->hasExpression; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Returns whether an expression is set |
|
81
|
|
|
* |
|
82
|
|
|
* @return bool |
|
83
|
|
|
*/ |
|
84
|
24 |
|
public function isExpression(): bool { |
|
85
|
24 |
|
return $this->hasExpression; |
|
86
|
1 |
|
} |
|
87
|
|
|
|
|
88
|
23 |
|
/** |
|
89
|
23 |
|
* Sets an expression |
|
90
|
|
|
* |
|
91
|
23 |
|
* @param mixed $expr |
|
92
|
|
|
* |
|
93
|
|
|
* @return $this |
|
94
|
|
|
*/ |
|
95
|
|
|
public function setExpression(mixed $expr): self { |
|
96
|
|
|
$this->expression = $expr; |
|
97
|
|
|
$this->hasExpression = true; |
|
98
|
|
|
|
|
99
|
1 |
|
return $this; |
|
100
|
1 |
|
} |
|
101
|
1 |
|
|
|
102
|
|
|
/** |
|
103
|
1 |
|
* Returns the expression |
|
104
|
|
|
* |
|
105
|
|
|
* @return mixed |
|
106
|
|
|
*/ |
|
107
|
|
|
public function getExpression(): mixed { |
|
108
|
|
|
return $this->expression; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
16 |
|
/** |
|
112
|
16 |
|
* Unsets the expression |
|
113
|
|
|
* |
|
114
|
|
|
* @return $this |
|
115
|
|
|
*/ |
|
116
|
|
|
public function unsetExpression(): self { |
|
117
|
|
|
$this->expression = null; |
|
118
|
|
|
$this->hasExpression = false; |
|
119
|
|
|
|
|
120
|
22 |
|
return $this; |
|
121
|
22 |
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|