<?php

namespace App\Http\Requests;

use App\Libs\Security\XSSCleanFilter;
use Illuminate\Contracts\Validation\Validator;
use Illuminate\Foundation\Http\FormRequest;
use App\Libs\Support\Sanitizer\Traits\SanitizesInput;
use Illuminate\Validation\ValidationException;

class UserCompany extends FormRequest
{
    use SanitizesInput;
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize(): bool
    {
        return $this->user() !== null;
    }

    public function validationData(): array
    {
        return [
            'id'            => $this->input('id'),
            'companyName'   => $this->input('companyName'),
        ];
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules():array
    {
        return [
            'id'            => 'required|numeric',
            'companyName'   => 'required|string|max:150',
        ];
    }

    public function filters() {
        return [
            'id'            => "trim|strip_tags|xss_clean",
            'companyName'   => "trim|strip_tags|xss_clean",
        ];
    }

    public function all($keys = null)
    {
        $data = parent::all();
        if ($this->route()->hasParameters()) {
            $parameterName = $this->route()->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);
    }
}
