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

DateTimeZone   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 9
lcom 0
cbo 1
dl 0
loc 38
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
C filter() 0 24 9
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