Total Complexity | 13 |
Total Lines | 66 |
Duplicated Lines | 0 % |
Coverage | 92.86% |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
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() |
|
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) |
|
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() |
|
75 | } |
||
76 | } |