Skip to content

Commit 7491e65

Browse files
author
Kevin Buchholz
committed
Merge branch 'release/3.0.0'
2 parents 704c154 + 7e847e7 commit 7491e65

File tree

11 files changed

+259
-321
lines changed

11 files changed

+259
-321
lines changed

CHANGELOG.md

Lines changed: 0 additions & 77 deletions
This file was deleted.

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ Enum implementation for Laravel. Based on [eloquent/enumeration](https://github.
3434
## Requirements
3535

3636
* eloquent/enumeration 6.0 or newer
37-
* Laravel 7.5 or newer;
37+
* Laravel 8.0 or newer;
3838
* PHP 7.3 or newer
3939

4040
## Install

composer.json

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
"typed",
1414
"extendible",
1515
"castable",
16-
"casts"
16+
"casts",
17+
"php"
1718
],
1819
"license": "MIT",
1920
"homepage": "https://github.com/sourceboat/laravel-enumeration",
@@ -28,13 +29,13 @@
2829
"require": {
2930
"php": ">=7.3",
3031
"eloquent/enumeration": "^6.0",
31-
"illuminate/console": ">=7.5",
32-
"illuminate/contracts": ">=7.5",
33-
"illuminate/support": ">=7.5"
32+
"illuminate/console": "^8.0",
33+
"illuminate/contracts": "^8.0",
34+
"illuminate/support": "^8.0"
3435
},
3536
"require-dev": {
3637
"consistence/coding-standard": "3.10.1",
37-
"orchestra/testbench": "^5.0 || ^6.0",
38+
"orchestra/testbench": "^6.0",
3839
"phpmd/phpmd": "^2.6",
3940
"phpunit/phpunit": "9.*",
4041
"slevomat/coding-standard": "6.4.0",
@@ -59,7 +60,7 @@
5960
},
6061
"extra": {
6162
"branch-alias": {
62-
"dev-master": "1.3.x-dev"
63+
"dev-master": "3.x-dev"
6364
},
6465
"laravel": {
6566
"providers": [

src/Casts/Enum.php

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ public function __construct(string $enumClass, bool $nullable = true)
1919
/**
2020
* Transform the attribute from the underlying model values.
2121
*
22-
* @param \Illuminate\Database\Eloquent\Model $model
23-
* @param string $key
24-
* @param mixed $value
25-
* @param array<mixed> $attributes
22+
* @param \Illuminate\Database\Eloquent\Model $model
23+
* @param string $key
24+
* @param mixed $value
25+
* @param array<mixed> $attributes
2626
* @return self|null
2727
* @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint
2828
* @phpcsSuppress SlevomatCodingStandard.TypeHints.ReturnTypeHint.MissingNativeTypeHint
@@ -44,18 +44,17 @@ public function get($model, string $key, $value, array $attributes)
4444
/**
4545
* Transform the attribute to its underlying model values.
4646
*
47-
* @param \Illuminate\Database\Eloquent\Model $model
48-
* @param string $key
49-
* @param mixed $value
50-
* @param array<mixed> $attributes
47+
* @param \Illuminate\Database\Eloquent\Model $model
48+
* @param string $key
49+
* @param mixed $value
50+
* @param array<mixed> $attributes
5151
* @return array<mixed>
5252
* @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint
5353
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
5454
*/
5555
public function set($model, string $key, $value, array $attributes): array
5656
{
57-
if ($this->enumClass::hasValue($value)
58-
|| ($this->nullable && is_null($value))) {
57+
if ($this->enumClass::hasValue($value) || ($this->nullable && is_null($value))) {
5958
return [
6059
$key => $value,
6160
];

src/Enumeration.php

Lines changed: 59 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,27 @@ abstract class Enumeration extends AbstractEnumeration implements Castable
2424
protected static $localizationPath = null;
2525

2626
/**
27-
* Get the localization path for this enum.
27+
* Get the default enum member.
28+
* Override for your own value / logic.
2829
*
29-
* @return string
30+
* @return static
3031
*/
31-
protected static function getLocalizationPath(): string
32+
public static function defaultMember()
3233
{
33-
return static::$localizationPath ?? sprintf('enums.%s', static::class);
34+
return collect(static::members())
35+
->first();
36+
}
37+
38+
/**
39+
* Get a random member of this enum.
40+
*
41+
* @param array<mixed>|null $blacklist
42+
* @return static
43+
*/
44+
public static function randomMember(?array $blacklist = [])
45+
{
46+
return collect(self::membersByBlacklist($blacklist))
47+
->random();
3448
}
3549

3650
/**
@@ -61,7 +75,10 @@ public function is($value): bool
6175
*/
6276
public static function values(): array
6377
{
64-
return collect(self::members())->map->value()->values()->all();
78+
return collect(self::members())
79+
->map->value()
80+
->values()
81+
->all();
6582
}
6683

6784
/**
@@ -71,7 +88,10 @@ public static function values(): array
7188
*/
7289
public static function localizedValues(): array
7390
{
74-
return collect(self::members())->map->localized()->values()->all();
91+
return collect(self::members())
92+
->map->localized()
93+
->values()
94+
->all();
7595
}
7696

7797
/**
@@ -81,7 +101,10 @@ public static function localizedValues(): array
81101
*/
82102
public static function keys(): array
83103
{
84-
return collect(self::members())->map->key()->values()->all();
104+
return collect(self::members())
105+
->map->key()
106+
->values()
107+
->all();
85108
}
86109

87110
/**
@@ -94,8 +117,9 @@ public static function toLocalizedSelectArray(?array $blacklist = []): array
94117
{
95118
return collect(self::membersByBlacklist($blacklist))
96119
->mapWithKeys(static function (Enumeration $item): array {
97-
return [ $item->value() => $item->localized() ];
98-
})->all();
120+
return [$item->value() => $item->localized()];
121+
})
122+
->all();
99123
}
100124

101125
/**
@@ -108,8 +132,9 @@ public static function toSelectArray(?array $blacklist = []): array
108132
{
109133
return collect(self::membersByBlacklist($blacklist))
110134
->mapWithKeys(static function (Enumeration $item): array {
111-
return [ $item->value() => $item->key() ];
112-
})->all();
135+
return [$item->value() => $item->key()];
136+
})
137+
->all();
113138
}
114139

115140
/**
@@ -120,20 +145,9 @@ public static function toSelectArray(?array $blacklist = []): array
120145
*/
121146
public static function membersByBlacklist(?array $blacklist = []): array
122147
{
123-
return collect(self::membersByPredicate(static function (Enumeration $enumValue) use ($blacklist): bool {
148+
return self::membersByPredicate(static function (Enumeration $enumValue) use ($blacklist): bool {
124149
return !$enumValue->anyOfArray($blacklist);
125-
}))->all();
126-
}
127-
128-
/**
129-
* Get a random member of this enum.
130-
*
131-
* @param array<mixed>|null $blacklist
132-
* @return static
133-
*/
134-
public static function randomMember(?array $blacklist = [])
135-
{
136-
return collect(self::membersByBlacklist($blacklist))->random();
150+
});
137151
}
138152

139153
/**
@@ -168,17 +182,6 @@ public static function makeRuleWithBlacklist(?array $blacklist = []): Enumeratio
168182
return self::makeRuleWithWhitelist(self::membersByBlacklist($blacklist));
169183
}
170184

171-
/**
172-
* Get the default enum member.
173-
* Override for your own value / logic.
174-
*
175-
* @return static
176-
*/
177-
public static function defaultMember()
178-
{
179-
return collect(static::members())->first();
180-
}
181-
182185
/**
183186
* Checks if this enum has a member with the given value.
184187
*
@@ -187,7 +190,7 @@ public static function defaultMember()
187190
*/
188191
public static function hasValue($value): bool
189192
{
190-
return in_array($value, static::values());
193+
return in_array($value, static::values(), true);
191194
}
192195

193196
/**
@@ -198,7 +201,26 @@ public static function hasValue($value): bool
198201
*/
199202
public static function hasKey(string $key): bool
200203
{
201-
return in_array($key, static::keys());
204+
return in_array($key, static::keys(), true);
205+
}
206+
207+
/**
208+
* @param array<mixed> $arguments
209+
* @return \Illuminate\Contracts\Database\Eloquent\CastsAttributes
210+
*/
211+
public static function castUsing(array $arguments): CastsAttributes
212+
{
213+
return new Enum(static::class, ...$arguments);
214+
}
215+
216+
/**
217+
* Get the localization path for this enum.
218+
*
219+
* @return string
220+
*/
221+
protected static function getLocalizationPath(): string
222+
{
223+
return static::$localizationPath ?? sprintf('enums.%s', static::class);
202224
}
203225

204226
/**
@@ -227,9 +249,4 @@ public function __call($method, $arguments)
227249
return $this->is(static::memberByKey($key, false));
228250
}
229251
}
230-
231-
public static function castUsing(): CastsAttributes
232-
{
233-
return new Enum(static::class);
234-
}
235252
}

0 commit comments

Comments
 (0)