SubstituteHoliday   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 17 2
A getSubstitutedHoliday() 0 3 1
A getName() 0 14 4
A mergeGlobalTranslations() 0 7 1
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;
14
15
use Yasumi\Exception\InvalidDateException;
16
use Yasumi\Exception\MissingTranslationException;
17
use Yasumi\Exception\UnknownLocaleException;
18
19
/**
20
 * Class SubstituteHoliday.
21
 *
22
 * A substitute holiday is a holiday given in lieu of another holiday, if that day falls in a weekend or
23
 * overlaps with other holidays, so that people do not "lose" a day off in these years.
24
 *
25
 * @link https://en.wikipedia.org/wiki/Substitute_holiday
26
 */
27
class SubstituteHoliday extends Holiday
28
{
29
    /**
30
     * @var Holiday
31
     * @deprecated public access to this property is deprecated in favor of getSubstitutedHoliday()
32
     * @see getSubstitutedHoliday()
33
     */
34
    public $substitutedHoliday;
35
36
    /**
37
     * @var array list of translations of the "{0} observed" pattern
38
     */
39
    public $substituteHolidayTranslations;
40
41
    /**
42
     * Creates a new SubstituteHoliday.
43
     *
44
     * If a holiday date needs to be defined for a specific timezone, make sure that the date instance
45
     * (DateTimeInterface) has the correct timezone set. Otherwise the default system timezone is used.
46
     *
47
     * @param Holiday $substitutedHoliday The holiday being substituted
48
     * @param array $names An array containing the name/description of this holiday
49
     *                                               in various languages. Overrides global translations
50
     * @param \DateTimeInterface $date A DateTimeInterface instance representing the date of the holiday
51
     * @param string $displayLocale Locale (i.e. language) in which the holiday information needs to
52
     *                                               be displayed in. (Default 'en_US')
53
     * @param string $type The type of holiday. Use the following constants: TYPE_OFFICIAL,
54
     *                                               TYPE_OBSERVANCE, TYPE_SEASON, TYPE_BANK or TYPE_OTHER. By default
55
     *                                               an official holiday is considered.
56
     *
57
     * @throws InvalidDateException
58
     * @throws UnknownLocaleException
59
     * @throws \InvalidArgumentException
60
     * @throws \Exception
61
     */
62
    public function __construct(
63
        Holiday $substitutedHoliday,
64
        array $names,
65
        \DateTimeInterface $date,
66
        string $displayLocale = self::DEFAULT_LOCALE,
67
        string $type = self::TYPE_OFFICIAL
68
    ) {
69
        $this->substitutedHoliday = $substitutedHoliday;
0 ignored issues
show
Deprecated Code introduced by
The property Yasumi\SubstituteHoliday::$substitutedHoliday has been deprecated: public access to this property is deprecated in favor of getSubstitutedHoliday() ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

69
        /** @scrutinizer ignore-deprecated */ $this->substitutedHoliday = $substitutedHoliday;

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
70
71
        $key = 'substituteHoliday:' . $substitutedHoliday->getKey();
72
73
        if ($date == $substitutedHoliday) {
74
            throw new \InvalidArgumentException('Date must differ from the substituted holiday');
75
        }
76
77
        // Construct instance
78
        parent::__construct($key, $names, $date, $displayLocale, $type);
79
    }
80
81
    /**
82
     * Returns the holiday being substituted.
83
     *
84
     * @return Holiday the holiday being substituted.
85
     */
86
    public function getSubstitutedHoliday(): Holiday
87
    {
88
        return $this->substitutedHoliday;
0 ignored issues
show
Deprecated Code introduced by
The property Yasumi\SubstituteHoliday::$substitutedHoliday has been deprecated: public access to this property is deprecated in favor of getSubstitutedHoliday() ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

88
        return /** @scrutinizer ignore-deprecated */ $this->substitutedHoliday;

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
89
    }
90
91
    /**
92
     * Returns the localized name of this holiday
93
     *
94
     * The provided locales are searched for a translation. The first locale containing a translation will be used.
95
     *
96
     * If no locale is provided, proceed as if an array containing the display locale, Holiday::DEFAULT_LOCALE ('en_US'), and
97
     * Holiday::LOCALE_KEY (the holiday key) was provided.
98
     *
99
     * @param array $locales The locales to search for translations
100
     *
101
     * @return string
102
     * @throws MissingTranslationException
103
     *
104
     * @see Holiday::DEFAULT_LOCALE
105
     * @see Holiday::LOCALE_KEY
106
     */
107
    public function getName(array $locales = null): string
108
    {
109
        $name = parent::getName();
110
111
        if ($name === $this->getKey()) {
112
            foreach ($this->getLocales($locales) as $locales) {
113
                $pattern = $this->substituteHolidayTranslations[$locales] ?? null;
114
                if ($pattern) {
115
                    return \str_replace('{0}', $this->substitutedHoliday->getName(), $pattern);
0 ignored issues
show
Deprecated Code introduced by
The property Yasumi\SubstituteHoliday::$substitutedHoliday has been deprecated: public access to this property is deprecated in favor of getSubstitutedHoliday() ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

115
                    return \str_replace('{0}', /** @scrutinizer ignore-deprecated */ $this->substitutedHoliday->getName(), $pattern);

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
116
                }
117
            }
118
        }
119
120
        return $name;
121
    }
122
123
    /**
124
     * Merges local translations (preferred) with global translations.
125
     *
126
     * @param TranslationsInterface $globalTranslations global translations
127
     */
128
    public function mergeGlobalTranslations(TranslationsInterface $globalTranslations): void
129
    {
130
        $this->substituteHolidayTranslations = $globalTranslations->getTranslations('substituteHoliday');
131
132
        parent::mergeGlobalTranslations($globalTranslations);
133
134
        $this->substitutedHoliday->mergeGlobalTranslations($globalTranslations);
0 ignored issues
show
Deprecated Code introduced by
The property Yasumi\SubstituteHoliday::$substitutedHoliday has been deprecated: public access to this property is deprecated in favor of getSubstitutedHoliday() ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

134
        /** @scrutinizer ignore-deprecated */ $this->substitutedHoliday->mergeGlobalTranslations($globalTranslations);

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
135
    }
136
}
137