AbstractDateTime::newDateTime()   B
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 25
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 25
ccs 12
cts 12
cp 1
rs 8.439
c 0
b 0
f 0
cc 5
eloc 12
nc 5
nop 1
crap 5
1
<?php
2
/**
3
 *
4
 * This file is part of Aura for PHP.
5
 *
6
 * @license http://opensource.org/licenses/bsd-license.php BSD
7
 *
8
 */
9
namespace Aura\Filter\Rule;
10
11
use DateTime;
12
13
/**
14
 *
15
 * Abstract rule for date-time filters.
16
 *
17
 * @package Aura.Filter
18
 *
19
 */
20
abstract class AbstractDateTime
21
{
22
    /**
23
     *
24
     * Returns a new DateTime object.
25
     *
26
     * @param mixed $value The incoming date/time value.
27
     *
28
     * @return mixed If the value is already a DateTime then it is returned
29
     * as-is; if the value is invalid as a date/time then `false` is returned;
30
     * otherwise, a new DateTime is constructed from the value and returned.
31
     *
32
     */
33 22
    protected function newDateTime($value)
34
    {
35 22
        if ($value instanceof DateTime) {
36 2
            return $value;
37
        }
38
39 20
        if (! is_scalar($value)) {
40 2
            return false;
41
        }
42
43 18
        if (trim($value) === '') {
44 3
            return false;
45
        }
46
47 15
        $datetime = date_create($value);
48
49
        // invalid dates (like 1979-02-29) show up as warnings.
50 15
        $errors = DateTime::getLastErrors();
51 15
        if ($errors['warnings']) {
52 4
            return false;
53
        }
54
55
        // looks OK
56 11
        return $datetime;
57
    }
58
}
59