1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Mbright\Validation\Rule\Validate\MySql; |
4
|
|
|
|
5
|
|
|
trait TimeTypeTrait |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* Extracts time parts from the given string. |
9
|
|
|
* |
10
|
|
|
* Will return an object with 3 properties, hours, minutes, seconds with the values from the string. Each property |
11
|
|
|
* defaults to zero. If the string cannot be parsed, return null. |
12
|
|
|
* |
13
|
|
|
* @param $value |
14
|
|
|
* |
15
|
|
|
* @return ?\stdClass |
|
|
|
|
16
|
|
|
*/ |
17
|
57 |
|
protected function extractTimeParts($value): ?\stdClass |
18
|
|
|
{ |
19
|
|
|
$extractedParts = (object) [ |
20
|
57 |
|
'hours' => 0, |
21
|
|
|
'minutes' => 0, |
22
|
|
|
'seconds'=> 0 |
23
|
|
|
]; |
24
|
|
|
|
25
|
57 |
|
$timeSegments = explode(':', $value); |
26
|
57 |
|
$numOfSegments = count($timeSegments); |
27
|
|
|
|
28
|
57 |
|
if ($numOfSegments === 3) { |
29
|
45 |
|
$extractedParts->hours = $timeSegments[0]; |
30
|
45 |
|
$extractedParts->minutes = $timeSegments[1]; |
31
|
45 |
|
$extractedParts->seconds = $timeSegments[2]; |
32
|
12 |
|
} elseif ($numOfSegments === 2) { |
33
|
|
|
$extractedParts->hours = $timeSegments[0]; |
34
|
|
|
$extractedParts->minutes = $timeSegments[1]; |
35
|
12 |
|
} elseif ($numOfSegments === 1) { |
36
|
|
|
// if a 3rd character is present and equal to . then we have a SS format segment else it has to be HHMMSS |
37
|
9 |
|
$isSecondsOnly = substr($timeSegments[0], 2, 1) !== '.'; |
38
|
9 |
|
if ($isSecondsOnly) { |
39
|
9 |
|
$extractedParts->seconds = $timeSegments[0]; |
40
|
|
|
} else { |
41
|
9 |
|
$extractedParts = $this->handleNonDelimitedString($timeSegments[0], $extractedParts); |
42
|
|
|
} |
43
|
|
|
} else { |
44
|
3 |
|
$extractedParts = null; |
45
|
|
|
} |
46
|
|
|
|
47
|
57 |
|
return $extractedParts; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Handles HHHMMSS formatted strings. |
52
|
|
|
* |
53
|
|
|
* @param string $timeString |
54
|
|
|
* @param \stdClass $extractedParts |
55
|
|
|
* |
56
|
|
|
* @return null|\stdClass |
57
|
|
|
*/ |
58
|
|
|
private function handleNonDelimitedString(string $timeString, \stdClass $extractedParts) |
59
|
|
|
{ |
60
|
|
|
$parsedSegments = []; |
61
|
|
|
preg_match('(^\d{2})(\d{2})(\d{2}\.\d+$|\d{2}$)', $timeString, $parsedSegments); |
62
|
|
|
|
63
|
|
|
if (count($parsedSegments) !== 4) { |
64
|
|
|
return null; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
$extractedParts->hours = $parsedSegments[0]; |
68
|
|
|
$extractedParts->minutes = $parsedSegments[1]; |
69
|
|
|
$extractedParts->seconds = $parsedSegments[2]; |
70
|
|
|
|
71
|
|
|
return $extractedParts; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Validates that the string can represent seconds. |
76
|
|
|
* |
77
|
|
|
* @param string $seconds |
78
|
|
|
* |
79
|
|
|
* @return bool |
80
|
|
|
*/ |
81
|
|
|
protected function validateSeconds(string $seconds): bool |
82
|
|
|
{ |
83
|
|
|
$precision = strlen(substr(strrchr($seconds, '.'), 1)); |
84
|
|
|
|
85
|
|
|
if ($precision > 6) { |
86
|
|
|
return false; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$seconds = (float) $seconds; |
90
|
|
|
|
91
|
|
|
return $seconds >= 0 && $seconds < 60; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Validates that the string can represent minutes. |
96
|
|
|
* |
97
|
|
|
* @param string $minutes |
98
|
|
|
* |
99
|
|
|
* @return bool |
100
|
|
|
*/ |
101
|
|
|
protected function validateMinutes(string $minutes): bool |
102
|
|
|
{ |
103
|
|
|
$minutes = (int) $minutes; |
104
|
|
|
|
105
|
|
|
return $minutes >= 0 && $minutes < 60; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Validates tha the string can represent hours. |
110
|
|
|
* |
111
|
|
|
* @param string $hours |
112
|
|
|
* |
113
|
|
|
* @return bool |
114
|
|
|
*/ |
115
|
|
|
protected function validateHours(string $hours): bool |
116
|
|
|
{ |
117
|
|
|
$hours = (int) $hours; |
118
|
|
|
|
119
|
|
|
return $hours >= -838 && $hours <= 838; |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.