| 1 | <?php |
||
| 15 | abstract class AbstractValidator |
||
| 16 | { |
||
| 17 | |||
| 18 | /** @var AbstractType */ |
||
| 19 | protected $field; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * @var string |
||
| 23 | */ |
||
| 24 | protected $errorMessage = ''; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * AbstractValidator constructor. |
||
| 28 | * @param AbstractType $field |
||
| 29 | */ |
||
| 30 | public function __construct(AbstractType $field) |
||
| 31 | { |
||
| 32 | $this->field = $field; |
||
| 33 | } |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @return boolean |
||
| 37 | * @codeCoverageIgnore |
||
| 38 | */ |
||
| 39 | public function validate() |
||
| 40 | { |
||
| 41 | if (!$this->process($this->field->getValue())) { |
||
| 42 | |||
| 43 | $this->field->addAttribute('class', 'error'); |
||
| 44 | $this->field->setErrorMessages([$this->getMessage()]); |
||
| 45 | return false; |
||
| 46 | |||
| 47 | } |
||
| 48 | |||
| 49 | return true; |
||
| 50 | } |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @return AbstractType |
||
| 54 | * @codeCoverageIgnore |
||
| 55 | */ |
||
| 56 | public function getField() |
||
| 57 | { |
||
| 58 | return $this->field; |
||
| 59 | } |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Return the error message |
||
| 63 | * @return string |
||
| 64 | * @codeCoverageIgnore |
||
| 65 | */ |
||
| 66 | public function getMessage() |
||
| 67 | { |
||
| 68 | $translate = new Translator(); |
||
| 69 | return $translate->translate($this->errorMessage); |
||
| 70 | } |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Init method which must be implemented by every validator |
||
| 74 | * @param mixed $data |
||
| 75 | * @return mixed |
||
| 76 | */ |
||
| 77 | abstract public function process($data); |
||
| 78 | |||
| 79 | } |