<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class PostRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        switch ($this->method()) {
          case 'POST':
            return $this->user()->hasPermissionTo('pages.store');
          case 'PUT':
            return $this->user()->hasPermissionTo('pages.update');
          case 'DELETE':
            return $this->user()->hasPermissionTo('pages.destroy');
        }
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        switch ($this->method()) {
          case 'POST':
            return [
              'name' => 'required|string',
              'data'=> 'required|json',
              'state'=> 'required',
              'type'=> 'required',
              'author'=> 'required',
            ];
        }
        return [
          //
        ];
    }

    protected function failedAuthorization()
    {
        throw new AuthorizationException(__('errors.action_unauthorized'));
    }
}
