FormValidator   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
c 1
b 0
f 0
lcom 1
cbo 1
dl 0
loc 47
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getErrorMsg() 0 4 1
A process() 0 11 4
A isValid() 0 4 1
A isLive() 0 4 1
validate() 0 1 ?
1
<?php
2
3
namespace fieldwork\validators;
4
5
use fieldwork\Form;
6
7
abstract class FormValidator
8
{
9
10
    private   $isValid        = true;
11
    protected $inflictsFields = array();
12
    protected $errorMsg       = '';
13
14
    /**
15
     * @param string $errorMsg       Error message to show if this validator returns false
16
     * @param array  $inflictsFields Array of fields to be marked "invalid" upon rendering the form if this validator
17
     *                               evaluates to false
18
     */
19
    public function __construct ($errorMsg, array $inflictsFields = array())
20
    {
21
        $this->errorMsg       = $errorMsg;
22
        $this->inflictsFields = $inflictsFields;
23
    }
24
25
    public function getErrorMsg ()
26
    {
27
        return $this->errorMsg;
28
    }
29
30
    public function process (Form $form)
31
    {
32
        if (($this->isValid = $this->validate($form)))
33
            return true;
34
        else {
35
            foreach ($this->inflictsFields as $if)
36
                if (($field = $form->f($if)))
37
                    $field->forceInvalid();
38
            return false;
39
        }
40
    }
41
42
    public function isValid ()
43
    {
44
        return $this->isValid;
45
    }
46
47
    public function isLive ()
48
    {
49
        return false;
50
    }
51
52
    public abstract function validate (Form $form);
53
}