Skip to content

Commit

Permalink
implicit enum binding
Browse files Browse the repository at this point in the history
  • Loading branch information
henzeb committed Jan 6, 2023
1 parent b7575e0 commit ea31829
Show file tree
Hide file tree
Showing 45 changed files with 1,332 additions and 162 deletions.
1 change: 1 addition & 0 deletions .codeclimate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ plugins:
MD029:
enabled: false
phpcodesniffer:
channel: "beta"
config:
standard: "PSR1,PSR2"
exclude_patterns:
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

All notable changes to `Enumhancer` will be documented in this file

## 1.21.0 - 2023-01-06

- added [(basic) enum binding](docs/binding.md) allowing you to bind
basic enumerations to your routes and use Enumhancers secret sauce.
- Fixed a lot of potential issues with PHPstan.

## 1.20.0 - 2023-01-04

- bugfix in [Default](docs/defaults.md) where configured defaults would
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ implemented the methods of `Getters`, `Extractor` and `Reporters`.

- [Blade](docs/blade.md)
- [Casting](docs/casting.md)
- [Implicit (basic) enum binding](docs/binding.md)
- [Validation](docs/laravel.validation.md)

### Laravel's auto-discovery
Expand Down
11 changes: 8 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@
"bitmask",
"bitmasks",
"macros",
"macroable"
"macroable",
"binding"
],
"homepage": "https://github.com/henzeb/enumhancer",
"license": "AGPL-3.0-only",
Expand All @@ -68,8 +69,12 @@
},
"require-dev": {
"mockery/mockery": "^1.5",
"phpunit/phpunit": "^9.5.27",
"orchestra/testbench": "v6.24.1|^7.0"
"nunomaduro/larastan": "^2.3",
"orchestra/testbench": "v6.24.1|^7.0",
"phpstan/phpstan": "^1.9",
"phpstan/phpstan-mockery": "^1.1",
"phpstan/phpstan-phpunit": "^1.3",
"phpunit/phpunit": "^9.5.27"
},
"autoload": {
"files": [
Expand Down
110 changes: 110 additions & 0 deletions docs/binding.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
# Implicit enum binding

Laravel already supports enum binding for routes out of the box. Enumhancer
just adds some of it's famous secret flavor to level up it's potential.

This feature is loaded out of the box and does not alone allow you to bind
basic enums, but also allows you to use features like [Mappers](mappers.md) on
both basic as string and int backed enums as well.

## binding a simple enum

````php
enum Suit {
case Hearts;
case Diamonds;
case Spades;
case Clubs;
}

Illuminate\Support\Facades\Route::get(
'/card/{card}',
function (Suit $card) {
print $card->name;
}
);

/card/Hearts // prints 'Hearts'
/card/diamonds // prints 'Diamonds'
/card/0 // prints 'Hearts'

````

## binding optionally

````php
enum Suit {
case Hearts;
case Diamonds;
case Spades;
case Clubs;
}

Illuminate\Support\Facades\Route::get(
'/card/{card?}',
function (Suit $card = null) {
print $card->name ?? 'nothing';
}
);

/card/Hearts // prints 'Hearts'
/card/ // prints 'nothing'

````

## binding optionally with default

````php
enum Suit {
case Hearts;
case Diamonds;
case Spades;
case Clubs;

const Default = Suit::Clubs;
}

Illuminate\Support\Facades\Route::get(
'/card/{card?}',
function (Suit $card) {
print $card->name;
}
);

/card/Hearts // prints 'Hearts'
/card/ // prints 'Clubs'
````

## Binding an int-backed enum

Binding an int-backed enum to your routes is just as easy as with basic enums.

````php
enum Suit: int {
case Hearts = 1;
case Diamonds = 5 ;
case Spades = 10 ;
case Clubs = 15;
}

Illuminate\Support\Facades\Route::get(
'/card/{card}',
function (Suit $card) {
print $card->name;
}
);

/card/Hearts // prints 'Hearts'
/card/diamonds // prints 'Diamonds'
/card/0 // prints 'Hearts'
/card/15 // prints 'Clubs'

````

## Binding a string-backed enum

Laravel has its own enum binding in place for string-backed enums. Under the
hood, Enumhancer is just adding another middleware and gives it priority over
Laravel's binding middleware. When a string-backed enum is used, Enumhancer maps
the given value if needed and replaces the requested parameter value with the
correct value.
Loading

0 comments on commit ea31829

Please sign in to comment.