|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace PhpCfdi\SatCatalogos\CFDI; |
|
6
|
|
|
|
|
7
|
|
|
use PhpCfdi\SatCatalogos\Exceptions\SatCatalogosLogicException; |
|
8
|
|
|
|
|
9
|
|
|
class HusoHorarioEstacion |
|
10
|
|
|
{ |
|
11
|
|
|
/** @var array<string, int> */ |
|
12
|
|
|
private const MONTH_NUMBERS = [ |
|
13
|
|
|
'enero' => 1, |
|
14
|
|
|
'febrero' => 2, |
|
15
|
|
|
'marzo' => 3, |
|
16
|
|
|
'abril' => 4, |
|
17
|
|
|
'mayo' => 5, |
|
18
|
|
|
'junio' => 6, |
|
19
|
|
|
'julio' => 7, |
|
20
|
|
|
'agosto' => 8, |
|
21
|
|
|
'septiembre' => 9, |
|
22
|
|
|
'octubre' => 10, |
|
23
|
|
|
'noviembre' => 11, |
|
24
|
|
|
'diciembre' => 12, |
|
25
|
|
|
]; |
|
26
|
|
|
|
|
27
|
|
|
private const WEEK_DAYS = [ |
|
28
|
|
|
'primer domingo' => 'first sunday of this month', |
|
29
|
|
|
'segundo domingo' => 'second sunday of this month', |
|
30
|
|
|
'último domingo' => 'last sunday of this month', |
|
31
|
|
|
]; |
|
32
|
|
|
|
|
33
|
|
|
/** @var string */ |
|
34
|
|
|
private $month; |
|
35
|
|
|
|
|
36
|
|
|
/** @var int */ |
|
37
|
|
|
private $monthNumber; |
|
38
|
|
|
|
|
39
|
|
|
/** @var string */ |
|
40
|
|
|
private $weekDay; |
|
41
|
|
|
|
|
42
|
|
|
/** @var string */ |
|
43
|
|
|
private $weekDayToTime; |
|
44
|
|
|
|
|
45
|
|
|
/** @var string */ |
|
46
|
|
|
private $time; |
|
47
|
|
|
|
|
48
|
|
|
/** @var int */ |
|
49
|
|
|
private $timeHour; |
|
50
|
|
|
|
|
51
|
|
|
/** @var int */ |
|
52
|
|
|
private $utcDiff; |
|
53
|
|
|
|
|
54
|
36 |
|
public function __construct(string $month, string $weekDay, string $time, int $utcDiff) |
|
55
|
|
|
{ |
|
56
|
36 |
|
$this->month = mb_strtolower($month); |
|
57
|
36 |
|
$this->weekDay = mb_strtolower($weekDay); |
|
58
|
36 |
|
$this->time = $time; |
|
59
|
36 |
|
$this->timeHour = 0; |
|
60
|
36 |
|
$this->utcDiff = $utcDiff; |
|
61
|
|
|
|
|
62
|
36 |
|
$empties = intval('' !== $month) + intval('' !== $weekDay) + intval('' !== $time); |
|
63
|
36 |
|
if (0 !== $empties && 3 !== $empties) { |
|
64
|
1 |
|
throw new SatCatalogosLogicException('La definición del momento del cambio de horario está incompleta'); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
35 |
|
if (abs($utcDiff) > 12) { |
|
68
|
1 |
|
throw new SatCatalogosLogicException( |
|
69
|
1 |
|
'La definición de diferencia de horario absoluta no puede ser mayor a 12', |
|
70
|
1 |
|
); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
34 |
|
$this->monthNumber = self::MONTH_NUMBERS[$this->month] ?? 0; |
|
74
|
34 |
|
if (0 === $this->monthNumber && '' !== $month) { |
|
75
|
1 |
|
throw new SatCatalogosLogicException(sprintf('El mes del huso horario "%s" es desconocido', $month)); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
33 |
|
$this->weekDayToTime = self::WEEK_DAYS[$this->weekDay] ?? ''; |
|
79
|
33 |
|
if ('' === $this->weekDayToTime && '' !== $weekDay) { |
|
80
|
1 |
|
throw new SatCatalogosLogicException( |
|
81
|
1 |
|
sprintf('El día de la semana del huso horario "%s" es desconocido', $weekDay), |
|
82
|
1 |
|
); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
32 |
|
if ('' !== $this->time) { |
|
86
|
26 |
|
if (! boolval(preg_match('/^\d\d:00$/', $this->time))) { |
|
87
|
5 |
|
throw new SatCatalogosLogicException( |
|
88
|
5 |
|
sprintf('La hora a la que inicia el cambio de horario "%s" no es válida', $this->time), |
|
89
|
5 |
|
); |
|
90
|
|
|
} |
|
91
|
21 |
|
$this->timeHour = intval(substr($this->time, 0, 2)); |
|
92
|
21 |
|
if ($this->timeHour > 23) { |
|
93
|
1 |
|
throw new SatCatalogosLogicException( |
|
94
|
1 |
|
sprintf('La hora a la que inicia el cambio de horario "%s" no puede ser mayor a 23', $this->time), |
|
95
|
1 |
|
); |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
3 |
|
public function mes(): string |
|
101
|
|
|
{ |
|
102
|
3 |
|
return $this->month; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
13 |
|
public function mesNumerico(): int |
|
106
|
|
|
{ |
|
107
|
13 |
|
return $this->monthNumber; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
3 |
|
public function dia(): string |
|
111
|
|
|
{ |
|
112
|
3 |
|
return $this->weekDay; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
13 |
|
public function diaExpresion(): string |
|
116
|
|
|
{ |
|
117
|
13 |
|
return $this->weekDayToTime; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
3 |
|
public function hora(): string |
|
121
|
|
|
{ |
|
122
|
3 |
|
return $this->time; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
10 |
|
public function horaNumero(): int |
|
126
|
|
|
{ |
|
127
|
10 |
|
return $this->timeHour; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
18 |
|
public function diferencia(): int |
|
131
|
|
|
{ |
|
132
|
18 |
|
return $this->utcDiff; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
26 |
|
public function tieneCambioHorario(): bool |
|
136
|
|
|
{ |
|
137
|
26 |
|
return (0 !== $this->monthNumber); |
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
|
|
|