UrlValidator   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 23
c 1
b 0
f 0
dl 0
loc 41
rs 10
ccs 13
cts 13
cp 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
A validateValue() 0 14 4
A init() 0 6 2
1
<?php
2
namespace App\Validator;
3
4
use Yii;
5
use yii\validators\Validator;
6
7
class UrlValidator extends Validator
8
{
9
    public const COMPONENT_SCHEME = 'scheme';
10
    public const COMPONENT_HOST = 'host';
11
    public const COMPONENT_PORT = 'port';
12
    public const COMPONENT_USER = 'user';
13
    public const COMPONENT_PASS = 'pass';
14
    public const COMPONENT_PATH = 'path';
15
    public const COMPONENT_QUERY = 'query';
16
    public const COMPONENT_FRAGMENT = 'fragment';
17
18
    public $schemes = ['http', 'https'];
19
20
    public $components = [
21
        self::COMPONENT_SCHEME,
22
        self::COMPONENT_HOST,
23
    ];
24
25 4
    public function init()
26
    {
27 4
        parent::init();
28
29 4
        if ($this->message === null) {
30 4
            $this->message = Yii::t('yii', '{attribute} is not a valid URL.');
31
        }
32 4
    }
33
    
34 4
    protected function validateValue($value)
35
    {
36 4
        $components = parse_url($value);
37 4
        foreach ($this->components as $name) {
38 4
            if (!isset($components[$name])) {
39 1
                return [$this->message, []];
40
            }
41
        }
42
43 3
        if (!in_array($components[self::COMPONENT_SCHEME], $this->schemes)) {
44 1
            return [$this->message, []];
45
        }
46
47 2
        return null;
48
    }
49
}
50