1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Obblm\Core\Helper\Rule\Inducement; |
4
|
|
|
|
5
|
|
|
use Obblm\Core\Contracts\InducementInterface; |
6
|
|
|
use Obblm\Core\Helper\Optionable; |
7
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
8
|
|
|
|
9
|
|
|
abstract class AbstractInducement extends Optionable implements InducementInterface |
10
|
|
|
{ |
11
|
|
|
/** @var string */ |
12
|
|
|
protected $key; |
13
|
|
|
/** @var int */ |
14
|
|
|
protected $value; |
15
|
|
|
/** @var int */ |
16
|
|
|
protected $discountValue; |
17
|
|
|
/** @var string */ |
18
|
|
|
protected $name; |
19
|
|
|
/** @var string */ |
20
|
|
|
protected $translationDomain; |
21
|
|
|
/** @var string */ |
22
|
|
|
protected $typeName; |
23
|
|
|
/** @var InducementType */ |
24
|
|
|
protected $type; |
25
|
|
|
/** @var int */ |
26
|
|
|
protected $max; |
27
|
|
|
/** @var array */ |
28
|
|
|
protected $rosters = null; |
29
|
|
|
|
30
|
|
|
protected function hydrateWithOptions() |
31
|
|
|
{ |
32
|
|
|
$this->key = $this->options['key'] ?? false; |
|
|
|
|
33
|
|
|
$this->type = $this->options['type'] ?? null; |
34
|
|
|
$this->name = $this->options['name'] ?? false; |
|
|
|
|
35
|
|
|
$this->translationDomain = $this->options['translation_domain'] ?? false; |
|
|
|
|
36
|
|
|
$this->typeName = $this->options['type_name'] ?? false; |
|
|
|
|
37
|
|
|
$this->value = $this->options['value'] ?? false; |
|
|
|
|
38
|
|
|
$this->discountValue = $this->options['discount_value'] ?? $this->value; |
|
|
|
|
39
|
|
|
$this->max = $this->options['max'] ?? false; |
|
|
|
|
40
|
|
|
$this->rosters = $this->options['rosters'] ?? []; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @return InducementType |
45
|
|
|
*/ |
46
|
|
|
public function getType(): ?InducementType |
47
|
|
|
{ |
48
|
|
|
return $this->type; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function isMultiple(): bool |
52
|
|
|
{ |
53
|
|
|
return false; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @return string |
58
|
|
|
*/ |
59
|
|
|
public function getKey(): string |
60
|
|
|
{ |
61
|
|
|
return $this->key; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @return string |
66
|
|
|
*/ |
67
|
|
|
public function getTypeKey(): string |
68
|
|
|
{ |
69
|
|
|
return $this->getType()->getKey(); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @return int |
74
|
|
|
*/ |
75
|
|
|
public function getDiscountValue(): int |
76
|
|
|
{ |
77
|
|
|
return $this->discountValue; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @return int |
82
|
|
|
*/ |
83
|
|
|
public function getValue(): int |
84
|
|
|
{ |
85
|
|
|
return $this->value; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @return string |
90
|
|
|
*/ |
91
|
|
|
public function getTypeName(): string |
92
|
|
|
{ |
93
|
|
|
return $this->getType()->getName(); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @return string |
98
|
|
|
*/ |
99
|
|
|
public function getName(): string |
100
|
|
|
{ |
101
|
|
|
return $this->name; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @return string |
106
|
|
|
*/ |
107
|
|
|
public function getTranslationDomain(): string |
108
|
|
|
{ |
109
|
|
|
return $this->translationDomain; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @return int |
114
|
|
|
*/ |
115
|
|
|
public function getMax(): int |
116
|
|
|
{ |
117
|
|
|
return $this->max; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @return array |
122
|
|
|
*/ |
123
|
|
|
public function getRosters(): ?array |
124
|
|
|
{ |
125
|
|
|
return $this->rosters; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @param string $key |
130
|
|
|
*/ |
131
|
|
|
public function setKey(string $key): void |
132
|
|
|
{ |
133
|
|
|
$this->key = $key; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @param int $value |
138
|
|
|
*/ |
139
|
|
|
public function setValue(int $value): void |
140
|
|
|
{ |
141
|
|
|
$this->value = $value; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @param string $name |
146
|
|
|
*/ |
147
|
|
|
public function setName(string $name): void |
148
|
|
|
{ |
149
|
|
|
$this->name = $name; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @param string $translationDomain |
154
|
|
|
*/ |
155
|
|
|
public function setTranslationDomain(string $translationDomain): void |
156
|
|
|
{ |
157
|
|
|
$this->translationDomain = $translationDomain; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @param InducementType|array $type |
162
|
|
|
*/ |
163
|
|
|
public function setType($type): void |
164
|
|
|
{ |
165
|
|
|
$this->type = $type; |
|
|
|
|
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* @param int $max |
170
|
|
|
*/ |
171
|
|
|
public function setMax(int $max): void |
172
|
|
|
{ |
173
|
|
|
$this->max = $max; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* @param array $rosters |
178
|
|
|
*/ |
179
|
|
|
public function setRosters(array $rosters): void |
180
|
|
|
{ |
181
|
|
|
$this->rosters = $rosters; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
public function __toString(): string |
185
|
|
|
{ |
186
|
|
|
return $this->name; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
public function configureOptions(OptionsResolver $resolver):void |
190
|
|
|
{ |
191
|
|
|
$resolver->setDefaults([ |
192
|
|
|
'key' => null, |
193
|
|
|
'type' => null, |
194
|
|
|
'name' => null, |
195
|
|
|
'translation_domain' => null, |
196
|
|
|
'value' => null, |
197
|
|
|
'discount_value' => null, |
198
|
|
|
'max' => null, |
199
|
|
|
'rosters' => null, |
200
|
|
|
]) |
201
|
|
|
->setRequired(['key', 'type', 'name', 'translation_domain', 'value', 'max']) |
202
|
|
|
->setAllowedTypes('key', ['string']) |
203
|
|
|
->setAllowedTypes('type', [InducementType::class]) |
204
|
|
|
->setAllowedTypes('name', ['string']) |
205
|
|
|
->setAllowedTypes('translation_domain', ['string']) |
206
|
|
|
->setAllowedTypes('value', ['int']) |
207
|
|
|
->setAllowedTypes('discount_value', ['int', 'null']) |
208
|
|
|
->setAllowedTypes('max', ['int']) |
209
|
|
|
->setAllowedTypes('rosters', ['array', 'null']) |
210
|
|
|
; |
211
|
|
|
} |
212
|
|
|
} |
213
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.