ldc-legaulois/app/Http/Requests/StoreArticle.php
2022-02-10 16:14:58 +01:00

71 lines
2.3 KiB
PHP

<?php
namespace App\Http\Requests;
use App\Enums\ArticleCategory;
use Illuminate\Support\Str;
use Illuminate\Foundation\Http\FormRequest;
class StoreArticle extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return boolean
*/
public function authorize()
{
if (auth()->check()) {
$this->merge([
'slug' => Str::slug($this->title),
'status' => $this->status === null ? 0 : 1,
'notification' => $this->notification === null ? 0 : 1,
]);
if ($this->type == ArticleCategory::sondage()) {
$this->merge([
'multiple_response' => $this->multiple_response === null ? 0 : 1,
'stop_poll' => $this->stop_poll === null ? 0 : 1,
]);
}
return true;
}
return false;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
$moreRules = [];
if ($this->type == ArticleCategory::sondage()) {
$moreRules = [
'question' => 'required|min:10,max:255',
'answers' => 'required|array',
'answers.*' => 'required|string|distinct|min:10,max:255',
'multiple_response' => 'required|boolean',
'stop_poll' => 'required|boolean',
];
}
return array_merge([
'title' => 'required|min:10|max:255',
'slug' => 'required|unique:articles,slug|max:255',
'content' => 'required|min: 20',
'status' => 'required|boolean',
'notification' => 'required|boolean',
'type' => [
'required',
// @phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClassBeforeLastUsed
function ($attribute, $value, $fail) {
if (!in_array($value, ArticleCategory::toValues())) {
$fail(__('Veuillez choisir entre :values', [
'value' => implode(',', ArticleCategory::toLabels())
]));
}
}
]
], $moreRules);
}
}