EmailValidator   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 32
c 0
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 5 1
1
<?php /** MicroEmailValidator */
2
3
namespace Micro\Validator;
4
5
use Micro\Form\IFormModel;
6
7
/**
8
 * EmailValidator 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 EmailValidator 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_EMAIL)) {
33
                $this->errors[] = 'Parameter '.$element.' is not a valid E-mail 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
        return 'if (/^[\w.-]{1,}@[\w.-]{1,}$/.test(this.value) != true) {'.
48
            ' e.preventDefault(); this.focus(); alert(\'Value is not a valid e-mail\'); }';
49
    }
50
}
51