2022-08-03 21:02:07 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Rules;
|
|
|
|
|
2024-05-19 05:49:42 +00:00
|
|
|
use Closure;
|
2022-08-03 21:02:07 +00:00
|
|
|
use getID3;
|
2024-05-19 05:49:42 +00:00
|
|
|
use Illuminate\Contracts\Validation\ValidationRule;
|
2022-08-03 21:02:07 +00:00
|
|
|
use Illuminate\Support\Arr;
|
|
|
|
use Webmozart\Assert\Assert;
|
|
|
|
|
2024-05-19 05:49:42 +00:00
|
|
|
class SupportedAudioFile implements ValidationRule
|
2022-08-03 21:02:07 +00:00
|
|
|
{
|
|
|
|
private const SUPPORTED_FORMATS = ['mp3', 'aac', 'ogg', 'flac', 'wav'];
|
|
|
|
|
2024-05-19 05:49:42 +00:00
|
|
|
public function validate(string $attribute, mixed $value, Closure $fail): void
|
2022-08-03 21:02:07 +00:00
|
|
|
{
|
2024-05-19 05:49:42 +00:00
|
|
|
$passes = attempt(static function () use ($value) {
|
2022-08-03 21:02:07 +00:00
|
|
|
Assert::oneOf(
|
|
|
|
Arr::get((new getID3())->analyze($value->getRealPath()), 'fileformat'),
|
|
|
|
self::SUPPORTED_FORMATS
|
|
|
|
);
|
|
|
|
|
|
|
|
return true;
|
2022-08-08 16:00:59 +00:00
|
|
|
}, false) ?? false;
|
2022-08-03 21:02:07 +00:00
|
|
|
|
2024-05-19 05:49:42 +00:00
|
|
|
if (!$passes) {
|
|
|
|
$fail('Unsupported audio file');
|
|
|
|
}
|
2022-08-03 21:02:07 +00:00
|
|
|
}
|
|
|
|
}
|