Time   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 13
c 2
b 0
f 0
dl 0
loc 46
rs 10
wmc 12

2 Methods

Rating   Name   Duplication   Size   Complexity  
A isValid() 0 21 6
A checkTime() 0 3 6
1
<?php
2
3
namespace Kontrolio\Rules\Core;
4
5
use DateTime as PhpDateTime;
6
use Kontrolio\Rules\AbstractRule;
7
8
/**
9
 * Time validation rule.
10
 *
11
 * @package Kontrolio\Rules\Core
12
 */
13
class Time extends AbstractRule
14
{
15
    const PATTERN = '/^(\d{2}):(\d{2}):(\d{2})$/';
16
17
    /**
18
     * Validates input.
19
     *
20
     * @param mixed $input
21
     *
22
     * @return bool
23
     */
24
    public function isValid($input = null)
25
    {
26
        if ($input === null || $input === '' || $input instanceof PhpDateTime) {
27
            return false;
28
        }
29
30
        $input = (string) $input;
31
32
        if (!preg_match(static::PATTERN, $input, $matches)) {
33
            $this->violations[] = 'format';
34
35
            return false;
36
        }
37
38
        if (!self::checkTime($matches[1], $matches[2], $matches[3])) {
39
            $this->violations[] = 'time';
40
41
            return false;
42
        }
43
44
        return true;
45
    }
46
47
    /**
48
     * Checks whether a time is valid.
49
     *
50
     * @param int $hour
51
     * @param int $minute
52
     * @param int $second
53
     *
54
     * @return bool
55
     */
56
    protected static function checkTime($hour, $minute, $second)
57
    {
58
        return $hour >= 0 && $hour < 24 && $minute >= 0 && $minute < 60 && $second >= 0 && $second < 60;
59
    }
60
}
61