AbstractValidation   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Test Coverage

Coverage 92.86%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 13
eloc 24
c 1
b 0
f 0
dl 0
loc 66
ccs 26
cts 28
cp 0.9286
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A error() 0 3 1
A setErrorMessage() 0 3 1
A getErrorMessage() 0 5 2
A group() 0 3 1
A __construct() 0 4 3
A errorMessage() 0 3 1
A afterSuccessValidation() 0 2 1
A afterFailedValidation() 0 2 1
A validate() 0 10 2
1
<?php
2
3
4
namespace Lexuss1979\Validol\Validations;
5
6
7
use Lexuss1979\Validol\ValueObject;
8
9
abstract class AbstractValidation
10
{
11
    const REQUIREMENTS_GROUP = 'requirements';
12
    const TYPE_GROUP = 'type';
13
    const COMMON_GROUP = 'common';
14
    protected $error;
15
    protected $data;
16
    protected $options;
17
    protected $group = self::COMMON_GROUP;
18
    protected $errorMessage = null;
19
20 165
    public function __construct($options = null)
21
    {
22 165
        if(!is_null($options) && !is_array($options) ) $options = [$options];
23 165
        $this->options = $options;
24 165
    }
25
26 96
    public function error()
27
    {
28 96
        return $this->error;
29
    }
30
31 162
    public function validate(ValueObject $data)
32
    {
33 162
        $this->data = $data;
34 162
        if (!$this->isValid($data)) {
35 96
            $this->error = $this->getErrorMessage();
36 96
            $this->afterFailedValidation();
37 96
            return false;
38
        }
39 142
        $this->afterSuccessValidation();
40 142
        return true;
41
    }
42
43
    abstract public function isValid(ValueObject $data);
44
45 96
    public function getErrorMessage()
46
    {
47 96
        if (!is_null($this->errorMessage)) return str_replace("{name}", $this->data->name(), $this->errorMessage);
48
49 53
        return $this->errorMessage();
50
    }
51
52 2
    public function setErrorMessage($message)
53
    {
54 2
        $this->errorMessage = $message;
55 2
    }
56
57
    protected function errorMessage()
58
    {
59
        return "{$this->data->name()} is not valid";
60
    }
61
62 96
    protected function afterFailedValidation()
63
    {
64
65 96
    }
66
67 142
    protected function afterSuccessValidation()
68
    {
69
70 142
    }
71
72 159
    public function group()
73
    {
74 159
        return $this->group;
75
    }
76
}