<?php

namespace App\Http\Requests;


use App\Traits\ApiResponseTrait;
use Illuminate\Contracts\Validation\Validator;
use Illuminate\Http\Exceptions\HttpResponseException;
use Illuminate\Validation\ValidationException;

class ApiBaseRequest extends BaseRequest
{
    use ApiResponseTrait;

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            //
        ];
    }

    protected function passedValidation()
    {
        $request = $this->all();
        $rules = array_keys($this->rules());
        $final_data = [];
        if (!empty($rules)) {
            foreach ($rules as $rule) {
                if (isset($request[$rule])) {
                    $final_data[$rule] = $request[$rule];
                }
            }
        }

        $this->replace($final_data);
    }

    protected function failedValidation(Validator $validator)
    {
        $errors = (new ValidationException($validator))->errors();
        throw new HttpResponseException($this->sendApiResponse($errors,"validation error",config('constant.API_VALIDATION_FAILED')));
    }
}
