-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
24 changed files
with
247 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
src/CfdiUtils/Cleaner/Cleaners/SchemaLocationsXsdUrlsFixer.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace CfdiUtils\Cleaner\Cleaners; | ||
|
||
use CfdiUtils\Internals\StringUncaseReplacer; | ||
use CfdiUtils\Utils\SchemaLocations; | ||
use DOMAttr; | ||
|
||
/** | ||
* Class SchemaLocationsXsdUrlsFixer | ||
* | ||
* This class is an abstraction of method Cleaner::fixKnownSchemaLocationsXsdUrls | ||
* | ||
* @internal | ||
*/ | ||
final class SchemaLocationsXsdUrlsFixer | ||
{ | ||
/** @var StringUncaseReplacer */ | ||
private $replacer; | ||
|
||
/** | ||
* Create a new instance based on a map using keys as replacement and values as an array of needles | ||
* | ||
* @param array<string, array<string>> $replacements | ||
*/ | ||
private function __construct(array $replacements) | ||
{ | ||
$this->replacer = StringUncaseReplacer::create($replacements); | ||
} | ||
|
||
/** | ||
* Created a new instance based on known CFDI and TFD | ||
* It also includes the incorrect but allowed TFD 1.0 alternate urls | ||
* | ||
* @return static | ||
*/ | ||
public static function createWithKnownSatUrls(): self | ||
{ | ||
return new self([ | ||
'http://www.sat.gob.mx/sitio_internet/cfd/3/cfdv33.xsd' => [], | ||
'http://www.sat.gob.mx/sitio_internet/cfd/3/cfdv32.xsd' => [], | ||
'http://www.sat.gob.mx/sitio_internet/cfd/3/cfdv3.xsd' => [], | ||
'http://www.sat.gob.mx/sitio_internet/cfd/2/cfdv22.xsd' => [], | ||
'http://www.sat.gob.mx/sitio_internet/cfd/2/cfdv2.xsd' => [], | ||
'http://www.sat.gob.mx/sitio_internet/cfd/1/cfdv1.xsd' => [], | ||
'http://www.sat.gob.mx/sitio_internet/cfd/TimbreFiscalDigital/TimbreFiscalDigitalv11.xsd' => [], | ||
'http://www.sat.gob.mx/sitio_internet/cfd/TimbreFiscalDigital/TimbreFiscalDigital.xsd' => [ | ||
'http://www.sat.gob.mx/sitio_internet/TimbreFiscalDigital/TimbreFiscalDigital.xsd', | ||
], | ||
]); | ||
} | ||
|
||
public function fixSchemaLocationAttribute(DOMAttr $xsiSchemaLocation) | ||
{ | ||
$schemas = SchemaLocations::fromString($xsiSchemaLocation->value, false); | ||
$this->fixSchemaLocations($schemas); | ||
$fixedValue = $schemas->asString(); | ||
if ($xsiSchemaLocation->value !== $fixedValue) { | ||
$xsiSchemaLocation->value = $fixedValue; | ||
} | ||
} | ||
|
||
public function fixSchemaLocations(SchemaLocations $schemaLocations) | ||
{ | ||
foreach ($schemaLocations as $ns => $url) { | ||
$url = $this->replacer->findReplacement($url) ?: $url; | ||
$schemaLocations->append($ns, $url); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace CfdiUtils\Internals; | ||
|
||
/** @internal */ | ||
final class StringUncaseReplacer | ||
{ | ||
/** @var array<string, array<string, true>> */ | ||
private $replacements; | ||
|
||
/** | ||
* @param array<string, array<string, true>> $replacements | ||
*/ | ||
private function __construct(array $replacements = []) | ||
{ | ||
$this->replacements = $replacements; | ||
} | ||
|
||
/** | ||
* @param array<string, array<string>> $replacements | ||
* @return static | ||
*/ | ||
public static function create(array $replacements): self | ||
{ | ||
$replacer = new self(); | ||
foreach ($replacements as $replacement => $needles) { | ||
$replacer->addReplacement($replacement, ...$needles); | ||
} | ||
return $replacer; | ||
} | ||
|
||
private function addReplacement(string $replacement, string ...$needle) | ||
{ | ||
$needle[] = $replacement; // also include the replacement itself | ||
foreach ($needle as $entry) { | ||
$entry = mb_strtolower($entry); // normalize url to compare | ||
$this->replacements[$replacement][$entry] = true; | ||
} | ||
} | ||
|
||
public function findReplacement(string $url): string | ||
{ | ||
$url = mb_strtolower($url); // normalize url to compare | ||
foreach ($this->replacements as $replacement => $entries) { | ||
if (isset($entries[$url])) { | ||
return $replacement; | ||
} | ||
} | ||
return ''; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.