ldc-legaulois/app/Http/Requests/StoreOcbBreeder.php

135 lines
5.2 KiB
PHP

<?php
namespace App\Http\Requests;
use App\Enums\EleveurOcbStatus;
use App\Enums\GeoType;
use App\Enums\OCBType;
use App\Enums\PresentationType;
use App\Models\OCBBreeder;
use Illuminate\Foundation\Http\FormRequest;
class StoreOcbBreeder extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
if (!$this->inuavs) {
$this->request->add(['inuavs' => []]);
}
if (!$this->status) {
$this->request->add(['status' => false]);
}
if (!$this->audit) {
$this->request->add(['audit' => false]);
}
if (!$this->display_random) {
$this->request->add(['display_random' => false]);
}
if (!strlen($this->valid_marketing_date)) {
$this->request->add(['valid_marketing_date' => null]);
}
return auth()->check();
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'breeder_code' => 'required|string|min:2|max:50,|unique:ocb_breeders,breeder_code',
'first_name' => 'required|string|min:2|max:150',
'last_name' => 'required|string|min:2|max:150',
'picture' => 'required|image|mimes:jpeg,png,jpg|max:2048',
'postal_code' => 'required|string|min:5|max:20',
'place' => 'required|string|min:2|max:255',
'testimony' => 'required|string',
'latitude' => 'required|numeric|between:-99.99,99.99',
'longitude' => 'required|numeric|between:-99.99,99.99',
'geo_type' => [
'required',
// @phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClassBeforeLastUsed
function ($attribute, $value, $fail) {
if (!in_array($value, GeoType::toValues())) {
$fail(__('Veuillez choisir entre :values', [
'value' => implode(',', GeoType::toLabels())
]));
}
}
],
'ocbStatus' => [
'required',
// @phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClassBeforeLastUsed
function ($attribute, $value, $fail) {
if (!in_array($value, EleveurOcbStatus::toValues())) {
$fail(__('Veuillez choisir entre :values', [
'value' => implode(',', EleveurOcbStatus::toLabels())
]));
}
}
],
'ocbType' => [
'required',
// @phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClassBeforeLastUsed
function ($attribute, $value, $fail) {
if (!in_array($value, OCBType::toValues())) {
$fail(__('Veuillez choisir entre :values', [
'value' => implode(',', OCBType::toLabels())
]));
}
}
],
'presentation_type' => [
'required',
// @phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClassBeforeLastUsed
function ($attribute, $value, $fail) {
if (!in_array($value, PresentationType::toValues())) {
$fail(__('Veuillez choisir entre :values', [
'value' => implode(',', PresentationType::toLabels())
]));
}
}
],
'audit' => 'boolean',
'valid_marketing_date' => 'nullable|date',
'video_link' => 'string|max:255',
'picture1' => 'image|mimes:jpeg,png,jpg|max:2048',
'picture2' => 'image|mimes:jpeg,png,jpg|max:2048',
'picture3' => 'image|mimes:jpeg,png,jpg|max:2048',
'status' => 'boolean',
'display_random' => 'boolean',
'inuavs' => 'array',
'inuavs.*' => [
// @phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClassBeforeLastUsed
function ($attribute, $value, $fail) {
if (strpos(trim($value), ' ') !== false) {
$fail(trans('Les espaces ne sont pas autorisés dans les codes INUAV'));
}
if (count($this->inuavs) > count(array_unique($this->inuavs))) {
$fail(trans('Les codes INUAVS doinvent être uniques'));
}
$q = OCBBreeder::query();
foreach ($this->inuavs as $inuav) {
$q = $q->where('inuavs', 'LIKE', "%$inuav%");
}
// update condition
if ($this->breeder) {
$q->where('id', '!=', $this->breeder->id);
}
if ($q->count()) {
$fail(trans('Les codes INUAVS doinvent être uniques'));
}
}
],
];
}
}