DateTimeZone   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 8
lcom 0
cbo 0
dl 0
loc 34
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B filter() 0 20 8
1
<?php
2
namespace DominionEnterprises\Filter;
3
4
/**
5
 * A collection of filters for filtering strings into \DateTimeZone objects.
6
 */
7
class DateTimeZone
8
{
9
    /**
10
     * Filters the given value into a \DateTimeZone object.
11
     *
12
     * @param mixed   $value     The value to be filtered.
13
     * @param boolean $allowNull True to allow nulls through, and false (default) if nulls should not be allowed.
14
     *
15
     * @return \DateTimeZone
16
     *
17
     * @throws \InvalidArgumentException Thrown if $allowNull was not a boolean value.
18
     * @throws \Exception if the value did not pass validation.
19
     */
20
    public static function filter($value, $allowNull = false)
21
    {
22
        if ($allowNull !== false && $allowNull !== true) {
23
            throw new \InvalidArgumentException('$allowNull was not a boolean value');
24
        }
25
26
        if ($value === null && $allowNull) {
27
            return null;
28
        }
29
30
        if ($value instanceof \DateTimeZone) {
31
            return $value;
32
        }
33
34
        if (!is_string($value) || trim($value) == '') {
35
            throw new \Exception('$value not a non-empty string');
36
        }
37
38
        return new \DateTimeZone($value);
39
    }
40
}
41