Skip to content

#19 add laravel 8 support #20

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"typed",
"extendible",
"castable",
"casts"
"casts",
"php"
],
"license": "MIT",
"homepage": "https://github.com/sourceboat/laravel-enumeration",
Expand All @@ -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",
Expand All @@ -59,7 +60,7 @@
},
"extra": {
"branch-alias": {
"dev-master": "1.3.x-dev"
"dev-master": "3.x-dev"
},
"laravel": {
"providers": [
Expand Down
19 changes: 9 additions & 10 deletions src/Casts/Enum.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<mixed> $attributes
* @param \Illuminate\Database\Eloquent\Model $model
* @param string $key
* @param mixed $value
* @param array<mixed> $attributes
* @return self|null
* @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint
* @phpcsSuppress SlevomatCodingStandard.TypeHints.ReturnTypeHint.MissingNativeTypeHint
Expand All @@ -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<mixed> $attributes
* @param \Illuminate\Database\Eloquent\Model $model
* @param string $key
* @param mixed $value
* @param array<mixed> $attributes
* @return array<mixed>
* @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,
];
Expand Down
101 changes: 59 additions & 42 deletions src/Enumeration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<mixed>|null $blacklist
* @return static
*/
public static function randomMember(?array $blacklist = [])
{
return collect(self::membersByBlacklist($blacklist))
->random();
}

/**
Expand Down Expand Up @@ -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();
}

/**
Expand 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();
}

/**
Expand 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();
}

/**
Expand 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();
}

/**
Expand 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();
}

/**
Expand 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<mixed>|null $blacklist
* @return static
*/
public static function randomMember(?array $blacklist = [])
{
return collect(self::membersByBlacklist($blacklist))->random();
});
}

/**
Expand Down Expand Up @@ -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.
*
Expand All @@ -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);
}

/**
Expand All @@ -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<mixed> $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);
}

/**
Expand Down Expand Up @@ -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);
}
}
116 changes: 58 additions & 58 deletions src/Enums/Interfaces/Weighted.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<static>
*/
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<static>
*/
public static function getMembersGreaterThanOrEqualTo(Weighted $weighted): array;

/**
* Get members of this enum equal to $weighted.
*
* @param \Sourceboat\Enumeration\Enums\Interfaces\Weighted $weighted
* @return array<static>
*/
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<static>
*/
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<static>
*/
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<static>
*/
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<static>
*/
public static function getMembersBetweenOrEqualTo(Weighted $lower, Weighted $higher): array;

/**
* Get members of this enum greater than this.
*
Expand Down Expand Up @@ -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<static>
*/
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<static>
*/
public static function getMembersGreaterThanOrEqualTo(Weighted $weighted): array;

/**
* Get members of this enum equal to $weighted.
*
* @param \Sourceboat\Enumeration\Enums\Interfaces\Weighted $weighted
* @return array<static>
*/
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<static>
*/
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<static>
*/
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<static>
*/
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<static>
*/
public static function getMembersBetweenOrEqualTo(Weighted $lower, Weighted $higher): array;
}
Loading