Spain::initialize()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 13
dl 0
loc 21
c 0
b 0
f 0
rs 9.8333
cc 1
nc 1
nop 0
1
<?php declare(strict_types=1);
2
/**
3
 * This file is part of the Yasumi package.
4
 *
5
 * Copyright (c) 2015 - 2020 AzuyaLabs
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @author Sacha Telgenhof <[email protected]>
11
 */
12
13
namespace Yasumi\Provider;
14
15
use DateTime;
16
use Yasumi\Exception\InvalidDateException;
17
use Yasumi\Exception\UnknownLocaleException;
18
use Yasumi\Holiday;
19
20
/**
21
 * Provider for all holidays in Spain.
22
 */
23
class Spain extends AbstractProvider
24
{
25
    use CommonHolidays, ChristianHolidays;
26
27
    /**
28
     * Code to identify this Holiday Provider. Typically this is the ISO3166 code corresponding to the respective
29
     * country or sub-region.
30
     */
31
    public const ID = 'ES';
32
33
    /**
34
     * Initialize holidays for Spain.
35
     *
36
     * @throws InvalidDateException
37
     * @throws \InvalidArgumentException
38
     * @throws UnknownLocaleException
39
     * @throws \Exception
40
     */
41
    public function initialize(): void
42
    {
43
        $this->timezone = 'Europe/Madrid';
44
45
        // Add common holidays
46
        $this->addHoliday($this->newYearsDay($this->year, $this->timezone, $this->locale));
47
        $this->addHoliday($this->valentinesDay($this->year, $this->timezone, $this->locale, Holiday::TYPE_OTHER));
48
        $this->addHoliday($this->internationalWorkersDay($this->year, $this->timezone, $this->locale));
49
50
        // Add Christian holidays (common in Spain)
51
        $this->addHoliday($this->epiphany($this->year, $this->timezone, $this->locale));
52
        $this->addHoliday($this->goodFriday($this->year, $this->timezone, $this->locale));
53
        $this->addHoliday($this->easter($this->year, $this->timezone, $this->locale, Holiday::TYPE_OBSERVANCE));
54
        $this->addHoliday($this->assumptionOfMary($this->year, $this->timezone, $this->locale));
55
        $this->addHoliday($this->allSaintsDay($this->year, $this->timezone, $this->locale));
56
        $this->addHoliday($this->immaculateConception($this->year, $this->timezone, $this->locale));
57
        $this->addHoliday($this->christmasDay($this->year, $this->timezone, $this->locale));
58
59
        // Calculate other holidays
60
        $this->calculateNationalDay();
61
        $this->calculateConstitutionDay();
62
    }
63
64
    /**
65
     * National Day.
66
     *
67
     * The Fiesta Nacional de España is the national day of Spain. It is held annually on October 12 and is a national
68
     * holiday. It commemorates the anniversary of Christopher Columbus's first arrival in the Americas, a day also
69
     * celebrated in other countries. The day was known as Dia de la Hispanidad, emphasizing Spain's connection to the
70
     * Hispanidad, the international Hispanic community. On November 27, 1981, a royal decree established Día de la
71
     * Hispanidad as a national holiday.
72
     *
73
     * @link https://en.wikipedia.org/wiki/Fiesta_Nacional_de_España
74
     *
75
     * @throws InvalidDateException
76
     * @throws \InvalidArgumentException
77
     * @throws UnknownLocaleException
78
     * @throws \Exception
79
     */
80
    private function calculateNationalDay(): void
81
    {
82
        if ($this->year >= 1981) {
83
            $this->addHoliday(new Holiday(
84
                'nationalDay',
85
                [
86
                    'ca' => 'Festa Nacional d’Espanya',
87
                    'es' => 'Fiesta Nacional de España',
88
                ],
89
                new DateTime("$this->year-10-12", DateTimeZoneFactory::getDateTimeZone($this->timezone)),
90
                $this->locale
91
            ));
92
        }
93
    }
94
95
    /**
96
     * Day of Constitution.
97
     *
98
     * Constitution Day (Día de la Constitución) marks the anniversary of a referendum held in Spain on December 6,
99
     * 1978. In this referendum, a new constitution was approved. This was an important step in Spain's transition to
100
     * becoming a constitutional monarchy and democracy.
101
     *
102
     * @link https://www.timeanddate.com/holidays/spain/constitution-day
103
     *
104
     * @throws InvalidDateException
105
     * @throws \InvalidArgumentException
106
     * @throws UnknownLocaleException
107
     * @throws \Exception
108
     */
109
    private function calculateConstitutionDay(): void
110
    {
111
        if ($this->year >= 1978) {
112
            $this->addHoliday(new Holiday(
113
                'constitutionDay',
114
                [
115
                    'ca' => 'Dia de la Constitució',
116
                    'es' => 'Día de la Constitución',
117
                ],
118
                new DateTime("$this->year-12-6", DateTimeZoneFactory::getDateTimeZone($this->timezone)),
119
                $this->locale
120
            ));
121
        }
122
    }
123
}
124