Skip to content

Commit

Permalink
Merge pull request #30 from simplesquid/2.3.x
Browse files Browse the repository at this point in the history
Lots of new features 🎉:

- Add a customisable select filter.
- Add a customisable boolean filter.
- Add a flagged enum field.
- Drop support for Laravel `7.x`
- Drop support for `laravel-enum < 3.1`.
- Refactor and simplify tests.
  • Loading branch information
mdpoulter authored Jan 25, 2021
2 parents 6b508f5 + 8c9a064 commit ac53cad
Show file tree
Hide file tree
Showing 35 changed files with 1,551 additions and 251 deletions.
6 changes: 2 additions & 4 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,10 @@ jobs:
fail-fast: true
matrix:
os: [ ubuntu-latest ]
php: [ 7.3, 7.4 ]
laravel: [ 7.*, 8.* ]
php: [ 7.3, 7.4, 8.0 ]
laravel: [ 8.* ]
stability: [ prefer-lowest, prefer-stable ]
include:
- laravel: 7.*
testbench: 5.*
- laravel: 8.*
testbench: 6.*

Expand Down
25 changes: 22 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,29 @@

All notable changes to `nova-enum-field` will be documented in this file.

## 2.3.0 - 2021-01-25

- Drop support for Laravel `7.x`
- Drop support for `laravel-enum < 3.1`.
- Add a customisable select filter.
- Add a customisable boolean filter.
- Add a flagged enum field.
- Refactor and simplify tests.

## 2.2.0 - 2020-09-25

- Add support for Laravel `8.x`.
- Add support for PHP `8.0`.
- Allow editing enums without casts.

## 2.1.0 - 2020-09-01

- Add support for `laravel-enum 2.2.0`.

## 2.0.0 - 2020-07-08

- Update `larave-enum` to `2.x`
- Drop support for laravel < `7.x`
- Add support for `laravel-enum 2.x`.
- Drop support for Laravel `< 7.x`.

## 1.1.0 - 2019-09-30

Expand All @@ -16,7 +35,7 @@ All notable changes to `nova-enum-field` will be documented in this file.
## 1.0.4 - 2019-09-27

- Add documentation.
- Refactor field code.
- Refactor `Enum` field code.

## 1.0.3 - 2019-09-27

Expand Down
100 changes: 95 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ composer require simplesquid/nova-enum-field

## Setup

This package requires that you use Attribute Casting in your models. From the docs at [BenSampo/laravel-enum](https://github.com/BenSampo/laravel-enum#attribute-casting), this can be done like so:
It is strongly recommended that you use Attribute Casting in your models. From the docs at [BenSampo/laravel-enum](https://github.com/BenSampo/laravel-enum#attribute-casting), this can be done like this:

```php
use App\Enums\UserType;
use BenSampo\Enum\Traits\CastsEnums;
use BenSampo\Enum\Tests\Enums\UserType;
use Illuminate\Database\Eloquent\Model;

class Example extends Model
Expand All @@ -38,12 +38,12 @@ class Example extends Model

## Usage

You can use the `Enum` field in your Nova resource like so:
You can use the `Enum` field in your Nova resource like this:

```php
namespace App\Nova;

use BenSampo\Enum\Tests\Enums\UserType;
use App\Enums\UserType;
use SimpleSquid\Nova\Fields\Enum\Enum;

class Example extends Resource
Expand All @@ -55,14 +55,104 @@ class Example extends Resource
return [
// ...

Enum::make('User Type')->attachEnum(UserType::class),
Enum::make('User Type')->attach(UserType::class),

// ...
];
}
}
```

### Flagged Enums

You can use the `FlaggedEnum` field in your Nova resource like this (see [Flagged/Bitwise Enum](https://github.com/BenSampo/laravel-enum#flaggedbitwise-enum) setup):

```php
namespace App\Nova;

use App\Enums\UserPermissions;
use SimpleSquid\Nova\Fields\Enum\FlaggedEnum;

class Example extends Resource
{
// ...

public function fields(Request $request)
{
return [
// ...

FlaggedEnum::make('User Permissions')->attach(UserPermissions::class),

// ...
];
}
}
```

### Filters

If you would like to use the provided Nova Select filter (which is compatible with both the `Enum` and `FlaggedEnum` fields), you can include it like this:

```php
namespace App\Nova;

use App\Enums\UserPermissions;
use App\Enums\UserType;
use SimpleSquid\Nova\Fields\Enum\EnumFilter;

class Example extends Resource
{
// ...

public function filters(Request $request)
{
return [
new EnumFilter('user_type', UserType::class),

new EnumFilter('user_permissions', UserPermissions::class),

// Or with optional filter name:
(new EnumFilter('user_type', UserType::class))
->name('Type of user'),
];
}
}
```

Alternatively, you may wish to use the provided Nova Boolean filter (which is also compatible with both the `Enum` and `FlaggedEnum` fields):

```php
namespace App\Nova;

use App\Enums\UserPermissions;
use App\Enums\UserType;
use SimpleSquid\Nova\Fields\Enum\EnumBooleanFilter;

class Example extends Resource
{
// ...

public function filters(Request $request)
{
return [
new EnumBooleanFilter('user_type', UserType::class),

new EnumBooleanFilter('user_permissions', UserPermissions::class),

// Or with optional filter name:
(new EnumBooleanFilter('user_type', UserType::class))
->name('Type of user'),

// When filtering a FlaggedEnum, it will default to filtering
// by ANY flags, however you may wish to filter by ALL flags:
(new EnumBooleanFilter('user_permissions', UserPermissions::class))
->filterAllFlags(),
];
}
}
```

## Testing

``` bash
Expand Down
17 changes: 12 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,16 @@
],
"require": {
"php": "^7.3|^8.0",
"bensampo/laravel-enum": "^2.0|^3.0",
"cakephp/chronos": "^1.2.3|^2.0",
"illuminate/support": "^7.0|^8.0",
"bensampo/laravel-enum": "^3.1",
"illuminate/support": "^8.0",
"laravel/nova": "^3.0"
},
"require-dev": {
"orchestra/testbench": "^5.0|^6.0",
"phpunit/phpunit": "^8.2|^9.0",
"joshgaber/novaunit": "^2.2",
"mockery/mockery": "^1.3.3",
"nunomaduro/collision": "^5.2",
"orchestra/testbench": "^6.0",
"phpunit/phpunit": "^9.3.3",
"symfony/var-dumper": "^5.0"
},
"autoload": {
Expand All @@ -47,6 +49,11 @@
"SimpleSquid\\Nova\\Fields\\Enum\\Tests\\": "tests"
}
},
"extra": {
"laravel": {
"providers": []
}
},
"scripts": {
"test": "vendor/bin/phpunit --colors=always"
},
Expand Down
4 changes: 3 additions & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
printerClass="NunoMaduro\Collision\Adapters\Phpunit\Printer"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<coverage>
<include>
Expand All @@ -18,7 +19,8 @@
</coverage>
<testsuites>
<testsuite name="Simple Squid Test Suite">
<directory>tests</directory>
<directory>tests/Fields</directory>
<directory>tests/Filters</directory>
</testsuite>
</testsuites>
<php>
Expand Down
75 changes: 30 additions & 45 deletions src/Enum.php
Original file line number Diff line number Diff line change
@@ -1,64 +1,49 @@
<?php
/**
* Copyright (c) 2019 Matthew Poulter. All rights reserved.
*/

namespace SimpleSquid\Nova\Fields\Enum;

use BenSampo\Enum\Rules\EnumValue;
use Illuminate\Contracts\Support\Arrayable;
use Laravel\Nova\Fields\Select;
use Laravel\Nova\Http\Requests\NovaRequest;

class Enum extends Select
{
/**
* Setup the Enum field with the Enum class.
*
* @param string $enumClass
*
* @return $this
*/
public function attachEnum($enumClass)
public function __construct($name, $attribute = null, $resolveCallback = null)
{
return $this->options($this->getEnumOptions($enumClass))
->rules('required', new EnumValue($enumClass, false))
->resolveUsing(
function ($enum) {
return $enum instanceof \BenSampo\Enum\Enum ? $enum->value : $enum;
}
)
->displayUsing(
function ($enum) {
return $enum instanceof \BenSampo\Enum\Enum ? $enum->description : $enum;
}
);
parent::__construct($name, $attribute, $resolveCallback);

$this->resolveUsing(
function ($value) {
return $value instanceof \BenSampo\Enum\Enum ? $value->value : $value;
}
);

$this->displayUsing(
function ($value) {
return $value instanceof \BenSampo\Enum\Enum ? $value->description : $value;
}
);

$this->fillUsing(
function (NovaRequest $request, $model, $attribute, $requestAttribute) {
if ($request->exists($requestAttribute)) {
$model->{$attribute} = $request[$requestAttribute];
}
}
);
}

/**
* Hydrate the given attribute on the model based on the incoming request.
*
* @param NovaRequest $request
* @param string $requestAttribute
* @param object $model
* @param string $attribute
*
* @return void
*/
protected function fillAttributeFromRequest(NovaRequest $request, $requestAttribute, $model, $attribute)
public function attach($class)
{
if ($request->exists($requestAttribute)) {
$model->{$attribute} = $request[$requestAttribute];
}
return $this->options($class::asSelectArray())
->rules('required', new EnumValue($class, false));
}

protected function getEnumOptions(string $enumClass): array
/**
* @deprecated deprecated since version 2.3
*/
public function attachEnum($class)
{
// Since laravel-enum v2.2.0, the method has been named 'asSelectArray'
if (in_array(Arrayable::class, class_implements($enumClass))) {
return $enumClass::asSelectArray();
}

return $enumClass::toSelectArray();
return $this->attach($class);
}
}
Loading

0 comments on commit ac53cad

Please sign in to comment.