<?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;
    }

}