Passed
Push — master ( bc2c58...de2b0e )
by Carlos C
02:23 queued 12s
created

BoolDataField   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
c 1
b 0
f 0
dl 0
loc 26
rs 10
ccs 10
cts 10
cp 1
wmc 4

2 Methods

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