UrlFilter::filter()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 2
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
ccs 7
cts 7
cp 1
crap 3
1
<?php namespace Arcanedev\Sanitizer\Filters;
2
3
use Arcanedev\Sanitizer\Contracts\Filterable;
4
use Illuminate\Support\Str;
5
6
/**
7
 * Class     UrlFilter
8
 *
9
 * @package  Arcanedev\Sanitizer\Filters
10
 * @author   ARCANEDEV <[email protected]>
11
 */
12
class UrlFilter implements Filterable
13
{
14
    /* ------------------------------------------------------------------------------------------------
15
     |  Main Functions
16
     | ------------------------------------------------------------------------------------------------
17
     */
18
    /**
19
     * Sanitize url of the given string.
20
     *
21
     * @param  mixed  $value
22
     * @param  array  $options
23
     *
24
     * @return string|mixed
25
     */
26 9
    public function filter($value, array $options = [])
27
    {
28 9
        if ( ! is_string($value)) return $value;
29
30 9
        $value = trim($value);
31
32 9
        if ( ! Str::startsWith($value, ['http://', 'https://'])) {
33 9
            $value = "http://$value";
34 3
        }
35
36 9
        return filter_var($value, FILTER_SANITIZE_URL);
37
    }
38
}
39