-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #30 from simplesquid/2.3.x
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
Showing
35 changed files
with
1,551 additions
and
251 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
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 |
---|---|---|
@@ -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); | ||
} | ||
} |
Oops, something went wrong.