Total Complexity | 4 |
Total Lines | 26 |
Duplicated Lines | 0 % |
Coverage | 100% |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
7 | class BoolDataField extends AbstractDataField implements DataFieldInterface |
||
8 | { |
||
9 | /** |
||
10 | * @param string[] $trueValues |
||
11 | * @param string[] $falseValues |
||
12 | */ |
||
13 | 144 | public function __construct( |
|
14 | string $name, |
||
15 | private readonly array $trueValues = [], |
||
16 | private readonly array $falseValues = [], |
||
17 | private readonly bool $default = false |
||
18 | ) { |
||
19 | 144 | parent::__construct($name, $this->valueToBoolean(...)); |
|
|
|||
20 | } |
||
21 | |||
22 | /** @param scalar $input */ |
||
23 | 122 | protected function valueToBoolean($input): bool |
|
24 | { |
||
25 | 122 | $input = trim((string) $input); |
|
26 | 122 | if (in_array($input, $this->trueValues, true)) { |
|
27 | 20 | return true; |
|
28 | } |
||
29 | 122 | if (in_array($input, $this->falseValues, true)) { |
|
30 | 16 | return false; |
|
31 | } |
||
32 | 122 | return $this->default; |
|
33 | } |
||
35 |