mirror of
https://gitlab.com/CIEFWorldwideSdnBhd/shipping-portal.git
synced 2026-08-19 04:24:12 +00:00
49 lines
1.1 KiB
PHP
49 lines
1.1 KiB
PHP
<?php
|
|
|
|
namespace App\Classes\General\Abstracts;
|
|
|
|
|
|
use App\Classes\Exceptions\RequestValidationException;
|
|
use App\Classes\General\Interfaces\DataTransferObject;
|
|
use Illuminate\Support\Facades\Validator;
|
|
|
|
abstract class AbstractValidation
|
|
{
|
|
|
|
/** @var Validator */
|
|
private $validator;
|
|
|
|
/**
|
|
* AbstractValidation constructor.
|
|
* @param Validator $validator
|
|
*/
|
|
public function __construct(Validator $validator)
|
|
{
|
|
$this->validator = $validator;
|
|
}
|
|
|
|
|
|
abstract protected function data($object): array;
|
|
|
|
abstract protected function rules(): array;
|
|
|
|
abstract protected function messages(): array;
|
|
|
|
/**
|
|
* @param DataTransferObject $object
|
|
* @param null|string $type
|
|
* @return bool
|
|
* @throws RequestValidationException
|
|
*/
|
|
public function validate(DataTransferObject $object, ?string $type = 'POST')
|
|
{
|
|
$validator = $this->validator::make($this->data($object), $this->rules($type), $this->messages());
|
|
|
|
if($validator->fails()) {
|
|
throw new RequestValidationException($validator->messages()->first());
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
} |