Passed
Pull Request — master (#3)
by Carlos C
12:19 queued 41s
created

BoolDataField::valueToBoolean()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 6
c 0
b 0
f 0
nc 3
nop 1
dl 0
loc 10
rs 10
ccs 0
cts 0
cp 0
crap 12
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpCfdi\SatCatalogosPopulate\Database;
6
7
class BoolDataField extends AbstractDataField implements DataFieldInterface
8
{
9 73
    /**
10
     * @param string[] $trueValues
11 73
     * @param string[] $falseValues
12 62
     */
13 62
    public function __construct(
14 11
        string $name,
15
        private array $trueValues = [],
16 62
        private array $falseValues = [],
17 9
        private bool $default = false
18
    ) {
19 62
        parent::__construct($name, [$this, 'valueToBoolean']);
20 73
    }
21 73
22
    /** @param scalar $input */
23
    protected function valueToBoolean($input): bool
24
    {
25
        $input = trim((string) $input);
26
        if (in_array($input, $this->trueValues, true)) {
27
            return true;
28
        }
29
        if (in_array($input, $this->falseValues, true)) {
30
            return false;
31
        }
32
        return $this->default;
33
    }
34
}
35