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

IsDateString   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
c 0
b 0
f 0
lcom 0
cbo 1
dl 0
loc 36
ccs 11
cts 11
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 4 1
A isValidDateString() 0 18 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