forked from phpcfdi/ceutils
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBaseCurrency.php
More file actions
57 lines (47 loc) · 1.92 KB
/
BaseCurrency.php
File metadata and controls
57 lines (47 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<?php
declare(strict_types=1);
namespace PhpCfdi\CeUtils\Validate\Common;
use CfdiUtils\Nodes\NodeInterface;
use CfdiUtils\Validate\Asserts;
use CfdiUtils\Validate\Status;
use PhpCfdi\CeUtils\Internal\AssertPrefixPropertyTrait;
use PhpCfdi\CeUtils\Internal\AssertsStatus;
use PhpCfdi\CeUtils\Internal\FindAllNodes;
use PhpCfdi\CeUtils\Internal\PathPropertyTrait;
use PhpCfdi\CeUtils\Validate\ValidatorInterface;
abstract class BaseCurrency implements ValidatorInterface
{
use AssertPrefixPropertyTrait;
use PathPropertyTrait;
public function __construct(string $assertPrefix, string ...$path)
{
$this->assertPrefix = $assertPrefix;
$this->path = array_values($path);
}
public function validate(NodeInterface $root, Asserts $asserts): void
{
$assert = $asserts->put(
$this->getAssertCode(''),
'La moneda se establece únicamente cuando el registro no es en moneda nacional',
);
$count = 1;
$nodes = FindAllNodes::find($root, ...$this->path);
foreach ($nodes as $location => $node) {
$this->validateNode($node, $asserts, $location, $count);
$count = $count + 1;
}
$assert->setStatus(AssertsStatus::fromPrefix($asserts, $assert->getCode()));
}
private function validateNode(NodeInterface $node, Asserts $asserts, string $location, int $count): void
{
$currencyExists = $node->attributes()->exists('Moneda');
$currency = $node['Moneda'];
$currencyExplanation = $currencyExists ? ($currency ?: '(vacía)') : '(ninguna)';
$asserts->put(
$this->getAssertCode(sprintf('-%03d', $count)),
'La moneda solo se especifica en caso de que sea diferente a moneda nacional',
Status::when(! $currencyExists || ! in_array($currency, ['', 'MXN'], true)),
sprintf('Moneda: %s, Nodo: %s', $currencyExplanation, $location),
);
}
}