| Total Complexity | 11 |
| Total Lines | 69 |
| Duplicated Lines | 0 % |
| Coverage | 100% |
| Changes | 0 | ||
| 1 | <?php |
||
| 11 | class Time implements ValidateRuleInterface |
||
| 12 | { |
||
| 13 | use TimeTypeTrait; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * Indicates if the given field's value is a MySql safe Time. |
||
| 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 | 36 | $timeParts = $this->extractTimeParts($value); |
|
| 27 | |||
| 28 | 36 | return ! is_null($timeParts) |
|
| 29 | 36 | && $this->validateHours($timeParts->hours) |
|
| 30 | 36 | && $this->validateMinutes($timeParts->minutes) |
|
| 31 | 36 | && $this->validateSeconds($timeParts->seconds); |
|
| 32 | } |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Validates that the string can represent seconds. |
||
| 36 | * |
||
| 37 | * @param string $seconds |
||
| 38 | * |
||
| 39 | * @return bool |
||
| 40 | */ |
||
| 41 | 27 | protected function validateSeconds(string $seconds): bool |
|
| 42 | { |
||
| 43 | 27 | $precision = strlen(substr(strrchr($seconds, '.'), 1)); |
|
| 44 | |||
| 45 | 27 | if ($precision > 6) { |
|
| 46 | 6 | return false; |
|
| 47 | } |
||
| 48 | |||
| 49 | 21 | $seconds = (float) $seconds; |
|
| 50 | |||
| 51 | 21 | return $seconds >= 0 && $seconds < 60; |
|
| 52 | } |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Validates that the string can represent minutes. |
||
| 56 | * |
||
| 57 | * @param string $minutes |
||
| 58 | * |
||
| 59 | * @return bool |
||
| 60 | */ |
||
| 61 | 27 | protected function validateMinutes(string $minutes): bool |
|
| 62 | { |
||
| 63 | 27 | $minutes = (int) $minutes; |
|
| 64 | |||
| 65 | 27 | return $minutes >= 0 && $minutes < 60; |
|
| 66 | } |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Validates tha the string can represent hours. |
||
| 70 | * |
||
| 71 | * @param string $hours |
||
| 72 | * |
||
| 73 | * @return bool |
||
| 74 | */ |
||
| 75 | 33 | protected function validateHours(string $hours): bool |
|
| 80 | } |
||
| 81 | } |
||
| 82 |