diff --git a/composer.json b/composer.json index cafbdf7..dc98125 100644 --- a/composer.json +++ b/composer.json @@ -13,7 +13,8 @@ "typed", "extendible", "castable", - "casts" + "casts", + "php" ], "license": "MIT", "homepage": "https://github.com/sourceboat/laravel-enumeration", @@ -28,13 +29,13 @@ "require": { "php": ">=7.3", "eloquent/enumeration": "^6.0", - "illuminate/console": ">=7.5", - "illuminate/contracts": ">=7.5", - "illuminate/support": ">=7.5" + "illuminate/console": "^8.0", + "illuminate/contracts": "^8.0", + "illuminate/support": "^8.0" }, "require-dev": { "consistence/coding-standard": "3.10.1", - "orchestra/testbench": "^5.0 || ^6.0", + "orchestra/testbench": "^6.0", "phpmd/phpmd": "^2.6", "phpunit/phpunit": "9.*", "slevomat/coding-standard": "6.4.0", @@ -59,7 +60,7 @@ }, "extra": { "branch-alias": { - "dev-master": "1.3.x-dev" + "dev-master": "3.x-dev" }, "laravel": { "providers": [ diff --git a/src/Casts/Enum.php b/src/Casts/Enum.php index bc5d4f8..c39b218 100644 --- a/src/Casts/Enum.php +++ b/src/Casts/Enum.php @@ -19,10 +19,10 @@ public function __construct(string $enumClass, bool $nullable = true) /** * Transform the attribute from the underlying model values. * - * @param \Illuminate\Database\Eloquent\Model $model - * @param string $key - * @param mixed $value - * @param array $attributes + * @param \Illuminate\Database\Eloquent\Model $model + * @param string $key + * @param mixed $value + * @param array $attributes * @return self|null * @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint * @phpcsSuppress SlevomatCodingStandard.TypeHints.ReturnTypeHint.MissingNativeTypeHint @@ -44,18 +44,17 @@ public function get($model, string $key, $value, array $attributes) /** * Transform the attribute to its underlying model values. * - * @param \Illuminate\Database\Eloquent\Model $model - * @param string $key - * @param mixed $value - * @param array $attributes + * @param \Illuminate\Database\Eloquent\Model $model + * @param string $key + * @param mixed $value + * @param array $attributes * @return array * @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function set($model, string $key, $value, array $attributes): array { - if ($this->enumClass::hasValue($value) - || ($this->nullable && is_null($value))) { + if ($this->enumClass::hasValue($value) || ($this->nullable && is_null($value))) { return [ $key => $value, ]; diff --git a/src/Enumeration.php b/src/Enumeration.php index dfaeb33..f222aeb 100644 --- a/src/Enumeration.php +++ b/src/Enumeration.php @@ -24,13 +24,27 @@ abstract class Enumeration extends AbstractEnumeration implements Castable protected static $localizationPath = null; /** - * Get the localization path for this enum. + * Get the default enum member. + * Override for your own value / logic. * - * @return string + * @return static */ - protected static function getLocalizationPath(): string + public static function defaultMember() { - return static::$localizationPath ?? sprintf('enums.%s', static::class); + return collect(static::members()) + ->first(); + } + + /** + * Get a random member of this enum. + * + * @param array|null $blacklist + * @return static + */ + public static function randomMember(?array $blacklist = []) + { + return collect(self::membersByBlacklist($blacklist)) + ->random(); } /** @@ -61,7 +75,10 @@ public function is($value): bool */ public static function values(): array { - return collect(self::members())->map->value()->values()->all(); + return collect(self::members()) + ->map->value() + ->values() + ->all(); } /** @@ -71,7 +88,10 @@ public static function values(): array */ public static function localizedValues(): array { - return collect(self::members())->map->localized()->values()->all(); + return collect(self::members()) + ->map->localized() + ->values() + ->all(); } /** @@ -81,7 +101,10 @@ public static function localizedValues(): array */ public static function keys(): array { - return collect(self::members())->map->key()->values()->all(); + return collect(self::members()) + ->map->key() + ->values() + ->all(); } /** @@ -94,8 +117,9 @@ public static function toLocalizedSelectArray(?array $blacklist = []): array { return collect(self::membersByBlacklist($blacklist)) ->mapWithKeys(static function (Enumeration $item): array { - return [ $item->value() => $item->localized() ]; - })->all(); + return [$item->value() => $item->localized()]; + }) + ->all(); } /** @@ -108,8 +132,9 @@ public static function toSelectArray(?array $blacklist = []): array { return collect(self::membersByBlacklist($blacklist)) ->mapWithKeys(static function (Enumeration $item): array { - return [ $item->value() => $item->key() ]; - })->all(); + return [$item->value() => $item->key()]; + }) + ->all(); } /** @@ -120,20 +145,9 @@ public static function toSelectArray(?array $blacklist = []): array */ public static function membersByBlacklist(?array $blacklist = []): array { - return collect(self::membersByPredicate(static function (Enumeration $enumValue) use ($blacklist): bool { + return self::membersByPredicate(static function (Enumeration $enumValue) use ($blacklist): bool { return !$enumValue->anyOfArray($blacklist); - }))->all(); - } - - /** - * Get a random member of this enum. - * - * @param array|null $blacklist - * @return static - */ - public static function randomMember(?array $blacklist = []) - { - return collect(self::membersByBlacklist($blacklist))->random(); + }); } /** @@ -168,17 +182,6 @@ public static function makeRuleWithBlacklist(?array $blacklist = []): Enumeratio return self::makeRuleWithWhitelist(self::membersByBlacklist($blacklist)); } - /** - * Get the default enum member. - * Override for your own value / logic. - * - * @return static - */ - public static function defaultMember() - { - return collect(static::members())->first(); - } - /** * Checks if this enum has a member with the given value. * @@ -187,7 +190,7 @@ public static function defaultMember() */ public static function hasValue($value): bool { - return in_array($value, static::values()); + return in_array($value, static::values(), true); } /** @@ -198,7 +201,26 @@ public static function hasValue($value): bool */ public static function hasKey(string $key): bool { - return in_array($key, static::keys()); + return in_array($key, static::keys(), true); + } + + /** + * @param array $arguments + * @return \Illuminate\Contracts\Database\Eloquent\CastsAttributes + */ + public static function castUsing(array $arguments): CastsAttributes + { + return new Enum(static::class, ...$arguments); + } + + /** + * Get the localization path for this enum. + * + * @return string + */ + protected static function getLocalizationPath(): string + { + return static::$localizationPath ?? sprintf('enums.%s', static::class); } /** @@ -227,9 +249,4 @@ public function __call($method, $arguments) return $this->is(static::memberByKey($key, false)); } } - - public static function castUsing(): CastsAttributes - { - return new Enum(static::class); - } } diff --git a/src/Enums/Interfaces/Weighted.php b/src/Enums/Interfaces/Weighted.php index 2e1eba5..e532a5d 100644 --- a/src/Enums/Interfaces/Weighted.php +++ b/src/Enums/Interfaces/Weighted.php @@ -9,64 +9,6 @@ */ interface Weighted extends EnumerationInterface { - /** - * Get members of this enum greater than $weighted. - * - * @param \Sourceboat\Enumeration\Enums\Interfaces\Weighted $weighted - * @return array - */ - public static function getMembersGreaterThan(Weighted $weighted): array; - - /** - * Get members of this enum greater than or equal to $weighted. - * - * @param \Sourceboat\Enumeration\Enums\Interfaces\Weighted $weighted - * @return array - */ - public static function getMembersGreaterThanOrEqualTo(Weighted $weighted): array; - - /** - * Get members of this enum equal to $weighted. - * - * @param \Sourceboat\Enumeration\Enums\Interfaces\Weighted $weighted - * @return array - */ - public static function getMembersEqualTo(Weighted $weighted): array; - - /** - * Get members of this enum less than or equal to $weighted. - * - * @param \Sourceboat\Enumeration\Enums\Interfaces\Weighted $weighted - * @return array - */ - public static function getMembersLessThanOrEqualTo(Weighted $weighted): array; - - /** - * Get members of this enum less than or equal to $weighted. - * - * @param \Sourceboat\Enumeration\Enums\Interfaces\Weighted $weighted - * @return array - */ - public static function getMembersLessThan(Weighted $weighted): array; - - /** - * Get members of this enum between the given members weights. - * - * @param \Sourceboat\Enumeration\Enums\Interfaces\Weighted $lower - * @param \Sourceboat\Enumeration\Enums\Interfaces\Weighted $higher - * @return array - */ - public static function getMembersBetween(Weighted $lower, Weighted $higher): array; - - /** - * Get members of this enum between or equal to the given members weights. - * - * @param \Sourceboat\Enumeration\Enums\Interfaces\Weighted $lower - * @param \Sourceboat\Enumeration\Enums\Interfaces\Weighted $higher - * @return array - */ - public static function getMembersBetweenOrEqualTo(Weighted $lower, Weighted $higher): array; - /** * Get members of this enum greater than this. * @@ -182,4 +124,62 @@ public function isBetween(Weighted $lower, Weighted $higher): bool; * @return bool */ public function isBetweenOrEqualTo(Weighted $lower, Weighted $higher): bool; + + /** + * Get members of this enum greater than $weighted. + * + * @param \Sourceboat\Enumeration\Enums\Interfaces\Weighted $weighted + * @return array + */ + public static function getMembersGreaterThan(Weighted $weighted): array; + + /** + * Get members of this enum greater than or equal to $weighted. + * + * @param \Sourceboat\Enumeration\Enums\Interfaces\Weighted $weighted + * @return array + */ + public static function getMembersGreaterThanOrEqualTo(Weighted $weighted): array; + + /** + * Get members of this enum equal to $weighted. + * + * @param \Sourceboat\Enumeration\Enums\Interfaces\Weighted $weighted + * @return array + */ + public static function getMembersEqualTo(Weighted $weighted): array; + + /** + * Get members of this enum less than or equal to $weighted. + * + * @param \Sourceboat\Enumeration\Enums\Interfaces\Weighted $weighted + * @return array + */ + public static function getMembersLessThanOrEqualTo(Weighted $weighted): array; + + /** + * Get members of this enum less than or equal to $weighted. + * + * @param \Sourceboat\Enumeration\Enums\Interfaces\Weighted $weighted + * @return array + */ + public static function getMembersLessThan(Weighted $weighted): array; + + /** + * Get members of this enum between the given members weights. + * + * @param \Sourceboat\Enumeration\Enums\Interfaces\Weighted $lower + * @param \Sourceboat\Enumeration\Enums\Interfaces\Weighted $higher + * @return array + */ + public static function getMembersBetween(Weighted $lower, Weighted $higher): array; + + /** + * Get members of this enum between or equal to the given members weights. + * + * @param \Sourceboat\Enumeration\Enums\Interfaces\Weighted $lower + * @param \Sourceboat\Enumeration\Enums\Interfaces\Weighted $higher + * @return array + */ + public static function getMembersBetweenOrEqualTo(Weighted $lower, Weighted $higher): array; } diff --git a/src/Enums/Traits/IsConfigurable.php b/src/Enums/Traits/IsConfigurable.php index 7f9745d..24b4e5d 100644 --- a/src/Enums/Traits/IsConfigurable.php +++ b/src/Enums/Traits/IsConfigurable.php @@ -9,21 +9,11 @@ */ trait IsConfigurable { - /** - * Get the config path for this enum. - * - * @return string - */ - public static function configPath(): string - { - return sprintf('enums.%s', static::class); - } - /** * Get the config key from this enum for the given path and subkey. * - * @param string $path - * @param string $subKey + * @param string $path + * @param string $subKey * @return string */ public function getConfigKey(string $path, string $subKey): string @@ -42,4 +32,14 @@ public function config(string $key, $default = null) { return config($this->getConfigKey(static::configPath(), $key), $default); } + + /** + * Get the config path for this enum. + * + * @return string + */ + public static function configPath(): string + { + return sprintf('enums.%s', static::class); + } } diff --git a/src/Enums/Traits/IsWeighted.php b/src/Enums/Traits/IsWeighted.php index 5c965c3..d140c30 100644 --- a/src/Enums/Traits/IsWeighted.php +++ b/src/Enums/Traits/IsWeighted.php @@ -11,109 +11,6 @@ */ trait IsWeighted { - /** - * Get the config key for the weights of this enum. - * - * @return string - */ - public static function getWeightOptionsKey(): string - { - return sprintf('enums.%s.weights', static::class); - } - - /** - * Get members of this enum greater than $weighted. - * - * @param \Sourceboat\Enumeration\Enums\Interfaces\Weighted $weighted - * @return array - */ - public static function getMembersGreaterThan(Weighted $weighted): array - { - return static::membersByPredicate(static function (Weighted $member) use ($weighted): bool { - return $member->isGreaterThan($weighted); - }); - } - - /** - * Get members of this enum greater than or equal to $weighted. - * - * @param \Sourceboat\Enumeration\Enums\Interfaces\Weighted $weighted - * @return array - */ - public static function getMembersGreaterThanOrEqualTo(Weighted $weighted): array - { - return static::membersByPredicate(static function (Weighted $member) use ($weighted): bool { - return $member->isGreaterThanOrEqualTo($weighted); - }); - } - - /** - * Get members of this enum between the given members weights. - * - * @param \Sourceboat\Enumeration\Enums\Interfaces\Weighted $lower - * @param \Sourceboat\Enumeration\Enums\Interfaces\Weighted $higher - * @return array - */ - public static function getMembersBetween(Weighted $lower, Weighted $higher): array - { - return static::membersByPredicate(static function (Weighted $member) use ($lower, $higher): bool { - return $member->isBetween($lower, $higher); - }); - } - - /** - * Get members of this enum between or equal to the given members weights. - * - * @param \Sourceboat\Enumeration\Enums\Interfaces\Weighted $lower - * @param \Sourceboat\Enumeration\Enums\Interfaces\Weighted $higher - * @return array - */ - public static function getMembersBetweenOrEqualTo(Weighted $lower, Weighted $higher): array - { - return static::membersByPredicate(static function (Weighted $member) use ($lower, $higher): bool { - return $member->isBetweenOrEqualTo($lower, $higher); - }); - } - - /** - * Get members of this enum equal to $weighted. - * - * @param \Sourceboat\Enumeration\Enums\Interfaces\Weighted $weighted - * @return array - */ - public static function getMembersEqualTo(Weighted $weighted): array - { - return static::membersByPredicate(static function (Weighted $member) use ($weighted): bool { - return $member->isEqualTo($weighted); - }); - } - - /** - * Get members of this enum less than or equal to $weighted. - * - * @param \Sourceboat\Enumeration\Enums\Interfaces\Weighted $weighted - * @return array - */ - public static function getMembersLessThanOrEqualTo(Weighted $weighted): array - { - return static::membersByPredicate(static function (Weighted $member) use ($weighted): bool { - return $member->isLessThanOrEqualTo($weighted); - }); - } - - /** - * Get members of this enum less than $weighted. - * - * @param \Sourceboat\Enumeration\Enums\Interfaces\Weighted $weighted - * @return array - */ - public static function getMembersLessThan(Weighted $weighted): array - { - return static::membersByPredicate(static function (Weighted $member) use ($weighted): bool { - return $member->isLessThan($weighted); - }); - } - /** * Get members of this enum greater than this. * @@ -164,7 +61,6 @@ public function getMembersLessThanThis(): array return static::getMembersLessThan($this); } - /** * Get members of this enum between this and the given higher bound. * @@ -187,7 +83,6 @@ public function getMembersBetweenOrEqualToThisAnd(Weighted $higher): array return static::getMembersBetweenOrEqualTo($this, $higher); } - /** * Get the weight of this member. * @@ -279,4 +174,107 @@ public function isBetweenOrEqualTo(Weighted $lower, Weighted $higher): bool return $this->weight() >= $lower->weight() && $this->weight() <= $higher->weight(); } + + /** + * Get the config key for the weights of this enum. + * + * @return string + */ + public static function getWeightOptionsKey(): string + { + return sprintf('enums.%s.weights', static::class); + } + + /** + * Get members of this enum greater than $weighted. + * + * @param \Sourceboat\Enumeration\Enums\Interfaces\Weighted $weighted + * @return array + */ + public static function getMembersGreaterThan(Weighted $weighted): array + { + return static::membersByPredicate(static function (Weighted $member) use ($weighted): bool { + return $member->isGreaterThan($weighted); + }); + } + + /** + * Get members of this enum greater than or equal to $weighted. + * + * @param \Sourceboat\Enumeration\Enums\Interfaces\Weighted $weighted + * @return array + */ + public static function getMembersGreaterThanOrEqualTo(Weighted $weighted): array + { + return static::membersByPredicate(static function (Weighted $member) use ($weighted): bool { + return $member->isGreaterThanOrEqualTo($weighted); + }); + } + + /** + * Get members of this enum between the given members weights. + * + * @param \Sourceboat\Enumeration\Enums\Interfaces\Weighted $lower + * @param \Sourceboat\Enumeration\Enums\Interfaces\Weighted $higher + * @return array + */ + public static function getMembersBetween(Weighted $lower, Weighted $higher): array + { + return static::membersByPredicate(static function (Weighted $member) use ($lower, $higher): bool { + return $member->isBetween($lower, $higher); + }); + } + + /** + * Get members of this enum between or equal to the given members weights. + * + * @param \Sourceboat\Enumeration\Enums\Interfaces\Weighted $lower + * @param \Sourceboat\Enumeration\Enums\Interfaces\Weighted $higher + * @return array + */ + public static function getMembersBetweenOrEqualTo(Weighted $lower, Weighted $higher): array + { + return static::membersByPredicate(static function (Weighted $member) use ($lower, $higher): bool { + return $member->isBetweenOrEqualTo($lower, $higher); + }); + } + + /** + * Get members of this enum equal to $weighted. + * + * @param \Sourceboat\Enumeration\Enums\Interfaces\Weighted $weighted + * @return array + */ + public static function getMembersEqualTo(Weighted $weighted): array + { + return static::membersByPredicate(static function (Weighted $member) use ($weighted): bool { + return $member->isEqualTo($weighted); + }); + } + + /** + * Get members of this enum less than or equal to $weighted. + * + * @param \Sourceboat\Enumeration\Enums\Interfaces\Weighted $weighted + * @return array + */ + public static function getMembersLessThanOrEqualTo(Weighted $weighted): array + { + return static::membersByPredicate(static function (Weighted $member) use ($weighted): bool { + return $member->isLessThanOrEqualTo($weighted); + }); + } + + /** + * Get members of this enum less than $weighted. + * + * @param \Sourceboat\Enumeration\Enums\Interfaces\Weighted $weighted + * @return array + */ + public static function getMembersLessThan(Weighted $weighted): array + { + return static::membersByPredicate(static function (Weighted $member) use ($weighted): bool { + return $member->isLessThan($weighted); + }); + } } diff --git a/src/Models/Traits/HasWeightedEnumScopes.php b/src/Models/Traits/HasWeightedEnumScopes.php index 2d734df..2c01350 100644 --- a/src/Models/Traits/HasWeightedEnumScopes.php +++ b/src/Models/Traits/HasWeightedEnumScopes.php @@ -13,7 +13,7 @@ trait HasWeightedEnumScopes /** * Get all models with a column greater than that enum member. * - * @param \Illuminate\Database\Eloquent\Builder $query + * @param \Illuminate\Database\Eloquent\Builder $query * @param string $column * @param \Sourceboat\Enumeration\Enums\Interfaces\Weighted $enumMember * @return \Illuminate\Database\Eloquent\Builder @@ -26,7 +26,7 @@ public function scopeWhereGreaterThanEnum(Builder $query, string $column, Weight /** * Get all models with a column greater than that enum member. * - * @param \Illuminate\Database\Eloquent\Builder $query + * @param \Illuminate\Database\Eloquent\Builder $query * @param string $column * @param \Sourceboat\Enumeration\Enums\Interfaces\Weighted $enumMember * @return \Illuminate\Database\Eloquent\Builder @@ -39,7 +39,7 @@ public function scopeWhereGreaterThanOrEqualToEnum(Builder $query, string $colum /** * Get all models with a column greater than that enum member. * - * @param \Illuminate\Database\Eloquent\Builder $query + * @param \Illuminate\Database\Eloquent\Builder $query * @param string $column * @param \Sourceboat\Enumeration\Enums\Interfaces\Weighted $enumMember * @return \Illuminate\Database\Eloquent\Builder @@ -52,7 +52,7 @@ public function scopeWhereEqualToEnum(Builder $query, string $column, Weighted $ /** * Get all models with a column greater than that enum member. * - * @param \Illuminate\Database\Eloquent\Builder $query + * @param \Illuminate\Database\Eloquent\Builder $query * @param string $column * @param \Sourceboat\Enumeration\Enums\Interfaces\Weighted $enumMember * @return \Illuminate\Database\Eloquent\Builder @@ -65,7 +65,7 @@ public function scopeWhereLessThanOrEqualToEnum(Builder $query, string $column, /** * Get all models with a column greater than that enum member. * - * @param \Illuminate\Database\Eloquent\Builder $query + * @param \Illuminate\Database\Eloquent\Builder $query * @param string $column * @param \Sourceboat\Enumeration\Enums\Interfaces\Weighted $enumMember * @return \Illuminate\Database\Eloquent\Builder @@ -78,7 +78,7 @@ public function scopeWhereLessThanEnum(Builder $query, string $column, Weighted /** * Get all models with a column between the enum members. * - * @param \Illuminate\Database\Eloquent\Builder $query + * @param \Illuminate\Database\Eloquent\Builder $query * @param string $column * @param \Sourceboat\Enumeration\Enums\Interfaces\Weighted $lower * @param \Sourceboat\Enumeration\Enums\Interfaces\Weighted $higher @@ -92,7 +92,7 @@ public function scopeWhereBetweenEnum(Builder $query, string $column, Weighted $ /** * Get all models with a column between or equal to the enum members. * - * @param \Illuminate\Database\Eloquent\Builder $query + * @param \Illuminate\Database\Eloquent\Builder $query * @param string $column * @param \Sourceboat\Enumeration\Enums\Interfaces\Weighted $lower * @param \Sourceboat\Enumeration\Enums\Interfaces\Weighted $higher diff --git a/src/Rules/EnumerationValue.php b/src/Rules/EnumerationValue.php index e4e29dd..b02957a 100644 --- a/src/Rules/EnumerationValue.php +++ b/src/Rules/EnumerationValue.php @@ -45,7 +45,7 @@ public function __construct(string $enum, ?array $values = null) * Determine if the validation rule passes. * * @param string $attribute - * @param mixed $value + * @param mixed $value * @return bool * @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint * @SuppressWarnings(PHPMD.UnusedFormalParameter) diff --git a/tests/FruitType.php b/tests/FruitType.php index dcd8f4c..43c7125 100644 --- a/tests/FruitType.php +++ b/tests/FruitType.php @@ -20,10 +20,10 @@ class FruitType extends Enumeration implements Weighted { use IsWeighted; - protected static $localizationPath = 'test'; - public const BERRY = 'berry'; public const NUT = 'nut'; public const ACCESSORY_FRUIT = 'accessory_fruit'; public const LEGUME = 'legume'; + + protected static $localizationPath = 'test'; }