Completed
Push — master ( 2dd332...d6426f )
by steef
02:53
created

Parser::getAllValidDates()   C

Complexity

Conditions 7
Paths 9

Size

Total Lines 23
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 23
ccs 0
cts 14
cp 0
rs 6.7272
cc 7
eloc 10
nc 9
nop 3
crap 56
1
<?php
2
3
namespace Stefanius\SpecialDates\DateParser;
4
5
use Stefanius\SpecialDates\SDK\SpecialDateInterface;
6
7
class Parser extends AbstractParser
8
{
9
    /**
10
     * @param $year
11
     * @param bool $singleDay
12
     * @param bool $multiDays
13
     *
14
     * @return array
15
     */
16
    public function getAllValidDates($year, $singleDay = true, $multiDays = true)
17
    {
18
        $items = $this->getAllDates($year);
19
20
        /**
21
         * @var $value SpecialDateInterface
22
         */
23
        foreach ($items as $key => $value) {
24
            if ($value->isValid() === false) {
25
                unset($items[$key]);
26
            }
27
28
            if (!$singleDay && $value->getTotalLength() === 1) {
29
                unset($items[$key]);
30
            }
31
32
            if (!$multiDays && $value->getTotalLength() > 1) {
33
                unset($items[$key]);
34
            }
35
        }
36
37
        return $items;
38
    }
39
40
    /**
41
     * @param \DateTime $date
42
     *
43
     * @return array
44
     */
45 1
    public function findSpecialDateByDateTime(\DateTime $date)
46
    {
47 1
        $formattedDate = $date->format('Y') . '-' . $date->format('m') . $date->format('d');
48 1
        $items = $this->getAllDates($date->format('Y'));
49 1
        $found = [];
50
51
        /**
52
         * @var $item SpecialDateInterface
53
         */
54 1
        foreach ($items as $key => $item) {
55 1
            $formattedStartDate = $item->getStartDate()->format('Y') . '-' . $item->getStartDate()->format('m') . $item->getStartDate()->format('d');
56
57 1
            if ($formattedDate === $formattedStartDate) {
58 1
                $found[$key] = $item;
59 1
            }
60 1
        }
61
62 1
        return $found;
63 1
    }
64
65
    /**
66
     * @param $year
67
     * @param $month
68
     *
69
     * @return array
70
     */
71 1
    public function findSpecialDateByMonthNumber($year, $month)
72 1
    {
73 1
        $items = $this->getAllDates($year);
74 1
        $found = [];
75
76
        /**
77
         * @var $item SpecialDateInterface
78
         */
79 1
        foreach ($items as $key => $item) {
80 1
            if ((int)$month === (int)$item->getStartDate()->format('m')) {
81 1
                $found[$key] = $item;
82 1
            }
83 1
        }
84
85 1
        return $found;
86
    }
87
}
88