<?php

namespace App\Http\Controllers;

use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Http\Request;

class Controller extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;

    /**
     * Validate the given request with the given rules.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  array  $rules
     * @param  array  $messages
     * @param  array  $customAttributes
     * @return array
     *
     * @throws \Illuminate\Validation\ValidationException
     */
    public function validate(Request $request, array $rules,
                             array $messages = [], array $customAttributes = [])
    {
        $validate = [];
        foreach ($rules as $k => $v){
            $tmp = explode('|', $k);
            $validate[$tmp[0]] = $v;
            if (!empty($tmp[1]) && empty($customAttributes[$tmp[0]])){
                $customAttributes[$tmp[0]] = $tmp[1];
            }
        }

        return $this->getValidationFactory()->make(
            $request->all(), $validate, $messages, $customAttributes
        )->validate();
    }
}
