34 lines
1.1 KiB
PHP
34 lines
1.1 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace App\Rules;
|
||
|
|
|
||
|
|
use Illuminate\Contracts\Validation\ValidationRule;
|
||
|
|
use Illuminate\Support\Str;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Validate a youtube ID regex
|
||
|
|
*
|
||
|
|
* @url https://webapps.stackexchange.com/questions/54443/format-for-id-of-youtube-video
|
||
|
|
* @package App\Rules
|
||
|
|
*/
|
||
|
|
class IsValidYoutubeId implements ValidationRule
|
||
|
|
{
|
||
|
|
/**
|
||
|
|
* Run the validation rule.
|
||
|
|
*
|
||
|
|
* @param string $attribute
|
||
|
|
* @param mixed $value
|
||
|
|
* @param \Closure(string):\Illuminate\Translation\PotentiallyTranslatedString $fail
|
||
|
|
* @return void
|
||
|
|
* @phpcs:disable Generic.CodeAnalysis.UnusedFunctionParameter.FoundInImplementedInterfaceBeforeLastUsed
|
||
|
|
* @phpcs:disable Squiz.Commenting.FunctionComment.IncorrectTypeHint
|
||
|
|
*/
|
||
|
|
public function validate(string $attribute, mixed $value, \Closure $fail): void
|
||
|
|
{
|
||
|
|
// phpcs:enable
|
||
|
|
if (!Str::of($value)->isMatch('/[0-9A-Za-z_-]{10}[048AEIMQUYcgkosw]/')) {
|
||
|
|
$fail(trans('validation.youtube_id'));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|