DatetimeValidator   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 92.31%

Importance

Changes 9
Bugs 1 Features 0
Metric Value
wmc 7
c 9
b 1
f 0
lcom 1
cbo 1
dl 0
loc 43
ccs 12
cts 13
cp 0.9231
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A formatDate() 0 6 2
B formatDefault() 0 20 5
1
<?php
2
namespace JsonTable\Validate\Format;
3
4
use \JsonTable\Validate\AbstractFormatValidator;
5
6
/**
7
 * Lexical datetime validator.
8
 *
9
 * @package JSON table
10
 */
11
class DatetimeValidator extends AbstractFormatValidator
12
{
13
    /**
14
     * Validate that the input is a valid ISO8601 formatted date.
15
     *
16
     * @return  boolean Whether the input is valid.
17
     */
18 27
    protected function formatDefault()
19
    {
20 27
        if (1 === preg_match('/^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})Z$/', $this->input)) {
21
            return (false !== \DateTime::createFromFormat(DATE_ISO8601, $this->input));
22
        }
23
24 27
        if ($this->formatDate('Y-m-d H:i:s')) {
25 4
            return true;
26
        }
27
28 26
        if ($this->formatDate('Y-m-d')) {
29 4
            return true;
30
        }
31
32 26
        if ($this->formatDate('H:i:s')) {
33 4
            return true;
34
        }
35
36 22
        return false;
37
    }
38
39
40
    /**
41
     * Validate that the input is a valid date in the specified format.
42
     *
43
     * @param   string   $format   The date format to validate against.
44
     *
45
     * @return  boolean            Whether the input is valid.
46
     */
47 35
    protected function formatDate($format)
48
    {
49 35
        $date = \DateTime::createFromFormat($format, $this->input);
50
51 35
        return $date && ($date->format($format) === $this->input);
52
    }
53
}
54