Passed
Push — master ( c1d97f...7cdedb )
by Carlos C
02:15 queued 11s
created

HusoHorarioEstacion::diaExpresion()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
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 33
    public function __construct(string $month, string $weekDay, string $time, int $utcDiff)
55
    {
56 33
        $this->month = mb_strtolower($month);
57 33
        $this->weekDay = mb_strtolower($weekDay);
58 33
        $this->time = $time;
59 33
        $this->timeHour = 0;
60 33
        $this->utcDiff = $utcDiff;
61
62 33
        $empties = intval('' !== $month) + intval('' !== $weekDay) + intval('' !== $time);
63 33
        if (0 !== $empties && 3 !== $empties) {
64 1
            throw new SatCatalogosLogicException('La definición del momento del cambio de horario está incompleta');
65
        }
66
67 32
        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
            );
71
        }
72
73 31
        $this->monthNumber = self::MONTH_NUMBERS[$this->month] ?? 0;
74 31
        if (0 === $this->monthNumber && '' !== $month) {
75 1
            throw new SatCatalogosLogicException(sprintf('El mes del huso horario "%s" es desconocido', $month));
76
        }
77
78 30
        $this->weekDayToTime = self::WEEK_DAYS[$this->weekDay] ?? '';
79 30
        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
            );
83
        }
84
85 29
        if ('' !== $this->time) {
86 25
            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
                );
90
            }
91 20
            $this->timeHour = intval(substr($this->time, 0, 2));
92 20
            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
                );
96
            }
97
        }
98 23
    }
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 16
    public function diferencia(): int
131
    {
132 16
        return $this->utcDiff;
133
    }
134
135 23
    public function tieneCambioHorario(): bool
136
    {
137 23
        return (0 !== $this->monthNumber);
138
    }
139
}
140