| 1 | <?php |
||
| 2 | |||
| 3 | namespace ByJG\AnyDataset\Core; |
||
| 4 | use Closure; |
||
| 5 | |||
| 6 | class RowValidator |
||
| 7 | { |
||
| 8 | /** |
||
| 9 | * @var array |
||
| 10 | */ |
||
| 11 | protected $fieldValidator = []; |
||
| 12 | |||
| 13 | const REQUIRED="required"; |
||
| 14 | const NUMBER="number"; |
||
| 15 | const REGEX="regex"; |
||
| 16 | const CUSTOM="custom"; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * @param string|array $fieldList |
||
| 20 | * @param string $property |
||
| 21 | * @param mixed $value |
||
| 22 | * @return void |
||
| 23 | */ |
||
| 24 | 4 | protected function setProperty($fieldList, $property, $value) |
|
| 25 | { |
||
| 26 | 4 | foreach ((array)$fieldList as $field) { |
|
| 27 | 4 | if (!isset($this->fieldValidator[$field])) { |
|
| 28 | 4 | $this->fieldValidator[$field] = []; |
|
| 29 | } |
||
| 30 | 4 | $this->fieldValidator[$field] = [ $property => $value ]; |
|
| 31 | } |
||
| 32 | } |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @return RowValidator |
||
| 36 | */ |
||
| 37 | 4 | public static function getInstance() |
|
| 38 | { |
||
| 39 | 4 | return new RowValidator(); |
|
| 40 | } |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Return an empty array if no errors found, otherwise and array with the errors |
||
| 44 | * |
||
| 45 | * @param Row $row |
||
| 46 | * @return array |
||
| 47 | */ |
||
| 48 | 4 | public function validate(Row $row) |
|
| 49 | { |
||
| 50 | 4 | $errors = []; |
|
| 51 | 4 | foreach ($this->fieldValidator as $field => $properties) { |
|
| 52 | 4 | $errors[] = $this->checkRequired($field, $properties, $row->get($field)); |
|
| 53 | 4 | $errors[] = $this->checkNumber($field, $properties, $row->get($field)); |
|
| 54 | 4 | $errors[] = $this->checkRegex($field, $properties, $row->get($field)); |
|
| 55 | 4 | $errors[] = $this->checkCustom($field, $properties, $row->get($field)); |
|
| 56 | } |
||
| 57 | 4 | return array_values(array_filter($errors, function($value) { return !empty($value); })); |
|
| 58 | } |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Return null if the value is not empty, otherwise a string with the error message |
||
| 62 | * |
||
| 63 | * @param string $field |
||
| 64 | * @param array $properties |
||
| 65 | * @param mixed $value |
||
| 66 | * @return string|null |
||
| 67 | */ |
||
| 68 | 4 | protected function checkRequired($field, $properties, $value) |
|
| 69 | { |
||
| 70 | 4 | if (isset($properties[self::REQUIRED]) && $properties[self::REQUIRED] && empty($value)) { |
|
| 71 | 1 | return "$field is required"; |
|
| 72 | } |
||
| 73 | 4 | return null; |
|
| 74 | } |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Return null if the value is numeric, otherwise a string with the error message |
||
| 78 | * |
||
| 79 | * @param string $field |
||
| 80 | * @param array $properties |
||
| 81 | * @param mixed $value |
||
| 82 | * @return string|null |
||
| 83 | */ |
||
| 84 | 4 | protected function checkNumber($field, $properties, $value) |
|
| 85 | { |
||
| 86 | 4 | if (isset($properties[self::NUMBER]) && $properties[self::NUMBER] && !is_numeric($value)) { |
|
| 87 | 1 | return "$field needs to be a number"; |
|
| 88 | } |
||
| 89 | 4 | return null; |
|
| 90 | } |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Return null if the value matches with the regular expression, otherwise a string with the error message |
||
| 94 | * |
||
| 95 | * @param string $field |
||
| 96 | * @param array $properties |
||
| 97 | * @param mixed $value |
||
| 98 | * @return string|null |
||
| 99 | */ |
||
| 100 | 4 | protected function checkRegex($field, $properties, $value) |
|
| 101 | { |
||
| 102 | 4 | if (isset($properties[self::REGEX]) && !empty($properties[self::REGEX]) && !preg_match($properties[self::REGEX], $value)) { |
|
| 103 | 1 | return "Regex expression for $field doesn't match"; |
|
| 104 | } |
||
| 105 | 4 | return null; |
|
| 106 | } |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Return null if the closure returns null, otherwise the value returned by the Closure |
||
| 110 | * |
||
| 111 | * @param string $field |
||
| 112 | * @param array $properties |
||
| 113 | * @param mixed $value |
||
| 114 | * @return string|null |
||
| 115 | */ |
||
| 116 | 4 | protected function checkCustom($field, $properties, $value) |
|
|
0 ignored issues
–
show
|
|||
| 117 | { |
||
| 118 | 4 | $result = null; |
|
| 119 | 4 | if (isset($properties[self::CUSTOM]) && $properties[self::CUSTOM] instanceof Closure) { |
|
| 120 | 1 | $result = $properties[self::CUSTOM]($value); |
|
| 121 | } |
||
| 122 | 4 | return empty($result) ? null : $result; |
|
| 123 | } |
||
| 124 | |||
| 125 | /** |
||
| 126 | * @param string $field |
||
| 127 | * @return RowValidator |
||
| 128 | */ |
||
| 129 | public function requiredField($field) |
||
| 130 | { |
||
| 131 | return $this->requiredFields([$field]); |
||
| 132 | } |
||
| 133 | |||
| 134 | /** |
||
| 135 | * @param array $fieldList |
||
| 136 | * @return RowValidator |
||
| 137 | */ |
||
| 138 | 1 | public function requiredFields($fieldList) |
|
| 139 | { |
||
| 140 | 1 | $this->setProperty($fieldList, self::REQUIRED, true); |
|
| 141 | 1 | return $this; |
|
| 142 | } |
||
| 143 | |||
| 144 | /** |
||
| 145 | * @param array $fieldList |
||
| 146 | * @return RowValidator |
||
| 147 | */ |
||
| 148 | 1 | public function numericFields($fieldList) |
|
| 149 | { |
||
| 150 | 1 | $this->setProperty($fieldList, self::NUMBER, true); |
|
| 151 | 1 | return $this; |
|
| 152 | } |
||
| 153 | |||
| 154 | /** |
||
| 155 | * @param array|string $field |
||
| 156 | * @param string $regEx |
||
| 157 | * @return RowValidator |
||
| 158 | */ |
||
| 159 | 1 | public function regexValidation($field, $regEx) |
|
| 160 | { |
||
| 161 | 1 | $this->setProperty($field, self::REGEX, $regEx); |
|
| 162 | 1 | return $this; |
|
| 163 | } |
||
| 164 | |||
| 165 | /** |
||
| 166 | * @param array|string $field |
||
| 167 | * @param Closure $closure |
||
| 168 | * @return RowValidator |
||
| 169 | */ |
||
| 170 | 1 | public function customValidation($field, $closure) |
|
| 171 | { |
||
| 172 | 1 | $this->setProperty($field, self::CUSTOM, $closure); |
|
| 173 | 1 | return $this; |
|
| 174 | } |
||
| 175 | |||
| 176 | } |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.