Completed
Push — develop ( 44c7a3...43ed67 )
by John
02:07
created

Date::getUSValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Alpha\Model\Type;
4
5
use Alpha\Util\Helper\Validator;
6
use Alpha\Exception\IllegalArguementException;
7
use Alpha\Util\Config\ConfigProvider;
8
use \DateTime;
9
10
/**
11
 * The Date complex data type.
12
 *
13
 * @since 1.0
14
 *
15
 * @author John Collins <[email protected]>
16
 * @license http://www.opensource.org/licenses/bsd-license.php The BSD License
17
 * @copyright Copyright (c) 2018, John Collins (founder of Alpha Framework).
18
 * All rights reserved.
19
 *
20
 * <pre>
21
 * Redistribution and use in source and binary forms, with or
22
 * without modification, are permitted provided that the
23
 * following conditions are met:
24
 *
25
 * * Redistributions of source code must retain the above
26
 *   copyright notice, this list of conditions and the
27
 *   following disclaimer.
28
 * * Redistributions in binary form must reproduce the above
29
 *   copyright notice, this list of conditions and the
30
 *   following disclaimer in the documentation and/or other
31
 *   materials provided with the distribution.
32
 * * Neither the name of the Alpha Framework nor the names
33
 *   of its contributors may be used to endorse or promote
34
 *   products derived from this software without specific
35
 *   prior written permission.
36
 *
37
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
38
 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
39
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
40
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
41
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
42
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
43
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
44
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
45
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
46
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
47
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
48
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
49
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
50
 * </pre>
51
 */
52
class Date extends Type implements TypeInterface
53
{
54
    /**
55
     * The year part.
56
     *
57
     * @var string
58
     *
59
     * @since 1.0
60
     */
61
    private $year;
62
63
    /**
64
     * The month part.
65
     *
66
     * @var string
67
     *
68
     * @since 1.0
69
     */
70
    private $month;
71
72
    /**
73
     * The day part.
74
     *
75
     * @var string
76
     *
77
     * @since 1.0
78
     */
79
    private $day;
80
81
    /**
82
     * The textual version of the day, e.g. Monday.
83
     *
84
     * @var string
85
     *
86
     * @since 1.0
87
     */
88
    private $weekday;
89
90
    /**
91
     * The validation rule (reg-ex) applied to Date values.
92
     *
93
     * @var string
94
     *
95
     * @since 1.0
96
     */
97
    private $validationRule;
98
99
    /**
100
     * The error message returned for invalid values.
101
     *
102
     * @var string
103
     *
104
     * @since 1.0
105
     */
106
    protected $helper = 'Not a valid date value!  A date should be in the ISO format YYYY-MM-DD.';
107
108
    /**
109
     * Constructor.  Leave $date param empty to default to now.
110
     *
111
     * @param string $date Date string in the ISO format YYYY-MM-DD.
112
     *
113
     * @since 1.0
114
     *
115
     * @throws \Alpha\Exception\IllegalArguementException
116
     */
117
    public function __construct($date = '')
118
    {
119
        $config = ConfigProvider::getInstance();
120
121
        $this->validationRule = Validator::ALLOW_ALL;
122
123
        if (empty($date)) {
124
            if ($config->get('app.default.datetime') == 'now') {
125
                $this->year = date('Y');
126
                $this->month = date('m');
127
                $this->day = date('d');
128
                $this->weekday = date('l');
129
            } else {
130
                $this->year = '0000';
131
                $this->month = '00';
132
                $this->day = '00';
133
            }
134
        } else {
135
            if (preg_match($this->validationRule, $date)) {
136
                $this->populateFromString($date);
137
            } else {
138
                throw new IllegalArguementException($this->helper);
139
            }
140
        }
141
    }
142
143
    /**
144
     * Accepts a full date string in ISO YYYY-mm-dd format and populates relevent Date attributes.
145
     *
146
     * @param string $date
147
     *
148
     * @since 1.0
149
     *
150
     * @throws \Alpha\Exception\IllegalArguementException
151
     */
152
    public function setValue($date)
153
    {
154
        $this->populateFromString($date);
155
    }
156
157
    /**
158
     * Set the Date attributes to match the three values provided.
159
     *
160
     * @param int $year
161
     * @param int $month
162
     * @param int $day
163
     *
164
     * @throws \Alpha\Exception\IllegalArguementException
165
     *
166
     * @since 1.0
167
     */
168
    public function setDateValue($year, $month, $day)
169
    {
170
        $valid = null;
171
172
        if (!preg_match('/^[0-9]{4}$/', $year)) {
173
            $valid = 'Error: the year value '.$year.' provided is invalid!';
174
        }
175
        if (!isset($valid) && !preg_match('/^[0-9]{1,2}$/', $month)) {
176
            $valid = 'Error: the month value '.$month.' provided is invalid!';
177
        }
178
        if (!isset($valid) && !preg_match('/^[0-9]{1,2}$/', $day)) {
179
            $valid = 'Error: the day value '.$day.' provided is invalid!';
180
        }
181
        if (!isset($valid) && !checkdate($month, $day, $year)) {
182
            $valid = 'Error: the day value '.$year.'-'.$month.'-'.$day.' provided is invalid!';
183
        }
184
185
        if (isset($valid)) {
186
            throw new IllegalArguementException($valid);
187
        } else {
188
            $this->year = $year;
0 ignored issues
show
Documentation Bug introduced by
The property $year was declared of type string, but $year is of type integer. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
189
            $this->month = str_pad($month, 2, '0', STR_PAD_LEFT);
190
            $this->day = str_pad($day, 2, '0', STR_PAD_LEFT);
191
            $unixTime = mktime(0, 0, 0, $this->month, $this->day, $this->year);
192
            $this->weekday = date('l', $unixTime);
193
        }
194
    }
195
196
    /**
197
     * Get the date value as a string in the format "YYYY-MM-DD".
198
     *
199
     * @return string
200
     *
201
     * @since 1.0
202
     */
203
    public function getValue()
204
    {
205
        return $this->year.'-'.$this->month.'-'.$this->day;
206
    }
207
208
    /**
209
     * Return the value in UNIX timestamp format.
210
     *
211
     * @return int
212
     *
213
     * @since 1.0
214
     */
215
    public function getUnixValue()
216
    {
217
        return mktime(0, 0, 0, $this->month, $this->day, $this->year);
218
    }
219
220
    /**
221
     * Get the date value as a string in the format "DD/MM/YYYY".
222
     *
223
     * @return string
224
     *
225
     * @since 1.0
226
     */
227
    public function getEuroValue()
228
    {
229
        return $this->day.'/'.$this->month.'/'.mb_substr($this->year, 2, 2);
230
    }
231
232
    /**
233
     * Get the date value as a string in the format "MM/DD/YYYY".
234
     *
235
     * @return string
236
     *
237
     * @since 1.0
238
     */
239
    public function getUSValue()
240
    {
241
        return $this->month.'/'.$this->day.'/'.mb_substr($this->year, 2, 2);
242
    }
243
244
    /**
245
     * Get the year part.
246
     *
247
     * @return string
248
     *
249
     * @since 1.0
250
     */
251
    public function getYear()
252
    {
253
        return $this->year;
254
    }
255
256
    /**
257
     * Get the month part.
258
     *
259
     * @return string
260
     *
261
     * @since 1.0
262
     */
263
    public function getMonth()
264
    {
265
        return $this->month;
266
    }
267
268
    /**
269
     * Get the month part.
270
     *
271
     * @return string
272
     *
273
     * @since 3.1
274
     */
275
    public function getMonthName()
276
    {
277
        return $this->monthName;
0 ignored issues
show
Bug introduced by
The property monthName does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
278
    }
279
280
    /**
281
     * Get the day part.
282
     *
283
     * @return string
284
     *
285
     * @since 1.0
286
     */
287
    public function getDay()
288
    {
289
        return $this->day;
290
    }
291
292
    /**
293
     * Get the textual weekday part, e.g. Monday.
294
     *
295
     * @return string
296
     *
297
     * @since 1.0
298
     */
299
    public function getWeekday()
300
    {
301
        return $this->weekday;
302
    }
303
304
    /**
305
     * Accepts a full date string in YYYY-MM-DD format and populates relevent Date attributes.
306
     *
307
     * @param string $date
308
     *
309
     * @throws \Alpha\Exception\IllegalArguementException
310
     *
311
     * @since 1.0
312
     */
313
    public function populateFromString($date)
314
    {
315
        $valid = null;
316
317
        if ($date == '' || $date == '0000-00-00') {
318
            $this->year = '0000';
319
            $this->month = '00';
320
            $this->day = '00';
321
        } else {
322
            // This is just here for legacy to ensure that any old time value from a Date object is ignored
323
            $spilt_by_space = explode(' ', $date);
324
325
            if (isset($spilt_by_space[0])) {
326
                $date = $spilt_by_space[0];
327
            } else {
328
                throw new IllegalArguementException('Invalid Date value ['.$date.'] provided!');
329
            }
330
331
            $split_by_dash = explode('-', $date);
332
333
            // Parse for the date parts, seperated by "-"
334
            if (isset($split_by_dash[0]) && isset($split_by_dash[1]) && isset($split_by_dash[2])) {
335
                $year = $split_by_dash[0];
336
                $month = $split_by_dash[1];
337
                $day = $split_by_dash[2];
338
            } else {
339
                throw new IllegalArguementException('Invalid Date value ['.$date.'] provided!');
340
            }
341
342
            if (!preg_match('/^[0-9]{4}$/', $year)) {
343
                $valid = 'Error: the year value '.$year.' provided is invalid!';
344
            }
345
            if (!isset($valid) && !preg_match('/^[0-9]{1,2}$/', $month)) {
346
                $valid = 'Error: the month value '.$month.' provided is invalid!';
347
            }
348
            if (!isset($valid) && !preg_match('/^[0-9]{1,2}$/', $day)) {
349
                $valid = 'Error: the day value '.$day.' provided is invalid!';
350
            }
351
            if (!isset($valid) && !checkdate($month, $day, $year)) {
352
                $valid = 'Error: the date value '.$year.'-'.$month.'-'.$day.' provided is invalid!';
353
            }
354
355
            if (isset($valid)) {
356
                throw new IllegalArguementException($valid);
357
            } else {
358
                $this->year = $year;
359
                $this->month = str_pad($month, 2, '0', STR_PAD_LEFT);
360
                $this->day = str_pad($day, 2, '0', STR_PAD_LEFT);
361
                $unixTime = mktime(0, 0, 0, $this->month, $this->day, $this->year);
362
                $this->weekday = date('l', $unixTime);
363
            }
364
        }
365
    }
366
367
    /**
368
     * Get the validation rule.
369
     *
370
     * @return string
371
     *
372
     * @since 1.0
373
     */
374
    public function getRule()
375
    {
376
        return $this->validationRule;
377
    }
378
379
    /**
380
     * Set the validation rule.
381
     *
382
     * @param string $rule
383
     *
384
     * @since 1.0
385
     */
386
    public function setRule($rule)
387
    {
388
        $this->validationRule = $rule;
389
    }
390
391
    /**
392
     *
393
     * Increment the cunrrent date by the amount provided
394
     *
395
     * @param string $amount The amount to increment the date by, e.g. "1 day"
396
     *
397
     * @since 3.1.0
398
     */
399
    public function increment($amount)
400
    {
401
        $date = strtotime($amount, strtotime($this->getValue()));
402
        $this->setValue(date("Y-m-d", $date));
403
    }
404
405
    /**
406
     *
407
     * Get the start date and the end date of the week of the year provided
408
     *
409
     * @param int The number of the week (1-52)
410
     * @param int The year (YYYY)
411
     *
412
     * @return array An array containing the "start" date and "end" date.
413
     *
414
     * @since 3.1.0
415
     */
416
    public static function getStartAndEndDate($week, $year)
417
    {
418
        $dateTime = new DateTime();
419
        $dateTime->setISODate($year, $week);
420
421
        $value = array();
422
423
        $value['start'] = $dateTime->format('Y-m-d');
424
        $dateTime->modify('+6 days');
425
        $value['end'] = $dateTime->format('Y-m-d');
426
        
427
        return $value;
428
    }
429
}
430