-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
201 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
# Blade | ||
|
||
Currently, in blade enums aren't casted to strings. Due to limitations, your can't automate this by just adding | ||
UnitEnum/BackedEnum as stringables. Using this feature allows you easy registering of your enums for use in blade. | ||
|
||
## Example | ||
```php | ||
use Henzeb\Enumhancer\Concerns\Value; | ||
|
||
enum MyUnitEnum { | ||
use Value; | ||
|
||
case Enum; | ||
} | ||
|
||
enum MyStringEnum: string { | ||
case Enum = 'My Enum'; | ||
} | ||
|
||
enum MyIntEnum: int { | ||
case Enum = 0; | ||
} | ||
``` | ||
|
||
In your Service Provider: | ||
```php | ||
use Henzeb\Enumhancer\Helpers\EnumBlade; | ||
|
||
EnumBlade::register(MyUnitEnum::class, MyStringEnum::class, MyIntEnum::class); | ||
|
||
/** When you want the value to be lower case */ | ||
EnumBlade::registerLowercase(MyUnitEnum::class, MyStringEnum::class, MyIntEnum::class); | ||
``` | ||
|
||
In your blade file: | ||
```php | ||
/** With register */ | ||
{{$unitEnum}} // Enum | ||
{{$unitEnum->name}} // Enum | ||
{{$unitEnum->value}} // throws error | ||
{{$unitEnum->value()}} // enum | ||
{{$unitEnum instanceof \UnitEnum}} // 1 | ||
{{$unitEnum instanceof \BackedEnum}} // 0 | ||
|
||
{{$stringEnum}} // My Enum | ||
{{$stringEnum->name}} // Enum | ||
{{$stringEnum->value}} // My Enum | ||
{{$stringEnum instanceof \UnitEnum}} // 0 | ||
{{$stringEnum instanceof \BackedEnum}} // 1 | ||
|
||
{{$intEnum}} // 0 | ||
{{$intEnum->name}} // Enum | ||
{{$intEnum->value}} // 0 | ||
{{$intEnum instanceof \UnitEnum}} // 0 | ||
{{$intEnum instanceof \BackedEnum}} // 1 | ||
|
||
/** With registerLowercase */ | ||
{{$unitEnum}} // enum | ||
{{$unitEnum->name}} // Enum | ||
{{$unitEnum->value}} // throws error | ||
{{$unitEnum->value()}} // enum | ||
{{$unitEnum instanceof \UnitEnum}} // 1 | ||
{{$unitEnum instanceof \BackedEnum}} // 0 | ||
|
||
{{$stringEnum}} // My Enum | ||
{{$stringEnum->name}} // Enum | ||
{{$stringEnum->value}} // My Enum | ||
{{$stringEnum instanceof \UnitEnum}} // 0 | ||
{{$stringEnum instanceof \BackedEnum}} // 1 | ||
|
||
{{$intEnum}} // 0 | ||
{{$intEnum->name}} // Enum | ||
{{$intEnum->value}} // 0 | ||
{{$intEnum instanceof \UnitEnum}} // 0 | ||
{{$intEnum instanceof \BackedEnum}} // 1 | ||
``` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
namespace Henzeb\Enumhancer\Helpers; | ||
|
||
use Blade; | ||
use UnitEnum; | ||
use function Henzeb\Enumhancer\Functions\backing; | ||
use function Henzeb\Enumhancer\Functions\value as value; | ||
|
||
abstract class EnumBlade | ||
{ | ||
public static function register(string ...$enumclasses): void | ||
{ | ||
foreach ($enumclasses as $enumClass) { | ||
self::add($enumClass, true); | ||
} | ||
} | ||
|
||
public static function registerLowercase(string ...$enumclasses): void | ||
{ | ||
foreach ($enumclasses as $enumClass) { | ||
self::add($enumClass, false); | ||
} | ||
} | ||
|
||
private static function add(string $enumClass, bool $keepValueCase): void | ||
{ | ||
if (!is_subclass_of($enumClass, UnitEnum::class, true)) { | ||
throw new \RuntimeException('not an enum class'); | ||
} | ||
|
||
Blade::stringable( | ||
$enumClass, | ||
function (UnitEnum $enum) use ($keepValueCase): string { | ||
return value($enum, $keepValueCase); | ||
} | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?php | ||
|
||
namespace Henzeb\Enumhancer\Tests\Unit\Helpers; | ||
|
||
use UnitEnum; | ||
use Orchestra\Testbench\TestCase; | ||
use Henzeb\Enumhancer\Helpers\EnumBlade; | ||
use Illuminate\View\Compilers\BladeCompiler; | ||
use Henzeb\Enumhancer\Tests\Fixtures\IntBackedEnum; | ||
use Henzeb\Enumhancer\Tests\Fixtures\EnhancedUnitEnum; | ||
use Henzeb\Enumhancer\Tests\Fixtures\EnhancedBackedEnum; | ||
use function Henzeb\Enumhancer\Functions\name; | ||
use function Henzeb\Enumhancer\Functions\value; | ||
use function Henzeb\Enumhancer\Functions\backing; | ||
|
||
class EnumBladeTest extends TestCase | ||
{ | ||
public function providesTestcases(): array | ||
{ | ||
return [ | ||
'int-backed' => [IntBackedEnum::TEST], | ||
'string-backed' => [EnhancedBackedEnum::ENUM], | ||
'unit' => [EnhancedUnitEnum::ENUM], | ||
|
||
'int-backed-lower' => [IntBackedEnum::TEST, false], | ||
'string-backed-lower' => [EnhancedBackedEnum::ENUM, false], | ||
'unit-lower' => [EnhancedUnitEnum::ENUM, false], | ||
]; | ||
} | ||
|
||
/** | ||
* @param UnitEnum $enum | ||
* @param bool $keepValueCase | ||
* @return void | ||
* | ||
* @dataProvider providesTestcases | ||
*/ | ||
public function testShouldRenderValue(UnitEnum $enum, bool $keepValueCase = true): void | ||
{ | ||
$method = $keepValueCase ? 'register' : 'registerLowercase'; | ||
EnumBlade::$method($enum::class); | ||
|
||
$this->assertEquals( | ||
(string)value($enum, $keepValueCase), | ||
BladeCompiler::render('{{ $data }}', | ||
['data' => $enum], true | ||
) | ||
); | ||
|
||
$this->assertEquals( | ||
backing($enum, $keepValueCase)->value, | ||
BladeCompiler::render('{{ $data }}', | ||
['data' => backing($enum, $keepValueCase)], true | ||
) | ||
); | ||
|
||
$this->assertEquals( | ||
backing($enum, $keepValueCase)->value, | ||
BladeCompiler::render('{{ $data->value }}', | ||
['data' => backing($enum, $keepValueCase)], true | ||
) | ||
); | ||
|
||
$this->assertEquals( | ||
name($enum), | ||
BladeCompiler::render('{{ $data->name }}', | ||
['data' => $enum], true | ||
) | ||
); | ||
} | ||
} |