ldc-legaulois/app/Http/Requests/OpBrCharcut/ParticipateRequest.php
2022-03-10 13:31:10 +00:00

80 lines
2.7 KiB
PHP

<?php
namespace App\Http\Requests\OpBrCharcut;
use App\Models\OpBrCharcut\Code;
use App\Models\OpBrCharcut\CodeEan;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Str;
class ParticipateRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return boolean
*/
public function authorize()
{
if (is_string($this->code_ean)) {
$this->merge([
'code_ean' => \in_array(Str::length($this->code_ean), [13, 8, 7]) ?
CodeEan::where('code', $this->code_ean)
->orWhere('code', 'LIKE', Str::limit($this->code_ean, 13, '') . '%')
->orWhere('code', 'LIKE', Str::limit($this->code_ean, 8, '') . '%')
->orWhere('code', 'LIKE', Str::limit($this->code_ean, 7, '') . '%')
->first() : null
]);
}
if (is_string($this->code)) {
$this->merge([
'serial' => $this->code,
'code' => Code::whereId(Code::serialToInt(Str::upper($this->code)))->first() ?? false,
]);
}
return auth()->guard('customer')->check();
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
$rules = [
'code' => [
'required',
// phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClassBeforeLastUsed
function ($attribute, $value, $fail) {
if (
$value === false or
!\is_object($value) or
\get_class($value) !== Code::class
) {
$fail('Code incorrect, veuillez réessayer.');
return false;
} elseif ($value->code_used === 1) {
$fail('Ce code n\'est plus utilisable.');
return false;
}
// $this->code->code_used = 1;
// $this->code->save();
}
],
'code_ean' => [
'required',
// phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClassBeforeLastUsed
function ($attribute, $value, $fail) {
if (!\is_object($value) or \get_class($value) !== CodeEan::class) {
$fail('Le code barre entré est incorrect, veuillez réessayer.');
}
}
]
];
return $rules;
}
}