DateTime::validateDate()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 1
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Mbright\Validation\Rule\Validate\MySql;
4
5
use Mbright\Validation\Rule\Validate\ValidateRuleInterface;
6
7
/**
8
 * Validates that data can be inserted into one of the following column types:
9
 * - Time
10
 */
11
class DateTime implements ValidateRuleInterface
12
{
13
    use DateTypeTrait, TimeTypeTrait;
14
15
    /**
16
     * * Indicates if the given field's value is a MySql safe DateTime.
17
     *
18
     * @param object $subject
19
     * @param string $field
20
     *
21
     * @return bool
22
     */
23 36
    public function __invoke($subject, string $field): bool
24
    {
25 36
        $value = $subject->$field;
26
27 36
        if (!is_string($value)) {
28
            return false;
29
        }
30
31 36
        $dateTimeDelimiter = substr($value, 10, 1);
32
33 36
        if (! in_array($dateTimeDelimiter, [' ', 'T'])) {
34 9
            return false;
35
        }
36
37 27
        $separated = explode($dateTimeDelimiter, $value);
38
39 27
        return $this->validateDate($separated[0]) && $this->validateTime($separated[1]);
40
    }
41
42
    /**
43
     * Validates that the string can represent a date.
44
     *
45
     * Valid formats include: YY-MM-DD and YYYY-MM-DD.
46
     *
47
     * @param string $dateString
48
     *
49
     * @return bool
50
     */
51 27
    protected function validateDate(string $dateString): bool
52
    {
53 27
        $dateParts = $this->extractDateParts($dateString, '[^[:punct:]]', true);
54
55 27
        return !is_null($dateParts) && checkdate($dateParts->month, $dateParts->day, $dateParts->year);
56
    }
57
58
    /**
59
     * Validates that the string can represent a time.
60
     *
61
     * @param string $timeString
62
     *
63
     * @return bool
64
     */
65 21
    protected function validateTime(string $timeString): bool
66
    {
67 21
        $timeParts = $this->extractTimeParts($timeString);
68
69 21
        return ! is_null($timeParts)
70 21
            && $this->validateHours($timeParts->hours)
71 21
            && $this->validateMinutes($timeParts->minutes)
72 21
            && $this->validateSeconds($timeParts->seconds);
73
    }
74
75
    /**
76
     * Validates that the string can represent seconds.
77
     *
78
     * @param string $seconds
79
     *
80
     * @return bool
81
     */
82 12
    protected function validateSeconds(string $seconds): bool
83
    {
84 12
        $precision = strlen(substr(strrchr($seconds, '.'), 1));
85
86 12
        if ($precision > 6) {
87
            return false;
88
        }
89
90 12
        $seconds = (float) $seconds;
91
92 12
        return $seconds >= 0 && $seconds < 60;
93
    }
94
95
    /**
96
     * Validates that the string can represent minutes.
97
     *
98
     * @param string $minutes
99
     *
100
     * @return bool
101
     */
102 18
    protected function validateMinutes(string $minutes): bool
103
    {
104 18
        $minutes = (int) $minutes;
105
106 18
        return $minutes >= 0 && $minutes < 60;
107
    }
108
109
    /**
110
     * Validates tha the string can represent hours.
111
     *
112
     * @param string $hours
113
     *
114
     * @return bool
115
     */
116 21
    protected function validateHours(string $hours): bool
117
    {
118 21
        $hours = (int) $hours;
119
120 21
        return $hours >= 0 && $hours <= 23;
121
    }
122
}
123