mirror of
https://github.com/AnimeThemes/animethemes-server.git
synced 2026-10-17 16:20:49 +02:00
* chore: address deprecations to validation rules in Laravel 10 * style: fix StyleCI findings
42 lines
1.0 KiB
PHP
42 lines
1.0 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Rules\Api;
|
|
|
|
use Closure;
|
|
use Illuminate\Contracts\Validation\ValidationRule;
|
|
use Illuminate\Support\Str;
|
|
use Illuminate\Translation\PotentiallyTranslatedString;
|
|
|
|
/**
|
|
* Class DistinctIgnoringDirectionRule.
|
|
*/
|
|
class DistinctIgnoringDirectionRule implements ValidationRule
|
|
{
|
|
/**
|
|
* Run the validation rule.
|
|
*
|
|
* @param string $attribute
|
|
* @param mixed $value
|
|
* @param Closure(string): PotentiallyTranslatedString $fail
|
|
* @return void
|
|
*/
|
|
public function validate(string $attribute, mixed $value, Closure $fail): void
|
|
{
|
|
$values = Str::of($value)->explode(',');
|
|
|
|
$duplicateValues = $values->duplicates(function (mixed $sort) {
|
|
if (is_string($sort) && Str::startsWith($sort, '-')) {
|
|
return Str::replaceFirst('-', '', $sort);
|
|
}
|
|
|
|
return $sort;
|
|
});
|
|
|
|
if ($duplicateValues->isNotEmpty()) {
|
|
$fail(__('validation.distinct'));
|
|
}
|
|
}
|
|
}
|