Passed
Push — master ( e458aa...d84cb7 )
by Carlos C
02:30 queued 11s
created

SchemaLocationsXsdUrlsFixer::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace CfdiUtils\Cleaner\Cleaners;
6
7
use CfdiUtils\Internals\StringUncaseReplacer;
8
use CfdiUtils\Utils\SchemaLocations;
9
use DOMAttr;
10
11
/**
12
 * Class SchemaLocationsXsdUrlsFixer
13
 *
14
 * This class is an abstraction of method Cleaner::fixKnownSchemaLocationsXsdUrls
15
 *
16
 * @internal
17
 */
18
final class SchemaLocationsXsdUrlsFixer
19
{
20
    /** @var StringUncaseReplacer */
21
    private $replacer;
22
23
    /**
24
     * Create a new instance based on a map using keys as replacement and values as an array of needles
25
     *
26
     * @param array<string, array<string>> $replacements
27
     */
28 1
    private function __construct(array $replacements)
29
    {
30 1
        $this->replacer = StringUncaseReplacer::create($replacements);
31 1
    }
32
33
    /**
34
     * Created a new instance based on known CFDI and TFD
35
     * It also includes the incorrect but allowed TFD 1.0 alternate urls
36
     *
37
     * @return static
38
     */
39 1
    public static function createWithKnownSatUrls(): self
40
    {
41 1
        return new self([
42 1
            'http://www.sat.gob.mx/sitio_internet/cfd/3/cfdv33.xsd' => [],
43
            'http://www.sat.gob.mx/sitio_internet/cfd/3/cfdv32.xsd' => [],
44
            'http://www.sat.gob.mx/sitio_internet/cfd/3/cfdv3.xsd' => [],
45
            'http://www.sat.gob.mx/sitio_internet/cfd/2/cfdv22.xsd' => [],
46
            'http://www.sat.gob.mx/sitio_internet/cfd/2/cfdv2.xsd' => [],
47
            'http://www.sat.gob.mx/sitio_internet/cfd/1/cfdv1.xsd' => [],
48
            'http://www.sat.gob.mx/sitio_internet/cfd/TimbreFiscalDigital/TimbreFiscalDigitalv11.xsd' => [],
49
            'http://www.sat.gob.mx/sitio_internet/cfd/TimbreFiscalDigital/TimbreFiscalDigital.xsd' => [
50
                'http://www.sat.gob.mx/sitio_internet/TimbreFiscalDigital/TimbreFiscalDigital.xsd',
51
            ],
52
        ]);
53
    }
54
55 1
    public function fixSchemaLocationAttribute(DOMAttr $xsiSchemaLocation)
56
    {
57 1
        $schemas = SchemaLocations::fromString($xsiSchemaLocation->value, false);
58 1
        $this->fixSchemaLocations($schemas);
59 1
        $fixedValue = $schemas->asString();
60 1
        if ($xsiSchemaLocation->value !== $fixedValue) {
61 1
            $xsiSchemaLocation->value = $fixedValue;
62
        }
63 1
    }
64
65 1
    public function fixSchemaLocations(SchemaLocations $schemaLocations)
66
    {
67 1
        foreach ($schemaLocations as $ns => $url) {
68 1
            $url = $this->replacer->findReplacement($url) ?: $url;
69 1
            $schemaLocations->append($ns, $url);
70
        }
71 1
    }
72
}
73