UrlValidator   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 34
c 1
b 0
f 0
wmc 5
lcom 1
cbo 2
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A validate() 0 17 4
A client() 0 7 1
1
<?php /** MicroUrlValidator */
2
3
namespace Micro\Validator;
4
5
use Micro\Form\IFormModel;
6
7
/**
8
 * UrlValidator class file.
9
 *
10
 * @author Oleg Lunegov <[email protected]>
11
 * @link https://github.com/linpax/microphp-framework
12
 * @copyright Copyright (c) 2013 Oleg Lunegov
13
 * @license https://github.com/linpax/microphp-framework/blob/master/LICENSE
14
 * @package Micro
15
 * @subpackage Validator
16
 * @version 1.0
17
 * @since 1.0
18
 */
19
class UrlValidator extends BaseValidator
20
{
21
    /**
22
     * @inheritdoc
23
     */
24
    public function validate(IFormModel $model)
25
    {
26
        foreach ($this->elements AS $element) {
27
            if (!$model->checkAttributeExists($element)) {
28
                $this->errors[] = 'Parameter '.$element.' not defined in class '.get_class($model);
29
30
                return false;
31
            }
32
            if (filter_var($model->$element, FILTER_VALIDATE_URL) === false) {
33
                $this->errors[] = 'Parameter '.$element.' is not a valid URL address';
34
35
                return false;
36
            }
37
        }
38
39
        return true;
40
    }
41
42
    /**
43
     * @inheritdoc
44
     */
45
    public function client(IFormModel $model)
46
    {
47
        $javaScript = 'if (/(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/.test(this.value'.
48
            ') != true) { e.preventDefault(); this.focus(); alert(\'Value is not a URL\'); }';
49
50
        return $javaScript;
51
    }
52
}
53