<?php

namespace App\Http\Requests\Api;

use Illuminate\Foundation\Http\FormRequest;
use App\Libs\Security\XSSCleanFilter;
use Illuminate\Contracts\Validation\Validator;
use Illuminate\Validation\ValidationException;

class ClauseShow extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'id'         => "required|integer|min:1",
        ];
    }

    public function all($keys = null)
    {
        $data = parent::all();

        if ($this->route()->hasParameters()) {
            $parameterNames = $this->route()->parameterNames();

            if (!empty($parameterNames)) {
                $parameterName = $parameterNames[0];
                $data['id'] = $this->route()->parameter($parameterName);
            }
        }

        return $data;
    }

    public function customFilters()
    {
        return ['xss_clean' => XSSCleanFilter::class];
    }

    protected function failedValidation(Validator $validator)
    {
        throw (new ValidationException($validator))
            ->errorBag($this->errorBag);
    }
}
