Skip to content

Commit 4f4f104

Browse files
author
Kevin Buchholz
committed
Merge branch 'release/2.1.0'
2 parents def9274 + 6bf8b9f commit 4f4f104

File tree

6 files changed

+61
-7
lines changed

6 files changed

+61
-7
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@
33
## Next release
44

55

6+
## 2.1.0 - 2020-04-08 - minor release
7+
8+
### Added
9+
- Implementation of the `Castable` interface of Laravel 7.5
10+
11+
612
## 2.0.0 - 2020-03-29 - major release
713

814
### Added

README.md

Lines changed: 19 additions & 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 or newer;
37+
* Laravel 7.5 or newer;
3838
* PHP 7.3 or newer
3939

4040
## Install
@@ -314,6 +314,24 @@ echo $type === UserType::defaultMember(); // "true"
314314
echo $type->value; // "0"
315315
```
316316

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+
317335
## Weighted Enums
318336

319337
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.

composer.json

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111
"validation",
1212
"localization",
1313
"typed",
14-
"extendible"
14+
"extendible",
15+
"castable",
16+
"casts"
1517
],
1618
"license": "MIT",
1719
"homepage": "https://github.com/sourceboat/laravel-enumeration",
@@ -26,15 +28,15 @@
2628
"require": {
2729
"php": ">=7.3",
2830
"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"
3234
},
3335
"require-dev": {
3436
"consistence/coding-standard": "3.10",
3537
"orchestra/testbench": "^5.0",
3638
"phpmd/phpmd": "^2.6",
37-
"phpunit/phpunit": "8.3.* || 8.4.* || 8.5.* || 9.0.*",
39+
"phpunit/phpunit": "9.*",
3840
"slevomat/coding-standard": "6.0.8",
3941
"squizlabs/php_codesniffer": "^3.5"
4042
},

src/Enumeration.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,18 @@
33
namespace Sourceboat\Enumeration;
44

55
use Eloquent\Enumeration\AbstractEnumeration;
6+
use Illuminate\Contracts\Database\Eloquent\Castable;
7+
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
68
use Illuminate\Support\Str;
9+
use Sourceboat\Enumeration\Casts\Enum;
710
use Sourceboat\Enumeration\Rules\EnumerationValue;
811

912
/**
1013
* Abstract class to extend from for enum functionality.
1114
*
1215
* @SuppressWarnings(PHPMD.TooManyPublicMethods)
1316
*/
14-
abstract class Enumeration extends AbstractEnumeration
17+
abstract class Enumeration extends AbstractEnumeration implements Castable
1518
{
1619
/**
1720
* Path to the localization for the enum-values.
@@ -224,4 +227,9 @@ public function __call($method, $arguments)
224227
return $this->is(static::memberByKey($key, false));
225228
}
226229
}
230+
231+
public static function castUsing(): CastsAttributes
232+
{
233+
return new Enum(static::class);
234+
}
227235
}

tests/TestModel.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@ class TestModel extends Model
1010
protected $casts = [
1111
'role' => Enum::class . ':' . UserRole::class . ',0',
1212
'type' => Enum::class . ':' . FruitType::class,
13+
'typeCastable' => FruitType::class,
1314
];
1415
}

tests/feature/ModelCastsEnumsTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public function testGetDefaultMemberWhenNotSet(): void
3030
public function testGetNullWhenNotSetAndNullable(): void
3131
{
3232
$this->assertNull($this->model->type);
33+
$this->assertNull($this->model->typeCastable);
3334
}
3435

3536
/**
@@ -75,6 +76,24 @@ public function testSetterValidationWithCorrectNullValue(): void
7576
}
7677
}
7778

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+
7897
/**
7998
* Check if the validation when setting an enum-property
8099
* with a correct skalar value throws no exception.

0 commit comments

Comments
 (0)