Passed
Push — master ( 10c327...3c8cd4 )
by Carlos C
12:47 queued 14s
created

ToBeDefinedDataField   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Test Coverage

Coverage 92.31%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
c 1
b 0
f 0
dl 0
loc 26
ccs 12
cts 13
cp 0.9231
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
    /** @param string[] $toBeDefinedTexts */
15 5
    public function __construct(
16
        DataFieldInterface $nextDataField,
17
        private readonly array $toBeDefinedTexts = ['Por definir']
18
    ) {
19 5
        parent::__construct(
20 5
            fn ($input) => $this->matchToBeDefined($input) ? '' : $input,
21 5
            $nextDataField
22 5
        );
23
    }
24
25 2
    public function matchToBeDefined(mixed $input): bool
26
    {
27 2
        if (! is_scalar($input) && ! is_null($input)) {
28
            return false;
29
        }
30
31 2
        $input = trim(strval($input));
32 2
        foreach ($this->toBeDefinedTexts as $toBeDefinedText) {
33 2
            if (0 === strcasecmp($toBeDefinedText, $input)) {
34 2
                return true;
35
            }
36
        }
37 1
        return false;
38
    }
39
}
40