File tree 6 files changed +61
-7
lines changed
6 files changed +61
-7
lines changed Original file line number Diff line number Diff line change 3
3
## Next release
4
4
5
5
6
+ ## 2.1.0 - 2020-04-08 - minor release
7
+
8
+ ### Added
9
+ - Implementation of the ` Castable ` interface of Laravel 7.5
10
+
11
+
6
12
## 2.0.0 - 2020-03-29 - major release
7
13
8
14
### Added
Original file line number Diff line number Diff line change @@ -34,7 +34,7 @@ Enum implementation for Laravel. Based on [eloquent/enumeration](https://github.
34
34
## Requirements
35
35
36
36
* eloquent/enumeration 6.0 or newer
37
- * Laravel 7 or newer;
37
+ * Laravel 7.5 or newer;
38
38
* PHP 7.3 or newer
39
39
40
40
## Install
@@ -314,6 +314,24 @@ echo $type === UserType::defaultMember(); // "true"
314
314
echo $type->value; // "0"
315
315
```
316
316
317
+ Since Laravel 7.5 and version 2.1 of this package it is also possible to use the enum itself as cast type hint, but beware that ` null ` is always an allowed value.
318
+
319
+ ``` php
320
+ <?php
321
+
322
+ namespace App\Models;
323
+
324
+ use App\Enums\UserType;
325
+ use Illuminate\Database\Eloquent\Model;
326
+
327
+ class User extends Model
328
+ {
329
+ protected $casts = [
330
+ 'type' => UserType::class,
331
+ ];
332
+ }
333
+ ```
334
+
317
335
## Weighted Enums
318
336
319
337
It is Possible to define weights for enum members. the standard way is to define the weights a config file and them implement the ` Weighted ` -interface with the ` IsWeighted ` -trait and define the path to your config. The weights can be defined as integer or float values.
Original file line number Diff line number Diff line change 11
11
" validation" ,
12
12
" localization" ,
13
13
" typed" ,
14
- " extendible"
14
+ " extendible" ,
15
+ " castable" ,
16
+ " casts"
15
17
],
16
18
"license" : " MIT" ,
17
19
"homepage" : " https://github.com/sourceboat/laravel-enumeration" ,
26
28
"require" : {
27
29
"php" : " >=7.3" ,
28
30
"eloquent/enumeration" : " ^6.0" ,
29
- "illuminate/console" : " >=7.0 " ,
30
- "illuminate/contracts" : " >=7.0 " ,
31
- "illuminate/support" : " >=7.0 "
31
+ "illuminate/console" : " >=7.5 " ,
32
+ "illuminate/contracts" : " >=7.5 " ,
33
+ "illuminate/support" : " >=7.5 "
32
34
},
33
35
"require-dev" : {
34
36
"consistence/coding-standard" : " 3.10" ,
35
37
"orchestra/testbench" : " ^5.0" ,
36
38
"phpmd/phpmd" : " ^2.6" ,
37
- "phpunit/phpunit" : " 8.3.* || 8.4.* || 8.5.* || 9.0 .*" ,
39
+ "phpunit/phpunit" : " 9 .*" ,
38
40
"slevomat/coding-standard" : " 6.0.8" ,
39
41
"squizlabs/php_codesniffer" : " ^3.5"
40
42
},
Original file line number Diff line number Diff line change 3
3
namespace Sourceboat \Enumeration ;
4
4
5
5
use Eloquent \Enumeration \AbstractEnumeration ;
6
+ use Illuminate \Contracts \Database \Eloquent \Castable ;
7
+ use Illuminate \Contracts \Database \Eloquent \CastsAttributes ;
6
8
use Illuminate \Support \Str ;
9
+ use Sourceboat \Enumeration \Casts \Enum ;
7
10
use Sourceboat \Enumeration \Rules \EnumerationValue ;
8
11
9
12
/**
10
13
* Abstract class to extend from for enum functionality.
11
14
*
12
15
* @SuppressWarnings(PHPMD.TooManyPublicMethods)
13
16
*/
14
- abstract class Enumeration extends AbstractEnumeration
17
+ abstract class Enumeration extends AbstractEnumeration implements Castable
15
18
{
16
19
/**
17
20
* Path to the localization for the enum-values.
@@ -224,4 +227,9 @@ public function __call($method, $arguments)
224
227
return $ this ->is (static ::memberByKey ($ key , false ));
225
228
}
226
229
}
230
+
231
+ public static function castUsing (): CastsAttributes
232
+ {
233
+ return new Enum (static ::class);
234
+ }
227
235
}
Original file line number Diff line number Diff line change @@ -10,5 +10,6 @@ class TestModel extends Model
10
10
protected $ casts = [
11
11
'role ' => Enum::class . ': ' . UserRole::class . ',0 ' ,
12
12
'type ' => Enum::class . ': ' . FruitType::class,
13
+ 'typeCastable ' => FruitType::class,
13
14
];
14
15
}
Original file line number Diff line number Diff line change @@ -30,6 +30,7 @@ public function testGetDefaultMemberWhenNotSet(): void
30
30
public function testGetNullWhenNotSetAndNullable (): void
31
31
{
32
32
$ this ->assertNull ($ this ->model ->type );
33
+ $ this ->assertNull ($ this ->model ->typeCastable );
33
34
}
34
35
35
36
/**
@@ -75,6 +76,24 @@ public function testSetterValidationWithCorrectNullValue(): void
75
76
}
76
77
}
77
78
79
+ /**
80
+ * Check if the validation when setting an enum-property
81
+ * with a correct value of null throws no exception.
82
+ */
83
+ public function testSetterValidationWithCorrectNullValueCastable (): void
84
+ {
85
+ try {
86
+ $ this ->model ->typeCastable = FruitType::NUT ();
87
+ $ this ->assertEquals (FruitType::NUT (), $ this ->model ->typeCastable );
88
+
89
+ $ this ->model ->typeCastable = null ;
90
+ $ this ->assertTrue (true );
91
+ $ this ->assertNull ($ this ->model ->typeCastable );
92
+ } catch (UndefinedMemberException $ e ) {
93
+ $ this ->assertTrue (false , 'Correct value was rejected ' );
94
+ }
95
+ }
96
+
78
97
/**
79
98
* Check if the validation when setting an enum-property
80
99
* with a correct skalar value throws no exception.
You can’t perform that action at this time.
0 commit comments