Completed
Push — main ( ec0f68...b41cf0 )
by Andreas
14s queued 12s
created

GregorianWeekday::getValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
/**
4
 * Copyright Andreas Heigl <[email protected]>
5
 *
6
 * Licenses under the MIT-license. For details see the included file LICENSE.md
7
 */
8
9
declare(strict_types=1);
10
11
namespace Org_Heigl\Holidaychecker;
12
13
use DateTimeInterface;
14
use IntlCalendar;
15
use RuntimeException;
16
use UnexpectedValueException;
17
use function method_exists;
18
use function sprintf;
19
use function strtolower;
20
21
final class GregorianWeekday
22
{
23
	/** @var string */
24
	private $value;
25
26
	/** @var array<string, GregorianWeekday> */
27
	private static $instances = [];
28
29
	private const MONDAY = 'monday';
30
	private const TUESDAY = 'tuesday';
31
	private const WEDNESDAY = 'wednesday';
32
	private const THURSDAY = 'thursday';
33
	private const FRIDAY = 'friday';
34
	private const SATURDAY = 'saturday';
35
	private const SUNDAY = 'sunday';
36
37
	private function __construct(string $value)
38
    {
39
		$this->value = $value;
40
	}
41
42
	public function getValue(): string
43
	{
44
		return $this->value;
45
	}
46
47
	public static function monday(): self
48
	{
49
		if (! isset(self::$instances[self::MONDAY])) {
50
			self::$instances[self::MONDAY] = new self(self::MONDAY);
51
		}
52
53
		return self::$instances[self::MONDAY];
54
	}
55
56
	public static function tuesday(): self
57
	{
58
		if (! isset(self::$instances[self::TUESDAY])) {
59
			self::$instances[self::TUESDAY] = new self(self::TUESDAY);
60
		}
61
62
		return self::$instances[self::TUESDAY];
63
	}
64
65
	public static function wednesday(): self
66
	{
67
		if (! isset(self::$instances[self::WEDNESDAY])) {
68
			self::$instances[self::WEDNESDAY] = new self(self::WEDNESDAY);
69
		}
70
71
		return self::$instances[self::WEDNESDAY];
72
	}
73
74
	public static function thursday(): self
75
	{
76
		if (! isset(self::$instances[self::THURSDAY])) {
77
			self::$instances[self::THURSDAY] = new self(self::THURSDAY);
78
		}
79
80
		return self::$instances[self::THURSDAY];
81
	}
82
83
	public static function friday(): self
84
	{
85
		if (! isset(self::$instances[self::FRIDAY])) {
86
			self::$instances[self::FRIDAY] = new self(self::FRIDAY);
87
		}
88
89
		return self::$instances[self::FRIDAY];
90
	}
91
92
	public static function saturday(): self
93
	{
94
		if (! isset(self::$instances[self::SATURDAY])) {
95
			self::$instances[self::SATURDAY] = new self(self::SATURDAY);
96
		}
97
98
		return self::$instances[self::SATURDAY];
99
	}
100
101
	public static function sunday(): self
102
	{
103
		if (! isset(self::$instances[self::SUNDAY])) {
104
			self::$instances[self::SUNDAY] = new self(self::SUNDAY);
105
		}
106
107
		return self::$instances[self::SUNDAY];
108
	}
109
110
	public static function fromString(string $weekday): self
111
	{
112
		if (! method_exists(self::class, strtolower($weekday))) {
113
			throw new RuntimeException(sprintf(
114
				'Weekday "%s" is not known',
115
				$weekday
116
			));
117
		}
118
119
		/** @var GregorianWeekday $gregorianWeekday */
120
		$gregorianWeekday = [self::class, strtolower($weekday)]();
121
122
		return $gregorianWeekday;
123
	}
124
125
	public static function fromDateTimeInterface(DateTimeInterface $date): self
126
	{
127
		return self::fromString($date->format('l'));
128
	}
129
130
	public static function fromIntlWeekday(int $weekday): self
131
	{
132
		$mapper = [
133
			IntlCalendar::DOW_SUNDAY    => 'sunday',
134
			IntlCalendar::DOW_MONDAY    => 'monday',
135
			IntlCalendar::DOW_TUESDAY   => 'tuesday',
136
			IntlCalendar::DOW_WEDNESDAY => 'wednesday',
137
			IntlCalendar::DOW_THURSDAY  => 'thursday',
138
			IntlCalendar::DOW_FRIDAY    => 'friday',
139
			IntlCalendar::DOW_SATURDAY  => 'saturday',
140
		];
141
		if (! isset($mapper[$weekday])) {
142
			throw new UnexpectedValueException(sprintf(
143
				'IntlCalendar weekday %s could not be resolved',
144
				$weekday
145
			));
146
		}
147
148
		return self::fromString($mapper[$weekday]);
149
	}
150
151
	public function __toString(): string
152
	{
153
		return $this->getValue();
154
	}
155
}
156