Failed Conditions
Pull Request — master (#75)
by
unknown
03:37
created

DateTimeZone::filter()   C

Complexity

Conditions 9
Paths 6

Size

Total Lines 24
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 5.3563
c 0
b 0
f 0
cc 9
eloc 13
nc 6
nop 2
1
<?php
2
3
namespace TraderInteractive\Filter;
4
5
/**
6
 * A collection of filters for filtering strings into \DateTimeZone objects.
7
 */
8
class DateTimeZone
9
{
10
    /**
11
     * Filters the given value into a \DateTimeZone object.
12
     *
13
     * @param mixed   $value     The value to be filtered.
14
     * @param boolean $allowNull True to allow nulls through, and false (default) if nulls should not be allowed.
15
     *
16
     * @return \DateTimeZone
17
     *
18
     * @throws \InvalidArgumentException Thrown if $allowNull was not a boolean value.
19
     * @throws Exception if the value did not pass validation.
20
     */
21
    public static function filter($value, $allowNull = false)
22
    {
23
        if ($allowNull !== false && $allowNull !== true) {
24
            throw new \InvalidArgumentException('$allowNull was not a boolean value');
25
        }
26
27
        if ($value === null && $allowNull) {
28
            return null;
29
        }
30
31
        if ($value instanceof \DateTimeZone) {
32
            return $value;
33
        }
34
35
        if (!is_string($value) || trim($value) == '') {
36
            throw new Exception('$value not a non-empty string');
37
        }
38
39
        try {
40
            return new \DateTimeZone($value);
41
        } catch (\Exception $e) {
42
            throw new Exception($e->getMessage(), $e->getCode(), $e);
43
        }
44
    }
45
}
46