Sweden   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 214
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 8
eloc 63
c 3
b 0
f 0
dl 0
loc 214
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A calculateAllSaintsHolidays() 0 17 1
A calculateStJohnsHolidays() 0 17 1
A calculateEpiphanyEve() 0 8 1
A calculateWalpurgisEve() 0 8 1
A initialize() 0 27 1
A calculateNationalDay() 0 18 3
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 DateInterval;
16
use DateTime;
17
use Yasumi\Exception\InvalidDateException;
18
use Yasumi\Exception\UnknownLocaleException;
19
use Yasumi\Holiday;
20
21
/**
22
 * Provider for all holidays in Sweden.
23
 */
24
class Sweden extends AbstractProvider
25
{
26
    use CommonHolidays, ChristianHolidays;
27
28
    /**
29
     * Code to identify this Holiday Provider. Typically this is the ISO3166 code corresponding to the respective
30
     * country or sub-region.
31
     */
32
    public const ID = 'SE';
33
34
    /**
35
     * Initialize holidays for Sweden.
36
     *
37
     * @throws InvalidDateException
38
     * @throws \InvalidArgumentException
39
     * @throws UnknownLocaleException
40
     * @throws \Exception
41
     */
42
    public function initialize(): void
43
    {
44
        $this->timezone = 'Europe/Stockholm';
45
46
        // Add common holidays
47
        $this->addHoliday($this->newYearsDay($this->year, $this->timezone, $this->locale));
48
        $this->addHoliday($this->internationalWorkersDay($this->year, $this->timezone, $this->locale));
49
50
        // Add common Christian holidays (common in Sweden)
51
        $this->addHoliday($this->epiphany($this->year, $this->timezone, $this->locale));
52
        $this->calculateEpiphanyEve();
53
        $this->calculateWalpurgisEve();
54
        $this->addHoliday($this->goodFriday($this->year, $this->timezone, $this->locale));
55
        $this->addHoliday($this->easter($this->year, $this->timezone, $this->locale));
56
        $this->addHoliday($this->easterMonday($this->year, $this->timezone, $this->locale));
57
        $this->addHoliday($this->ascensionDay($this->year, $this->timezone, $this->locale));
58
        $this->addHoliday($this->pentecost($this->year, $this->timezone, $this->locale));
59
        $this->calculateStJohnsHolidays(); // aka Midsummer
60
        $this->calculateAllSaintsHolidays();
61
        $this->addHoliday($this->christmasEve($this->year, $this->timezone, $this->locale));
62
        $this->addHoliday($this->christmasDay($this->year, $this->timezone, $this->locale));
63
        $this->addHoliday($this->secondChristmasDay($this->year, $this->timezone, $this->locale));
64
65
        $this->addHoliday($this->newYearsEve($this->year, $this->timezone, $this->locale, Holiday::TYPE_OBSERVANCE));
66
67
        // Calculate other holidays
68
        $this->calculateNationalDay();
69
    }
70
71
    /**
72
     * Epiphany Eve.
73
     *
74
     * Epiphany is a Christian feast day that celebrates the revelation of God the Son as a human being in Jesus Christ.
75
     * The traditional date for the feast is January 6. In Sweden the holiday is celebrated on the evening before, also
76
     * known as Twelfth Night.
77
     *
78
     * Epiphany Eve is often treated with the afternoon off, but this varies depending on employer.
79
     *
80
     * @link https://en.wikipedia.org/wiki/Twelfth_Night_(holiday)
81
     *
82
     * @throws InvalidDateException
83
     * @throws UnknownLocaleException
84
     * @throws \InvalidArgumentException
85
     * @throws \Exception
86
     */
87
    public function calculateEpiphanyEve(): void
88
    {
89
        $this->addHoliday(new Holiday(
90
            'epiphanyEve',
91
            [],
92
            new DateTime("$this->year-1-5", DateTimeZoneFactory::getDateTimeZone($this->timezone)),
93
            $this->locale,
94
            Holiday::TYPE_OBSERVANCE
95
        ));
96
    }
97
98
99
    /**
100
     * Walpurgis Night.
101
     *
102
     * Walpurgis Night is the eve of the Christian feast day of Saint Walpurga, an 8th-century abbess in Francia.
103
     * This feast commemorates the canonization of Saint Walpurga and the movement of her relics to Eichstätt,
104
     * both of which occurred on 1 May 870
105
     *
106
     * Walpurgis Night is often treated with the afternoon off, but this varies depending on employer.
107
     *
108
     * @link https://en.wikipedia.org/wiki/Walpurgis_Night
109
     *
110
     * @throws InvalidDateException
111
     * @throws UnknownLocaleException
112
     * @throws \InvalidArgumentException
113
     * @throws \Exception
114
     */
115
    public function calculateWalpurgisEve(): void
116
    {
117
        $this->addHoliday(new Holiday(
118
            'walpurgisEve',
119
            [],
120
            new DateTime("$this->year-4-30", DateTimeZoneFactory::getDateTimeZone($this->timezone)),
121
            $this->locale,
122
            Holiday::TYPE_OBSERVANCE
123
        ));
124
    }
125
126
    /**
127
     * St. John's Day / Midsummer.
128
     *
129
     * Midsummer, also known as St John's Day, is the period of time centred upon the summer solstice, and more
130
     * specifically the Northern European celebrations that accompany the actual solstice or take place on a day
131
     * between June 19 and June 25 and the preceding evening. The exact dates vary between different cultures.
132
     * The Christian Church designated June 24 as the feast day of the early Christian martyr St John the Baptist, and
133
     * the observance of St John's Day begins the evening before, known as St John's Eve.
134
     *
135
     * In Sweden the holiday has always been on a Saturday (between June 20 and June 26). Many of the celebrations of
136
     * midsummer take place on midsummer eve, when many workplaces are closed and shops must close their doors at noon.
137
     *
138
     * @link https://en.wikipedia.org/wiki/Midsummer#Sweden
139
     *
140
     * @throws InvalidDateException
141
     * @throws \InvalidArgumentException
142
     * @throws UnknownLocaleException
143
     * @throws \Exception
144
     */
145
    private function calculateStJohnsHolidays(): void
146
    {
147
        $date = new DateTime("$this->year-6-20 this saturday", DateTimeZoneFactory::getDateTimeZone($this->timezone));
148
        $this->addHoliday(new Holiday(
149
            'stJohnsDay',
150
            [],
151
            $date,
152
            $this->locale
153
        ));
154
155
        $date->sub(new DateInterval('P1D'));
156
        $this->addHoliday(new Holiday(
157
            'stJohnsEve',
158
            [],
159
            $date,
160
            $this->locale,
161
            Holiday::TYPE_OBSERVANCE
162
        ));
163
    }
164
165
    /**
166
     * All Saints Day.
167
     *
168
     * All Saints' Day is a celebration of all Christian saints, particularly those who have no special feast days of
169
     * their own, in many Roman Catholic, Anglican and Protestant churches. In many western churches it is annually held
170
     * November 1 and in many eastern churches it is celebrated on the first Sunday after Pentecost. It is also known
171
     * as All Hallows Tide, All-Hallomas, or All Hallows' Day.
172
     *
173
     * The festival was retained after the Reformation in the calendar of the Anglican Church and in many Lutheran
174
     * churches. In the Lutheran churches, such as the Church of Sweden, it assumes a role of general commemoration of
175
     * the dead. In the Swedish calendar, the observance takes place on the Saturday between 31 October and 6 November.
176
     * In many Lutheran Churches, it is moved to the first Sunday of November.
177
     *
178
     * @link https://en.wikipedia.org/wiki/All_Saints%27_Day
179
     * @link https://www.timeanddate.com/holidays/sweden/all-saints-day
180
     *
181
     * @throws InvalidDateException
182
     * @throws \InvalidArgumentException
183
     * @throws UnknownLocaleException
184
     * @throws \Exception
185
     */
186
    private function calculateAllSaintsHolidays(): void
187
    {
188
        $date = new DateTime("$this->year-10-31 this saturday", DateTimeZoneFactory::getDateTimeZone($this->timezone));
189
        $this->addHoliday(new Holiday(
190
            'allSaintsDay',
191
            [],
192
            $date,
193
            $this->locale
194
        ));
195
196
        $date->sub(new DateInterval('P1D'));
197
        $this->addHoliday(new Holiday(
198
            'allSaintsEve',
199
            [],
200
            $date,
201
            $this->locale,
202
            Holiday::TYPE_OBSERVANCE
203
        ));
204
    }
205
206
    /**
207
     * National Day
208
     *
209
     * National Day of Sweden (Sveriges nationaldag) is a national holiday observed in Sweden on 6 June every year.
210
     * Prior to 1983, the day was celebrated as Svenska flaggans dag (Swedish flag day). At that time, the day was
211
     * renamed to the national day by the Riksdag. The tradition of celebrating this date began 1916 at the Stockholm
212
     * Olympic Stadium, in honour of the election of King Gustav Vasa in 1523, as this was considered the foundation of
213
     * modern Sweden.
214
     *
215
     * @throws InvalidDateException
216
     * @throws \InvalidArgumentException
217
     * @throws UnknownLocaleException
218
     * @throws \Exception
219
     */
220
    private function calculateNationalDay(): void
221
    {
222
        if ($this->year < 1916) {
223
            return;
224
        }
225
226
        $holidayName = 'Svenska flaggans dag';
227
228
        // Since 1983 this day was named 'Sveriges nationaldag'
229
        if ($this->year >= 1983) {
230
            $holidayName = 'Sveriges nationaldag';
231
        }
232
233
        $this->addHoliday(new Holiday(
234
            'nationalDay',
235
            ['sv' => $holidayName],
236
            new DateTime("$this->year-6-6", DateTimeZoneFactory::getDateTimeZone($this->timezone)),
237
            $this->locale
238
        ));
239
    }
240
}
241