TimeOfDayFilter   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 9
c 1
b 0
f 0
dl 0
loc 37
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A filter() 0 11 4
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 NON_EMPTY_STRING_ERROR = 'Value must be a non-empty string';
13
14
    /**
15
     * @var string
16
     */
17
    const INCORRECT_FORMAT_ERROR = 'Value must be in the format HH:MM:SS';
18
19
    /**
20
     * @var string
21
     */
22
    const TIME_OF_DAY_REGEX = '/^(0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]$/';
23
24
    /**
25
     * Filters a given value as a valid HH:MM:SS formatted string.
26
     *
27
     * @param mixed $value the value to be filtered.
28
     *
29
     * @return string
30
     *
31
     * @throws FilterException Thrown if the value cannot be filtered.
32
     */
33
    public static function filter($value) : string
34
    {
35
        if (!is_string($value) || trim($value) === '') {
36
            throw new FilterException(self::NON_EMPTY_STRING_ERROR);
37
        }
38
39
        if (preg_match(self::TIME_OF_DAY_REGEX, $value) === 0) {
40
            throw new FilterException(self::INCORRECT_FORMAT_ERROR);
41
        }
42
43
        return $value;
44
    }
45
}
46