Completed
Push — master ( 1358d6...984c1e )
by Karsten
01:40
created

IsDateString::isValidDateString()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 18
ccs 9
cts 9
cp 1
rs 9.2
cc 4
eloc 9
nc 4
nop 1
crap 4
1
<?php
2
/**
3
 * File was created 01.10.2015 18:02
4
 *
5
 * @author Karsten J. Gerber <[email protected]>
6
 */
7
namespace PeekAndPoke\Component\Psi\Psi;
8
9
use PeekAndPoke\Component\Psi\Functions\Unary\AbstractUnaryFunction;
10
11
/**
12
 * IsDateString
13
 *
14
 * @author Karsten J. Gerber <[email protected]>
15
 */
16
class IsDateString extends AbstractUnaryFunction
17
{
18
    /**
19
     * @param mixed $input
20
     *
21
     * @return bool
22
     */
23 22
    public function __invoke($input)
24
    {
25 22
        return self::isValidDateString($input);
26
    }
27
28
    /**
29
     * @param string    $str
30
     *
31
     * @return bool
32
     */
33 22
    public static function isValidDateString($str)
34
    {
35 22
        if (!is_string($str)) {
36 3
            return false;
37
        }
38
39 19
        if (preg_match('/^(\d{4})-(\d{2})-(\d{2})T((\d{2}):(\d{2}):(\d{2})(\.\d{3}(Z)?)?([+-]\d{2}:\d{2})?)$/', $str)) {
40 10
            return true;
41
        }
42
43 9
        $stamp = strtotime($str);
44
45 9
        if (!is_numeric($stamp)) {
46 4
            return false;
47
        }
48
49 5
        return checkdate(date('m', $stamp), date('d', $stamp), date('Y', $stamp));
50
    }
51
}
52