Passed
Pull Request — master (#3)
by Chad
02:40
created

TimeOfDayFilter::filter()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 5
nc 3
nop 1
dl 0
loc 11
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace TraderInteractive\Filter;
4
5
use TraderInteractive\Exceptions\FilterException;
6
7
final class TimeOfDayFilter
8
{
9
    /**
10
     * @var string
11
     */
12
    const TIME_OF_DAY_REGEX = '/^(0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]$/';
13
14
    /**
15
     * Filters a given value as a valid HH:MM:SS formatted string.
16
     *
17
     * @param mixed $value the value to be filtered.
18
     *
19
     * @return string
20
     *
21
     * @throws FilterException Thrown if the value cannot be filtered.
22
     */
23
    public static function filter($value) : string
24
    {
25
        if (!is_string($value) || trim($value) === '') {
26
            throw new FilterException('$value must be a non-empty string');
27
        }
28
29
        if (preg_match(self::TIME_OF_DAY_REGEX, $value) === 0) {
30
            throw new FilterException('$value must be in the correct format HH:MM:SS');
31
        }
32
33
        return $value;
34
    }
35
}
36