Url   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 0
dl 0
loc 40
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B filter() 0 21 7
1
<?php
2
/**
3
 * Defines the DominionEnterprises\Filter\Url class.
4
 */
5
6
namespace DominionEnterprises\Filter;
7
8
/**
9
 * A collection of filters for urls.
10
 */
11
final class Url
12
{
13
    /**
14
     * Filter an url
15
     *
16
     * Filters value as URL (according to » http://www.faqs.org/rfcs/rfc2396)
17
     *
18
     * The return value is the url, as expected by the \DominionEnterprises\Filterer class.
19
     * By default, nulls are not allowed.
20
     *
21
     * @param mixed $value The value to filter.
22
     * @param bool $allowNull True to allow nulls through, and false (default) if nulls should not be allowed.
23
     *
24
     * @return string The passed in $value.
25
     *
26
     * @throws \Exception if the value did not pass validation.
27
     * @throws \InvalidArgumentException if one of the parameters was not correctly typed.
28
     */
29
    public static function filter($value, $allowNull = false)
30
    {
31
        if ($allowNull !== false && $allowNull !== true) {
32
            throw new \InvalidArgumentException('$allowNull was not a boolean value');
33
        }
34
35
        if ($allowNull === true && $value === null) {
36
            return null;
37
        }
38
39
        if (!is_string($value)) {
40
            throw new \Exception("Value '" . var_export($value, true) . "' is not a string");
41
        }
42
43
        $filteredUrl = filter_var($value, FILTER_VALIDATE_URL);
44
        if ($filteredUrl === false) {
45
            throw new \Exception("Value '{$value}' is not a valid url");
46
        }
47
48
        return $filteredUrl;
49
    }
50
}
51