1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace PhpCfdi\SatCatalogos\CFDI40; |
6
|
|
|
|
7
|
|
|
use PhpCfdi\SatCatalogos\Common\BaseCatalog; |
8
|
|
|
use PhpCfdi\SatCatalogos\Common\BaseCatalogTrait; |
9
|
|
|
use PhpCfdi\SatCatalogos\Exceptions\SatCatalogosNotFoundException; |
10
|
|
|
use PhpCfdi\SatCatalogos\Repository; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Catálogo de Localidades |
14
|
|
|
*/ |
15
|
|
|
class Localidades implements BaseCatalog |
16
|
|
|
{ |
17
|
|
|
use BaseCatalogTrait; |
18
|
|
|
|
19
|
3 |
|
public function obtain(string $codigo, string $estado): Localidad |
20
|
|
|
{ |
21
|
3 |
|
$localidad = $this->find($codigo, $estado); |
22
|
3 |
|
if (null === $localidad) { |
23
|
1 |
|
throw new SatCatalogosNotFoundException( |
24
|
1 |
|
"No se encontró una localidad con código $codigo y estado $estado" |
25
|
|
|
); |
26
|
|
|
} |
27
|
2 |
|
return $localidad; |
28
|
|
|
} |
29
|
|
|
|
30
|
3 |
|
public function find(string $codigo, string $estado): ?Localidad |
31
|
|
|
{ |
32
|
|
|
/** @var array<array<string, scalar>> $data */ |
33
|
3 |
|
$data = $this->repository()->queryRowsByFields(Repository::CFDI_40_LOCALIDADES, [ |
34
|
|
|
'localidad' => $codigo, |
35
|
|
|
'estado' => $estado, |
36
|
|
|
]); |
37
|
3 |
|
if (! isset($data[0])) { |
38
|
1 |
|
return null; |
39
|
|
|
} |
40
|
2 |
|
return $this->create($data[0]); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param string $codigo |
45
|
|
|
* @param string $estado |
46
|
|
|
* @param string $texto |
47
|
|
|
* @return Localidad[] |
48
|
|
|
*/ |
49
|
3 |
|
public function search(string $codigo = '%', string $estado = '%', string $texto = '%'): array |
50
|
|
|
{ |
51
|
|
|
$filters = [ |
52
|
|
|
'localidad' => $codigo, |
53
|
|
|
'estado' => $estado, |
54
|
|
|
'texto' => $texto, |
55
|
|
|
]; |
56
|
|
|
|
57
|
3 |
|
return array_map( |
58
|
3 |
|
function (array $data): Localidad { |
59
|
3 |
|
return $this->create($data); |
60
|
|
|
}, |
61
|
3 |
|
$this->repository()->queryRowsByFields(Repository::CFDI_40_LOCALIDADES, $filters, 0, false) |
62
|
|
|
); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param array<string, scalar> $data |
67
|
|
|
* @return Localidad |
68
|
|
|
*/ |
69
|
6 |
|
public function create(array $data): Localidad |
70
|
|
|
{ |
71
|
6 |
|
return new Localidad( |
72
|
6 |
|
(string) $data['localidad'], |
73
|
6 |
|
(string) $data['estado'], |
74
|
6 |
|
(string) $data['texto'], |
75
|
6 |
|
intval(($data['vigencia_desde']) ? strtotime((string) $data['vigencia_desde']) : 0), |
76
|
6 |
|
intval(($data['vigencia_hasta']) ? strtotime((string) $data['vigencia_hasta']) : 0) |
77
|
|
|
); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|