2024-06-04 09:44:33 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Rules;
|
|
|
|
|
|
|
|
use App\Models\User;
|
|
|
|
use App\Repositories\SongRepository;
|
|
|
|
use Closure;
|
|
|
|
use Illuminate\Contracts\Validation\ValidationRule;
|
|
|
|
use Illuminate\Support\Arr;
|
|
|
|
|
|
|
|
class AllPlayablesAreAccessibleBy implements ValidationRule
|
|
|
|
{
|
|
|
|
public function __construct(private readonly User $user)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2024-06-04 13:35:00 +00:00
|
|
|
/** @param array<string> $value */
|
2024-06-04 09:44:33 +00:00
|
|
|
public function validate(string $attribute, mixed $value, Closure $fail): void
|
|
|
|
{
|
|
|
|
$ids = array_unique(Arr::wrap($value));
|
|
|
|
|
|
|
|
if ($ids && app(SongRepository::class)->getMany(ids: $ids, scopedUser: $this->user)->count() !== count($ids)) {
|
|
|
|
$fail('Not all songs or episodes exist in the database or are accessible by the user.');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|