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