Switzerland   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 124
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
eloc 41
c 1
b 0
f 0
dl 0
loc 124
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A initialize() 0 5 1
A calculateNationalDay() 0 24 4
A calculateBerchtoldsTag() 0 12 1
A calculateBettagsMontag() 0 13 2
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 Switzerland.
23
 */
24
class Switzerland 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 = 'CH';
33
34
    /**
35
     * Initialize holidays for Switzerland.
36
     *
37
     * @throws InvalidDateException
38
     * @throws \InvalidArgumentException
39
     * @throws UnknownLocaleException
40
     * @throws \Exception
41
     */
42
    public function initialize(): void
43
    {
44
        $this->timezone = 'Europe/Zurich';
45
46
        $this->calculateNationalDay();
47
    }
48
49
    /**
50
     * Swiss National Day.
51
     *
52
     * The Swiss National Day is set on 1 August. It has been an official national holiday
53
     * since 1994 only, although the day had been used for the celebration of the foundation
54
     * of the Swiss Confederacy for the first time in 1891, and than repeated annually since 1899.
55
     *
56
     * @link https://en.wikipedia.org/wiki/Swiss_National_Day
57
     *
58
     * @throws InvalidDateException
59
     * @throws \InvalidArgumentException
60
     * @throws UnknownLocaleException
61
     * @throws \Exception
62
     */
63
    private function calculateNationalDay(): void
64
    {
65
        $translations = [
66
            'en' => 'National Day',
67
            'fr' => 'Jour de la fête nationale',
68
            'de' => 'Bundesfeiertag',
69
            'it' => 'Giorno festivo federale',
70
            'rm' => 'Fiasta naziunala',
71
        ];
72
        if ($this->year >= 1994) {
73
            $this->addHoliday(new Holiday(
74
                'swissNationalDay',
75
                $translations,
76
                new DateTime($this->year . '-08-01', DateTimeZoneFactory::getDateTimeZone($this->timezone)),
77
                $this->locale,
78
                Holiday::TYPE_OFFICIAL
79
            ));
80
        } elseif ($this->year >= 1899 || 1891 === $this->year) {
81
            $this->addHoliday(new Holiday(
82
                'swissNationalDay',
83
                $translations,
84
                new DateTime($this->year . '-08-01', DateTimeZoneFactory::getDateTimeZone($this->timezone)),
85
                $this->locale,
86
                Holiday::TYPE_OBSERVANCE
87
            ));
88
        }
89
    }
90
91
    /**
92
     * Berchtoldstag
93
     *
94
     * Berchtoldstag is an Alemannic holiday, known in Switzerland and Liechtenstein. It is near
95
     * New Year's Day, during the Rauhnächte, in Switzerland nearly always on 2 January,
96
     * with the status of a public holiday in a number of cantons
97
     *
98
     * @link https://en.wikipedia.org/wiki/Berchtoldstag
99
     *
100
     * @throws InvalidDateException
101
     * @throws \InvalidArgumentException
102
     * @throws UnknownLocaleException
103
     * @throws \Exception
104
     */
105
    public function calculateBerchtoldsTag(): void
106
    {
107
        $this->addHoliday(new Holiday(
108
            'berchtoldsTag',
109
            [
110
                'de' => 'Berchtoldstag',
111
                'fr' => 'Jour de la Saint-Berthold',
112
                'en' => 'Berchtoldstag',
113
            ],
114
            new DateTime($this->year . '-01-02', DateTimeZoneFactory::getDateTimeZone($this->timezone)),
115
            $this->locale,
116
            Holiday::TYPE_OTHER
117
        ));
118
    }
119
120
    /**
121
     * Federal Day of Thanksgiving, Repentance and Prayer
122
     *
123
     * The Federal Day of Thanksgiving, Repentance and Prayer is a public holiday in Switzerland.
124
     * It is an interfaith feast observed by Roman Catholic dioceses, the Old Catholic Church,
125
     * the Jewish congregations and the Reformed church bodies as well as other Christian denominations.
126
     * The subsequent Monday (Lundi du Jeûne) is a public holiday in the canton of Vaud and Neuchâtel.
127
     *
128
     * @link https://en.wikipedia.org/wiki/Federal_Day_of_Thanksgiving,_Repentance_and_Prayer
129
     *
130
     * @throws InvalidDateException
131
     * @throws \InvalidArgumentException
132
     * @throws UnknownLocaleException
133
     * @throws \Exception
134
     */
135
    public function calculateBettagsMontag(): void
136
    {
137
        if ($this->year >= 1832) {
138
            // Find third Sunday of September
139
            $date = new DateTime('Third Sunday of ' . $this->year . '-09', DateTimeZoneFactory::getDateTimeZone($this->timezone));
140
            // Go to next Thursday
141
            $date->add(new DateInterval('P1D'));
142
143
            $this->addHoliday(new Holiday('bettagsMontag', [
144
                'fr' => 'Jeûne fédéral',
145
                'de' => 'Eidgenössischer Dank-, Buss- und Bettag',
146
                'it' => 'Festa federale di ringraziamento, pentimento e preghiera',
147
            ], $date, $this->locale, Holiday::TYPE_OTHER));
148
        }
149
    }
150
}
151