UrlValidator::client()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 1
eloc 4
nc 1
nop 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