Patron   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 23
c 2
b 0
f 0
dl 0
loc 63
ccs 23
cts 23
cp 1
rs 10
wmc 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A expresion() 0 3 1
A alEstarVacio() 0 3 1
A evalua() 0 4 1
A origen() 0 3 1
A __construct() 0 16 4
A expresionEsValida() 0 4 1
A __toString() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpCfdi\SatCatalogos\Helpers;
6
7
use PhpCfdi\SatCatalogos\Exceptions\PatronException;
8
9
class Patron
10
{
11
    /** @var string */
12
    private $origen;
13
14
    /** @var string */
15
    private $expresion;
16
17
    /** @var string */
18
    private $alEstarVacio;
19
20
    public const VACIO_PERMITE_NADA = 'NADA';
21
22
    public const VACIO_PERMITE_TODO = 'TODO';
23
24 59
    public function __construct(string $origen, string $alEstarVacio = self::VACIO_PERMITE_TODO)
25
    {
26 59
        if ('' === $origen && self::VACIO_PERMITE_TODO === $alEstarVacio) {
27
            // cualquier caracter no espaciado vertical, de 0 a N veces
28 11
            $expresion = '\V*';
29
        } else {
30 54
            $expresion = $origen;
31
        }
32 59
        $expresion = '/^' . $expresion . '$/u';
33 59
        if (! $this->expresionEsValida($expresion)) {
34 5
            throw new PatronException($expresion);
35
        }
36
37 56
        $this->origen = $origen;
38 56
        $this->expresion = $expresion;
39 56
        $this->alEstarVacio = $alEstarVacio;
40
    }
41
42 23
    public function origen(): string
43
    {
44 23
        return $this->origen;
45
    }
46
47 4
    public function expresion(): string
48
    {
49 4
        return $this->expresion;
50
    }
51
52 11
    public function alEstarVacio(): string
53
    {
54 11
        return $this->alEstarVacio;
55
    }
56
57 4
    public function evalua(string $texto): bool
58
    {
59 4
        $expresion = $this->expresion();
60 4
        return (bool) preg_match($expresion, $texto);
61
    }
62
63 59
    public function expresionEsValida(string $expresion): bool
64
    {
65
        /** @noinspection PhpUsageOfSilenceOperatorInspection */
66 59
        return (false !== @preg_match($expresion, ''));
67
    }
68
69 1
    public function __toString(): string
70
    {
71 1
        return $this->origen();
72
    }
73
}
74