Completed
Pull Request — master (#75)
by
unknown
01:26
created

Url::filter()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 17
Code Lines 9

Duplication

Lines 3
Ratio 17.65 %

Importance

Changes 0
Metric Value
dl 3
loc 17
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 9
nc 4
nop 2
1
<?php
2
3
namespace TraderInteractive\Filter;
4
5
/**
6
 * A collection of filters for urls.
7
 */
8
final class Url
9
{
10
    /**
11
     * Filter an url
12
     *
13
     * Filters value as URL (according to » http://www.faqs.org/rfcs/rfc2396)
14
     *
15
     * The return value is the url, as expected by the \TraderInteractive\Filterer class.
16
     * By default, nulls are not allowed.
17
     *
18
     * @param mixed $value The value to filter.
19
     * @param bool $allowNull True to allow nulls through, and false (default) if nulls should not be allowed.
20
     *
21
     * @return string|null The passed in $value.
22
     *
23
     * @throws Exception if the value did not pass validation.
24
     * @throws \InvalidArgumentException if one of the parameters was not correctly typed.
25
     */
26
    public static function filter($value, bool $allowNull = false)
27
    {
28
        if ($allowNull === true && $value === null) {
29
            return null;
30
        }
31
32 View Code Duplication
        if (!is_string($value)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
33
            throw new Exception("Value '" . var_export($value, true) . "' is not a string");
34
        }
35
36
        $filteredUrl = filter_var($value, FILTER_VALIDATE_URL);
37
        if ($filteredUrl === false) {
38
            throw new Exception("Value '{$value}' is not a valid url");
39
        }
40
41
        return $filteredUrl;
42
    }
43
}
44