Passed
Pull Request — master (#2)
by Chad
03:27
created

TimeSpanFilter::filter()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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