Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
| Total | |
100.00% |
1 / 1 |
|
100.00% |
5 / 5 |
CRAP | |
100.00% |
24 / 24 |
| RequisitesValidator | |
100.00% |
1 / 1 |
|
100.00% |
5 / 5 |
13 | |
100.00% |
24 / 24 |
| init | |
100.00% |
1 / 1 |
4 | |
100.00% |
9 / 9 |
|||
| getDefaultErrorMessages | n/a |
0 / 0 |
0 | n/a |
0 / 0 |
|||||
| checkValueLength | n/a |
0 / 0 |
0 | n/a |
0 / 0 |
|||||
| checkValueInstanceOf | |
100.00% |
1 / 1 |
2 | |
100.00% |
1 / 1 |
|||
| checkIsValuesCharsValid | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
| prepareData | |
100.00% |
1 / 1 |
1 | |
100.00% |
3 / 3 |
|||
| preValidate | |
100.00% |
1 / 1 |
5 | |
100.00% |
10 / 10 |
|||
| 1 | <?php |
| 2 | |
| 3 | namespace shumorkiniv\validators; |
| 4 | |
| 5 | use yii\validators\Validator; |
| 6 | |
| 7 | /** |
| 8 | * Class RequisitesValidator |
| 9 | * |
| 10 | * The basic class for requisites validators. |
| 11 | * |
| 12 | * @author Shumorkin Ilya <shumorkinilya@mail.ru> |
| 13 | */ |
| 14 | abstract class RequisitesValidator extends Validator |
| 15 | { |
| 16 | /** @var ?string User wrong count message */ |
| 17 | public ?string $wrongLengthMessage = null; |
| 18 | /** @var ?string User wrong character message */ |
| 19 | public ?string $wrongCharMessage = null; |
| 20 | |
| 21 | /** @var array */ |
| 22 | protected array $numbers; |
| 23 | /** @var int */ |
| 24 | protected int $length; |
| 25 | |
| 26 | /** |
| 27 | * @inheritdoc |
| 28 | */ |
| 29 | public function init() |
| 30 | { |
| 31 | parent::init(); |
| 32 | |
| 33 | $defaultErrorMessages = $this->getDefaultErrorMessages(); |
| 34 | |
| 35 | if ($this->wrongLengthMessage === null) { |
| 36 | $this->wrongLengthMessage = $defaultErrorMessages['wrongLength']; |
| 37 | } |
| 38 | |
| 39 | if ($this->wrongCharMessage === null) { |
| 40 | $this->wrongCharMessage = $defaultErrorMessages['wrongChar']; |
| 41 | } |
| 42 | |
| 43 | if ($this->message === null) { |
| 44 | $this->message = $defaultErrorMessages['invalidValue']; |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | |
| 49 | /** |
| 50 | * Returns array of default error messages |
| 51 | * |
| 52 | * The returns array the array is as follows: |
| 53 | * |
| 54 | * ['wrongLength' => 'wrongLength text', 'wrongChar' => 'wrongChar text', 'invalidValue' => 'invalidValue text'] |
| 55 | * |
| 56 | * @return string[] |
| 57 | */ |
| 58 | protected abstract function getDefaultErrorMessages(): array; |
| 59 | |
| 60 | /** |
| 61 | * Returns true if value is in valid length range |
| 62 | * |
| 63 | * @param string $value |
| 64 | * @return bool |
| 65 | */ |
| 66 | protected abstract function checkValueLength(string $value): bool; |
| 67 | |
| 68 | /** |
| 69 | * Returns true if value instance is string or int |
| 70 | * |
| 71 | * @param mixed $value |
| 72 | * @return bool |
| 73 | */ |
| 74 | protected function checkValueInstanceOf($value): bool |
| 75 | { |
| 76 | return is_int($value) || is_string($value); |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Returns true if value is numeric |
| 81 | * |
| 82 | * @param string $value |
| 83 | * @return bool |
| 84 | */ |
| 85 | protected function checkIsValuesCharsValid(string $value): bool |
| 86 | { |
| 87 | return is_numeric($value); |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Set properties numbers and length for next calculations |
| 92 | * |
| 93 | * @param string $value |
| 94 | */ |
| 95 | protected function prepareData(string $value) { |
| 96 | $this->numbers = array_map('intval', str_split($value)); |
| 97 | $this->length = count($this->numbers); |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Make preparatory actions before validation. |
| 102 | * |
| 103 | * Returns empty array if no errors. |
| 104 | * |
| 105 | * @param mixed $value |
| 106 | * @return array |
| 107 | */ |
| 108 | protected function preValidate($value): array |
| 109 | { |
| 110 | if (!$this->checkValueInstanceOf($value)) { |
| 111 | return [$this->message, []]; |
| 112 | } |
| 113 | |
| 114 | if (is_int($value)) { |
| 115 | $value = (string)$value; |
| 116 | } |
| 117 | |
| 118 | if (!$this->checkValueLength($value)) { |
| 119 | return [$this->wrongLengthMessage, []]; |
| 120 | } |
| 121 | |
| 122 | if (!$this->checkIsValuesCharsValid($value)) { |
| 123 | return [$this->wrongCharMessage, []]; |
| 124 | } |
| 125 | |
| 126 | $this->prepareData($value); |
| 127 | |
| 128 | return []; |
| 129 | } |
| 130 | } |