Time   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 17
dl 0
loc 69
ccs 19
cts 19
cp 1
rs 10
c 0
b 0
f 0
wmc 11

4 Methods

Rating   Name   Duplication   Size   Complexity  
A validateHours() 0 5 2
A validateSeconds() 0 11 3
A __invoke() 0 9 4
A validateMinutes() 0 5 2
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 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
76
    {
77 33
        $hours = (int) $hours;
78
79 33
        return $hours >= -838 && $hours <= 838;
80
    }
81
}
82