Fixes from code analyzer
This commit is contained in:
parent
402609ad07
commit
d8402c3397
4 changed files with 27 additions and 7 deletions
|
|
@ -39,7 +39,7 @@ abstract class BaseEnumRoutable extends BaseEnum implements UrlRoutable
|
|||
*
|
||||
* @param mixed $value
|
||||
* @param string|null $field
|
||||
* @return \Illuminate\Database\Eloquent\Model|null
|
||||
* @return \Kwaadpepper\Enum\BaseEnum|null
|
||||
*/
|
||||
// phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClassAfterLastUsed
|
||||
public function resolveRouteBinding($value, $field = null)
|
||||
|
|
|
|||
|
|
@ -13,7 +13,7 @@ class EnumServiceProvider extends ServiceProvider
|
|||
*/
|
||||
public function boot()
|
||||
{
|
||||
$this->loadJsonTranslationsFrom(__DIR__ . '/../resources/lang', 'enum');
|
||||
$this->loadJsonTranslationsFrom(__DIR__ . '/../resources/lang');
|
||||
|
||||
$this->publishes([
|
||||
__DIR__ . '/../resources/lang' => resource_path('lang/vendor/enum'),
|
||||
|
|
|
|||
|
|
@ -40,13 +40,16 @@ class EnumIsValidRule extends BaseEnumRule
|
|||
*/
|
||||
public function message(): string
|
||||
{
|
||||
$message = null;
|
||||
switch ($this->messageType) {
|
||||
case 1:
|
||||
return trans('enum::enum.isNotValid');
|
||||
$message = trans('enum::enum.isNotValid');
|
||||
break;
|
||||
case 2:
|
||||
return trans('enum::enum.notInList', [
|
||||
$message = trans('enum::enum.notInList', [
|
||||
'values' => implode(',', forward_static_call([$this->enumClass, 'toValues']))
|
||||
]);
|
||||
}
|
||||
return \is_string($message) ? $message : 'Invalid Enum';
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,9 +6,26 @@ use Kwaadpepper\Enum\BaseEnum;
|
|||
|
||||
/**
|
||||
* @property array $enumCasts Map attribute names to enum classes.
|
||||
* @method bool hasCast(string $key, array|string|null $types = null)
|
||||
* @method mixed castAttribute(string $key, mixed $value)
|
||||
*/
|
||||
trait CastsEnums
|
||||
{
|
||||
|
||||
/**
|
||||
* The model's attributes.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $attributes = [];
|
||||
|
||||
/**
|
||||
* Propertis that should be casted to enum
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $enumCasts = [];
|
||||
|
||||
/**
|
||||
* Get a plain attribute (not a relationship).
|
||||
*
|
||||
|
|
@ -36,7 +53,7 @@ trait CastsEnums
|
|||
public function setAttribute($key, $value)
|
||||
{
|
||||
if ($value !== null && $this->hasEnumCast($key)) {
|
||||
/** @var \App\Enums\BaseEnum $enum */
|
||||
/** @var \Kwaadpepper\Enum\BaseEnum $enum */
|
||||
$enum = $this->enumCasts[$key];
|
||||
|
||||
if ($value instanceof $enum) {
|
||||
|
|
@ -80,11 +97,11 @@ trait CastsEnums
|
|||
*
|
||||
* @param string $key
|
||||
* @param mixed $value
|
||||
* @return \App\Enums\BaseEnum|null
|
||||
* @return \Kwaadpepper\Enums\BaseEnum|null
|
||||
*/
|
||||
protected function castToEnum($key, $value): ?BaseEnum
|
||||
{
|
||||
/** @var \App\Enums\BaseEnum $enum */
|
||||
/** @var \Kwaadpepper\Enum\BaseEnum $enum */
|
||||
$enum = $this->enumCasts[$key];
|
||||
|
||||
if ($value === null || $value instanceof BaseEnum) {
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue