| 1 |
<?php |
| 2 |
|
| 3 |
namespace App\Billingo\Validation; |
| 4 |
|
| 5 |
use App\Billingo\Contracts\ValidableInterface; |
| 6 |
use App\Billingo\Enums\BillingoErrorEnum; |
| 7 |
use App\Billingo\Error\BillingoError; |
| 8 |
use App\Billingo\Exceptions\EmptyObjectException; |
| 9 |
use App\Billingo\Exceptions\ValidationException; |
| 10 |
use InvalidArgumentException; |
| 11 |
use Valitron\Validator as Valitron; |
| 12 |
|
| 13 |
abstract class Validator implements ValidableInterface |
| 14 |
{ |
| 15 |
protected readonly array $rules; |
| 16 |
protected ?BillingoError $errors = null; |
| 17 |
|
| 18 |
public function __construct() |
| 19 |
{ |
| 20 |
$this->rules = $this->rules(); |
| 21 |
} |
| 22 |
|
| 23 |
abstract protected function rules(): array; |
| 24 |
|
| 25 |
/** |
| 26 |
* @throws EmptyObjectException |
| 27 |
* @throws ValidationException |
| 28 |
*/ |
| 29 |
public function validate(array $data): array |
| 30 |
{ |
| 31 |
$relevantData = $this->filterInvalidItems($data, $this->rules); |
| 32 |
|
| 33 |
if (empty($relevantData)) { |
| 34 |
|
| 35 |
throw new EmptyObjectException(); |
| 36 |
} |
| 37 |
|
| 38 |
$validator = $this->createValidator($relevantData, $this->rules); |
| 39 |
|
| 40 |
if ($validator->validate()) { |
| 41 |
|
| 42 |
return $relevantData; |
| 43 |
} else { |
| 44 |
|
| 45 |
throw new ValidationException($validator->errors()); |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
public function validateProperty(string $key, mixed $value): bool |
| 50 |
{ |
| 51 |
if (!isset($this->rules[$key])) { |
| 52 |
|
| 53 |
return false; |
| 54 |
} |
| 55 |
|
| 56 |
$validator = $this->createValidator( |
| 57 |
[$key => $value], |
| 58 |
[$key => $this->rules[$key]]); |
| 59 |
|
| 60 |
return $validator->validate(); |
| 61 |
} |
| 62 |
|
| 63 |
public function createValidator(array $relevantData, array $rules): Valitron |
| 64 |
{ |
| 65 |
$validator = new Valitron($relevantData); |
| 66 |
|
| 67 |
foreach ($rules as $field => $fieldRules) { |
| 68 |
foreach ($fieldRules as $rule) { |
| 69 |
try { |
| 70 |
if (is_array($rule)) { |
| 71 |
$validator->rule($rule[0], $field, $rule[1]); |
| 72 |
} else { |
| 73 |
$validator->rule($rule, $field); |
| 74 |
} |
| 75 |
} catch (InvalidArgumentException $e) { |
| 76 |
|
| 77 |
//TODO: log? |
| 78 |
|
| 79 |
continue; |
| 80 |
} |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
return $validator; |
| 85 |
} |
| 86 |
|
| 87 |
public function filterInvalidItems(array $data, array $rules): array |
| 88 |
{ |
| 89 |
$relevantData = array_intersect_key($data, $rules); |
| 90 |
|
| 91 |
return array_filter($relevantData, function ($value, $key) use ($rules) { |
| 92 |
|
| 93 |
return !is_null($value) || in_array('nullable', $rules[$key]); |
| 94 |
}, ARRAY_FILTER_USE_BOTH); |
| 95 |
|
| 96 |
} |
| 97 |
|
| 98 |
public function getRules(): array |
| 99 |
{ |
| 100 |
return $this->rules; |
| 101 |
} |
| 102 |
|
| 103 |
public function getErrors(): ?BillingoError |
| 104 |
{ |
| 105 |
return $this->errors; |
| 106 |
} |
| 107 |
|
| 108 |
public function setErrors(BillingoErrorEnum $type, array $errors = null): void |
| 109 |
{ |
| 110 |
if (is_null($this->getErrors())) { |
| 111 |
$this->errors = new BillingoError($type, $errors); |
| 112 |
|
| 113 |
return; |
| 114 |
} |
| 115 |
|
| 116 |
$this->errors->setType($type); |
| 117 |
$this->errors->setValues($errors); |
| 118 |
} |
| 119 |
} |
| 120 |
|