ldc-dam/app/Rules/EnumIsValidRule.php
Jérémy Munsch 9767e62c72
Update NPM deps and webpack.mix.js
composer require ldc/web-status-checker-laravel remove Old static status checker

Fix ImportMedia

WIP commands OK

many fixes

Fix and try to optimize folder component

Fix front icons + actions based on icons

Favico

front fix alert

Fix Media upload on depot

fix folder component buttons

Full front fix with perf due tu vuejs

Download medias

Navigate within medias superfix

Move media + loader

isLocked boolean and remove media fix

Folder scroll fix

arcadia link color red

Fix panier show external

Tons of fixes back as front

REwrited all
2022-07-22 17:00:48 +02:00

65 lines
1.7 KiB
PHP

<?php
namespace App\Rules;
use Illuminate\Contracts\Validation\Rule;
/**
* Custom Rule to validate an enum
*/
class EnumIsValidRule implements Rule
{
/**
* The enum class
*
* @var string
*/
protected string $enum;
/**
* Create a new rule instance
* which validate the fact that a value is within an Enum
*
* @param string $enumClass
* @return void
* @throws \RuntimeException If not a valid enum.
*/
public function __construct(string $enumClass)
{
if (!enum_exists($enumClass)) {
throw new \RuntimeException("'$enumClass' is not an enum");
}
$this->enumClass = $enumClass;
}
/**
* Determine if the validation rule passes.
*
* @param string $attribute
* @param mixed $value
* @return boolean
* @phpcs:disable Squiz.Commenting.FunctionComment.ScalarTypeHintMissing
* @phpcs:disable Squiz.Commenting.FunctionComment.TypeHintMissing
* @phpcs:disable Generic.CodeAnalysis.UnusedFunctionParameter.FoundInImplementedInterfaceBeforeLastUsed
*/
public function passes($attribute, $value): bool
{
// phpcs:enable
try {
return $this->enumClass::tryFrom(\strval($value)) !== false;
} catch (\TypeError) {
return $this->enumClass::tryFrom(\intval($value)) !== false;
}
}
/**
* Get the validation error message.
*
* @return string
*/
public function message(): string
{
return trans('Le champ :attribute doit être une des valeurs suivantes: :values', [
'values' => collect($this->enumClass::cases())->implode(', ')
]);
}
}