Passed
Branch main (2e4b54)
by Andreas
10:30
created

ObservanceDecorator::dateMatches()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 7
rs 10
cc 2
nc 2
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * Copyright Andreas Heigl <[email protected]>
7
 *
8
 * Licenses under the MIT-license. For details see the included file LICENSE.md
9
 */
10
11
namespace Org_Heigl\Holidaychecker;
12
13
use DateTimeInterface;
14
15
final class ObservanceDecorator implements HolidayIteratorItemInterface
16
{
17
    /** @var int|null */
18
    private $firstObservance;
19
20
    /** @var int|null */
21
    private $lastObservance;
22
23
	/** @var HolidayIteratorItemInterface */
24
	private $wrapped;
25
26
	public function __construct(HolidayIteratorItemInterface $wrapped, ?int $firstObservance, ?int $lastObservance)
27
    {
28
		$this->wrapped = $wrapped;
29
        $this->firstObservance = $firstObservance;
30
        $this->lastObservance = $lastObservance;
31
    }
32
33
    private function isWithinObservance(int $gregorianYear): bool
34
    {
35
        if (null !== $this->firstObservance && $this->firstObservance > $gregorianYear) {
36
            return false;
37
        }
38
39
        return null === $this->lastObservance || $this->lastObservance >= $gregorianYear;
40
    }
41
42
	public function dateMatches(DateTimeInterface $date): bool
43
	{
44
		if (! $this->isWithinObservance((int) $date->format('Y'))) {
45
			return false;
46
		}
47
48
		return $this->wrapped->dateMatches($date);
49
	}
50
51
	public function getName(): string
52
	{
53
		return $this->wrapped->getName();
54
	}
55
56
	public function isHoliday(): bool
57
	{
58
		return $this->wrapped->isHoliday();
59
	}
60
}
61