Skip to content

Commit

Permalink
blade support
Browse files Browse the repository at this point in the history
  • Loading branch information
henzeb committed Jun 8, 2022
1 parent 3b85c65 commit 87f108a
Show file tree
Hide file tree
Showing 5 changed files with 201 additions and 10 deletions.
21 changes: 12 additions & 9 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,25 @@

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

## 1.9.0 - 2022-06-08
- Added [Blade](docs/blade.md) support

## 1.8.0 - 2022-06-07
- added [Helper functions](docs/functions.md) to ease usage of basic enums.
- Added [Helper functions](docs/functions.md) to ease usage of basic enums

## 1.7.0 - 2022-06-06
- When using [Comparison](docs/comparison.md), you can now assert with `is` or `isNot`

## 1.6.0 - 2022-06-04
- added Eloquent Casting support for basic enumerations.
- Added Eloquent Casting support for basic enumerations

## 1.5.0 - 2022-05-31
- added [Extractor](docs/extractor.md) to extract enums from a string mentioned by value
- some documentation repairs
- Added [Extractor](docs/extractor.md) to extract enums from a string mentioned by value
- Some documentation repairs

## 1.4.1 - 2022-03-04

- added `cases` method to `Subset`
- Added `cases` method to `Subset`

## 1.4.0 - 2022-03-02

Expand All @@ -28,15 +31,15 @@ All notable changes to `Enumhancer` will be documented in this file

## 1.3.0 - 2022-02-28

- added Multi. Currently allows you to compare against a subset of your enum
- Added Multi. Currently allows you to compare against a subset of your enum

## 1.2.0 - 2022-02-26

- added Value (for use with basic enums)
- Added Value (for use with basic enums)

## 1.1.0 - 2022-02-25

- added From. Useful for situations where you need them with basic enums
- Added From. Useful for situations where you need them with basic enums

## 1.0.2 - 2022-02-16

Expand All @@ -49,4 +52,4 @@ All notable changes to `Enumhancer` will be documented in this file

## 1.0.0 - 2022-02-15

- initial release
- Initial release
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ This package currently supports the following:
- Value (for basic enums)

Laravel specific features:
- Blade
- Casting (Laravel Specific)

Helper functions:
Expand Down Expand Up @@ -88,7 +89,7 @@ implemented the methods of `Makers`, `Extractor` and `Reporters`.
- [Value](docs/functions.md#value)

### Laravel specific Features

- [Blade](docs/blade.md)
- [Casting](docs/casting.md)

### Laravel's auto-discovery
Expand Down
77 changes: 77 additions & 0 deletions docs/blade.md
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
```

39 changes: 39 additions & 0 deletions src/Helpers/EnumBlade.php
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);
}
);
}
}
71 changes: 71 additions & 0 deletions tests/Unit/Helpers/EnumBladeTest.php
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
)
);
}
}

0 comments on commit 87f108a

Please sign in to comment.