ldc-op-insta/app/Http/Requests/Bo/Ajax/ExcludeTopMediaRequest.php
2023-04-21 14:49:53 +00:00

60 lines
2.1 KiB
PHP

<?php
namespace App\Http\Requests\Bo\Ajax;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Facades\Cache;
class ExcludeTopMediaRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return boolean
*/
public function authorize(): bool
{
return auth('backend')->check() and $this->ajax();
}
/**
* Get the validation rules that apply to the request.
*
* @return array<string, \Illuminate\Contracts\Validation\Rule|array|string>
*/
public function rules(): array
{
return [
'insta_id' => [
'required_without:username', 'digits_between:17,18',
function ($attribute, $value, $fail) {
/** @var \Illuminate\Support\Collection<\App\Lib\Instagram\Objects\TopMedia> */
$igMediaList = Cache::get('top_media_ajax_fetch');
if (
!$igMediaList->contains(function ($lValue) use ($value) {
/** @var \App\Lib\Instagram\Objects\TopMedia $lValue */
return $lValue->insta_id === \intval($value);
})
) {
$fail("$attribute Le media n'existe pas dans la liste en cache");
}
}
],
'username' => [
'required_without:insta_id', 'string', 'min:1',
function ($attribute, $value, $fail) {
/** @var \Illuminate\Support\Collection<\App\Lib\Instagram\Objects\TopMedia> */
$igMediaList = Cache::get('top_media_ajax_fetch');
if (
!$igMediaList->contains(function ($lValue) use ($value) {
/** @var \App\Lib\Instagram\Objects\TopMedia $lValue */
return $lValue->username === $value;
})
) {
$fail("$attribute Le media n'existe pas dans la liste en cache");
}
}
],
];
}
}