Completed
Push — master ( 8ace16...5d30f6 )
by Alexey
03:23
created

AbstractValidation::getErrorMessage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 2
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
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 = '{name} is not valid';
19
20 90
    public function __construct($options = null)
21
    {
22 90
        $this->options = $options;
23 90
    }
24
25 2
    public function setErrorMessage($message){
26 2
        $this->errorMessage = $message;
27 2
    }
28
29 49
    public function error()
30
    {
31 49
        return $this->error;
32
    }
33
34 87
    public function validate(ValueObject $data){
35 87
        $this->data = $data;
36 87
        if (! $this->isValid($data) ) {
37 49
            $this->error = $this->getErrorMessage();
38 49
            $this->afterFailedValidation();
39 49
            return false;
40
        }
41 67
        $this->afterSuccessValidation();
42 67
        return true;
43
    }
44 43
    public function getErrorMessage(){
45 43
        return str_replace("{name}", $this->data->name(), $this->errorMessage);
46
    }
47
48
    abstract public function isValid(ValueObject $data);
49
50 67
    protected function afterSuccessValidation(){
51
52 67
    }
53
54 49
    protected function afterFailedValidation(){
55
56 49
    }
57
58 85
    public function group()
59
    {
60 85
        return $this->group;
61
    }
62
}