Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
webbingbrasil/exams-module / Http / Requests / CadastroQuestionRequest.php
Size: Mime:
<?php

namespace Modules\Exams\Http\Requests;

use Modules\Core\Http\Requests\Request;

class CadastroQuestionRequest extends Request
{

    private $rules =[
            'title' => 'required|max:100',
            'is_active' => 'boolean',
            'is_correct' => 'required|int'
        ];
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;//policy(CoursesController::class)->create(Auth::user());
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return $this->rules;
    }

    /**
     * Sanitize input before validation.
     *
     * @return array
     */
    public function sanitize()
    {
        $input = array_map('trim', $this->all());

        if(!isset($input['is_active'])) {
            $input['is_active'] = false;
        }
        $isCorrect = [];

        $answers = array_intersect_key($input, array_flip(preg_grep('/^answer_\d+$/', array_keys($input), 0)));
        foreach($answers as $field => $v){
            $tmp = explode('_', $field);
            $isCorrect[] = end($tmp);
            $this->rules[$field] = 'required|max:300';
        }
        $this->rules['is_correct'] .= '|in:'.implode(',', $isCorrect);

        $this->replace($input);

        return $this->all();
    }
}