Passed
Push — master ( bd65b1...807cd9 )
by Aleksandr
01:31
created

Calendar::getInstance()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace carono\production;
4
5
/**
6
 * Class Calendar
7
 *
8
 * @package carono\production
9
 */
10
class Calendar
11
{
12
    public $format = 'Y-m-d';
13
    private static $_instance;
14
    protected static $holidays;
15
    /**
16
     * @var \DateTime
17
     */
18
    private static $date;
19
20
    public function __toString()
21
    {
22
        return $this->date()->format($this->format);
23
    }
24
25
    public static function date()
26
    {
27
        return static::$date;
0 ignored issues
show
Bug introduced by
Since $date is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $date to at least protected.
Loading history...
28
    }
29
30
    public function timestamp()
31
    {
32
        return static::date()->getTimestamp();
33
    }
34
35
    /**
36
     * @param \DateTime|string $date
37
     *
38
     * @param array $weekend
39
     *
40
     * @return bool
41
     */
42
    public static function isWorking($date, $weekend = [6, 0])
43
    {
44
        return static::isPreHoliday($date) || (!static::isHoliday($date) && !static::isWeekend($date, $weekend));
45
    }
46
47
    protected static function findDateInArray($date, $array)
48
    {
49
        $date = static::prepareDate($date);
50
        return in_array($date->format('Y-m-d'), $array);
51
52
    }
53
54
    public static function isPreHoliday($date)
55
    {
56
        return static::findDateInArray($date, static::getPreHolidaysByYear($date));
57
    }
58
59
    /**
60
     * @param $date
61
     * @return bool
62
     */
63
    public static function isHoliday($date = null)
64
    {
65
        return (static::isWeekend($date) && !static::isPreHoliday($date)) || static::findDateInArray($date, static::getHolidaysByYear($date));
66
    }
67
68
    public static function isWeekend($date, $weekend = [6, 0])
69
    {
70
        $date = static::prepareDate($date);
71
        return in_array($date->format('w'), $weekend);
72
    }
73
74
    /**
75
     * @param integer|string|\DateTime $year
76
     * @return array
77
     */
78
    public static function getHolidaysByYear($year)
79
    {
80
        if (!is_numeric($year)) {
81
            $year = static::prepareDate($year)->format('Y');
82
        }
83
        $holidays = static::getHolidays();
84
        return isset($holidays[$year]['holidays']) ? $holidays[$year]['holidays'] : [];
85
    }
86
87
    /**
88
     * @param integer|string|\DateTime $year
89
     * @return array
90
     */
91
    public static function getWorkingsByYear($year)
92
    {
93
        if (!is_numeric($year)) {
94
            $year = static::prepareDate($year)->format('Y');
95
        }
96
        $holidays = static::getHolidays();
97
        return isset($holidays[$year]['workings']) ? $holidays[$year]['workings'] : [];
98
    }
99
100
    /**
101
     * @param integer|string|\DateTime $year
102
     * @return array
103
     */
104
    public static function getPreHolidaysByYear($year)
105
    {
106
        if (!is_numeric($year)) {
107
            $year = static::prepareDate($year)->format('Y');
108
        }
109
        $holidays = static::getHolidays();
110
        return isset($holidays[$year]['preholidays']) ? $holidays[$year]['preholidays'] : [];
111
    }
112
113
    /**
114
     * @return $this
115
     */
116
    public function day()
117
    {
118
        return $this;
119
    }
120
121
    /**
122
     * @param null|string $format
123
     * @return string
124
     */
125
    public function format($format = null)
126
    {
127
        return $this->date()->format($format ? $format : $this->format);
128
    }
129
130
    /**
131
     * @return $this
132
     */
133
    public function working()
134
    {
135
        while (!static::isWorking(static::$date)) {
0 ignored issues
show
Bug introduced by
Since $date is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $date to at least protected.
Loading history...
136
            $this->next();
137
        }
138
        return $this;
139
    }
140
141
    /**
142
     * @return $this
143
     */
144
    public function holiday()
145
    {
146
        while (!static::isHoliday(static::$date) && static::haveData()) {
0 ignored issues
show
Bug introduced by
Since $date is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $date to at least protected.
Loading history...
147
            $this->next();
148
        }
149
        return $this;
150
    }
151
152
    protected static function haveData($date = null)
153
    {
154
        $date = $date ? static::prepareDate($date) : static::date();
155
        return isset(static::getHolidays()[$date->format('Y')]);
156
157
    }
158
159
    /**
160
     * @return $this
161
     */
162
    public function preHoliday()
163
    {
164
        while (!static::isPreHoliday(static::$date) && static::haveData($this->date())) {
0 ignored issues
show
Bug introduced by
Since $date is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $date to at least protected.
Loading history...
165
            $this->next();
166
        }
167
        return $this;
168
    }
169
170
    /**
171
     * @return $this
172
     */
173
    public function next()
174
    {
175
        static::$date->add(new \DateInterval('P1D'));
0 ignored issues
show
Bug introduced by
Since $date is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $date to at least protected.
Loading history...
176
        return $this;
177
    }
178
179
    /**
180
     * @return $this
181
     */
182
    public function prev()
183
    {
184
        static::$date->sub(new \DateInterval('P1D'));
0 ignored issues
show
Bug introduced by
Since $date is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $date to at least protected.
Loading history...
185
        return $this;
186
    }
187
188
    public static function getInstance()
189
    {
190
        return static::$_instance ?: static::find();
0 ignored issues
show
Bug introduced by
Since $_instance is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $_instance to at least protected.
Loading history...
191
    }
192
193
    /**
194
     * @param null|string|\DateTime $date
195
     * @return Calendar
196
     */
197
    public static function find($date = null)
198
    {
199
        static::$date = static::prepareDate($date);
0 ignored issues
show
Bug introduced by
Since $date is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $date to at least protected.
Loading history...
200
        if (static::$_instance) {
0 ignored issues
show
Bug introduced by
Since $_instance is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $_instance to at least protected.
Loading history...
201
            return static::$_instance;
202
        } else {
203
            $json = file_get_contents(__DIR__ . '/holidays.json');
204
            static::$_instance = new self();
205
            static::$holidays = json_decode($json, true);
206
            return static::$_instance;
207
        }
208
    }
209
210
    /**
211
     * @param string|\DateTime $date
212
     * @return \DateTime
213
     */
214
    protected static function prepareDate($date)
215
    {
216
        if (is_null($date) && static::$date) {
0 ignored issues
show
Bug introduced by
Since $date is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $date to at least protected.
Loading history...
217
            $date = static::$date;
218
        } elseif (!$date instanceof \DateTime) {
219
            $date = new \DateTime($date);
220
        }
221
        return $date;
222
    }
223
224
    public static function getHolidays()
225
    {
226
        if (!static::$_instance) {
0 ignored issues
show
Bug introduced by
Since $_instance is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $_instance to at least protected.
Loading history...
227
            static::find();
228
        }
229
        return static::$holidays;
230
    }
231
232
    private function __construct()
233
    {
234
    }
235
236
    protected function __clone()
237
    {
238
    }
239
}