Passed
Push — master ( 6cf998...fd9a38 )
by Carlos C
12:00 queued 13s
created

ToBeDefinedDataField   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Test Coverage

Coverage 92.86%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
c 1
b 0
f 0
dl 0
loc 28
ccs 13
cts 14
cp 0.9286
rs 10
wmc 7

2 Methods

Rating   Name   Duplication   Size   Complexity  
A matchToBeDefined() 0 13 5
A __construct() 0 7 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpCfdi\SatCatalogosPopulate\Database;
6
7
/**
8
 * Esta clase es un preprocesador, toma un valor y lo compara sin considerar mayúsculas y minúsculas
9
 * contra una lista de valores, si alguno coincide entonces devuelve una cadena de caracteres vacía,
10
 * si no coincide con algún elemento de la lista entonces devuelve el mismo valor comparado.
11
 */
12
final class ToBeDefinedDataField extends PreprocessDataField
13
{
14
    /** @var string[] */
15
    private array $toBeDefinedTexts;
16
17
    /** @param string[] $toBeDefinedTexts */
18 5
    public function __construct(DataFieldInterface $nextDataField, array $toBeDefinedTexts = ['Por definir'])
19
    {
20 5
        parent::__construct(
21 5
            fn ($input) => $this->matchToBeDefined($input) ? '' : $input,
22 5
            $nextDataField
23 5
        );
24 5
        $this->toBeDefinedTexts = $toBeDefinedTexts;
25
    }
26
27 2
    public function matchToBeDefined(mixed $input): bool
28
    {
29 2
        if (! is_scalar($input) && ! is_null($input)) {
30
            return false;
31
        }
32
33 2
        $input = trim(strval($input));
34 2
        foreach ($this->toBeDefinedTexts as $toBeDefinedText) {
35 2
            if (0 === strcasecmp($toBeDefinedText, $input)) {
36 2
                return true;
37
            }
38
        }
39 1
        return false;
40
    }
41
}
42