DateTime::getMonth()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 2
b 0
f 0
nc 2
nop 1
dl 0
loc 7
ccs 4
cts 4
cp 1
crap 2
rs 10
1
<?php
2
/**
3
 * DronePHP (http://www.dronephp.com)
4
 *
5
 * @link      http://github.com/Pleets/DronePHP
6
 * @copyright Copyright (c) 2016-2018 Pleets. (http://www.pleets.org)
7
 * @license   http://www.dronephp.com/license
8
 * @author    Darío Rivera <[email protected]>
9
 */
10
11
namespace Drone\Util;
12
13
/**
14
 * DateTime layer
15
 *
16
 * This class creates a \DateTime object
17
 */
18
class DateTime
19
{
20
    /**
21
     * Constants for days
22
     *
23
     * @var integer
24
     */
25
    const TODAY      = 0;
26
    const FIRST_DAY  = -1;
27
    const LAST_DAY   = -2;
28
29
    /**
30
     * Constants for months
31
     *
32
     * @var integer
33
     */
34
    const THIS_MONTH = -10;
35
    const LAST_MONTH = -11;
36
37
    /**
38
     * Constants for years
39
     *
40
     * @var integer
41
     */
42
    const THIS_YEAR  = -20;
43
    const LAST_YEAR  = -21;
44
45
    /**
46
     * Array of valid months
47
     *
48
     * @var string[]
49
     */
50
    const MONTHS = [
51
        1 => 'January', 'February', 'March', 'April', 'May', 'June',
52
        'July', 'August', 'September', 'October', 'November', 'December',
53
    ];
54
55
    /**
56
     * Returns the name of a month
57
     *
58
     * @param string $month_number
59
     *
60
     * @throws Exception\MonthOutOfRange
61
     *
62
     * @return string
63
     */
64 3
    public static function getMonth($month_number)
65
    {
66 3
        if (!array_key_exists($month_number, self::MONTHS)) {
67 1
            throw new Exception\MonthOutOfRange("Invalid Month");
68
        }
69
70 2
        return self::MONTHS[$month_number];
71
    }
72
73
    /**
74
     * Creates a \DateTime instance
75
     *
76
     * @param array $date
77
     *
78
     * @return \DateTime
79
     */
80 2
    public static function create(array $date = [])
81
    {
82 2
        if (!array_key_exists('day', $date)) {
83
            $date["day"] = self::TODAY;
84
        }
85
86 2
        if (!array_key_exists('month', $date)) {
87
            $date["month"] = self::THIS_MONTH;
88
        }
89
90 2
        if (!array_key_exists('year', $date)) {
91
            $date["year"] = self::THIS_YEAR;
92
        }
93
94 2
        switch ($date['year']) {
95 2
            case self::THIS_YEAR:
96 2
                $year = (int) date('Y');
97 2
                break;
98
99 1
            case self::LAST_YEAR:
100 1
                $year = ((int) date('Y'))- 1;
101 1
                break;
102
103
            default:
104
                $year = (int) $date["year"];
105
                break;
106
        }
107
108 2
        $month = ($date['month'] == self::THIS_MONTH)
109 2
            ? self::MONTHS[(int) date('m')]
110
            : (
111 1
                ($date['month'] == self::LAST_MONTH)
112 1
                ? '-1 month'
113 2
                : self::getMonth($date['month'])
114
            )
115
        ;
116
117 2
        switch ($date['day']) {
118 2
            case self::TODAY:
119 1
                $day = (int) date('d');
120 1
                break;
121
122 1
            case self::FIRST_DAY:
123 1
                $day = 'first day of';
124 1
                break;
125
126 1
            case self::LAST_DAY:
127 1
                $day = 'last day of';
128 1
                break;
129
130
            default:
131 1
                $day = (int) $date["day"];
132 1
                break;
133
        }
134
135 2
        if (is_string($day)) {
136 1
            $d = new \DateTime("$day $month $year");
137
        } else {
138 2
            if (in_array($month, self::MONTHS)) {
139 2
                $d = new \DateTime("first day of $month $year");
140
            } else {
141
                $d = new \DateTime("first day of $month");
142
                $d->setDate($year, (int) $d->format('m'), (int) $d->format('d'));
143
            }
144
145 2
            $d->add(new \DateInterval('P'.($day - 1).'D'));
146
        }
147
148 2
        return $d;
149
    }
150
}
151