diff --git a/composer.json b/composer.json index 8de3c46..ca3b2d4 100644 --- a/composer.json +++ b/composer.json @@ -20,7 +20,7 @@ ], "require": { "php": "^8.0", - "laravel/framework": "^10.0", + "laravel/framework": "^10.0|^11.0", "laravel-enso/enums": "^2.0", "laravel-enso/helpers": "^3.0" }, @@ -34,7 +34,7 @@ "providers": [ "LaravelEnso\\Forms\\AppServiceProvider" ], - "aliases": {} + "aliases": [] } } } \ No newline at end of file diff --git a/src/Exceptions/Template.php b/src/Exceptions/Template.php index 2ca0754..72f6e08 100644 --- a/src/Exceptions/Template.php +++ b/src/Exceptions/Template.php @@ -173,7 +173,8 @@ public static function missingInputContent($field) public static function invalidSelectOptions($field) { return new static(__( - '"options" meta parameter for field ":field" must be an array a collection or an Enum', + '"options" meta parameter for field ":field" must be an array a ' + .'collection or an Enum that implements the Select Contract ', ['field' => $field] )); } diff --git a/src/Services/Validators/Meta.php b/src/Services/Validators/Meta.php index bb81c7d..90f1bf4 100644 --- a/src/Services/Validators/Meta.php +++ b/src/Services/Validators/Meta.php @@ -3,8 +3,7 @@ namespace LaravelEnso\Forms\Services\Validators; use Illuminate\Support\Collection; -use LaravelEnso\Enums\Services\Enum; -use LaravelEnso\Enums\Traits\Select; +use LaravelEnso\Enums\Contracts\Select; use LaravelEnso\Forms\Attributes\Meta as Attributes; use LaravelEnso\Forms\Exceptions\Template; use LaravelEnso\Helpers\Services\Obj; @@ -100,23 +99,14 @@ private function selectMetaParameterMissing(): bool || (! $this->meta->has('options') && ! $this->meta->has('source')); } - private function invalidOptions($options) + private function invalidOptions($options): bool { - if (! $options) { - return false; - } - - $isLegacyEnum = (class_exists($options) - && ! enum_exists($options) - && new $options() instanceof Enum); - - $validEnum = $isLegacyEnum || in_array( - Select::class, - array_keys((new ReflectionEnum($options))->getTraits()) - ); - - return ! is_array($options) - && ! (is_string($options) && $validEnum) - && ! method_exists($options, 'toArray'); + return $options + && ! is_array($options) + && ( + enum_exists($options) && ! (new ReflectionEnum($options)) + ->implementsInterface(Select::class) + || ! class_exists($options) && ! method_exists($options, 'toArray') + ); } } diff --git a/tests/unit/Services/BuilderTest.php b/tests/unit/Services/BuilderTest.php index 221dfa0..86c62da 100644 --- a/tests/unit/Services/BuilderTest.php +++ b/tests/unit/Services/BuilderTest.php @@ -6,8 +6,9 @@ use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Collection; use Illuminate\Support\Facades\Route; +use LaravelEnso\Enums\Contracts\Select; use LaravelEnso\Enums\Services\Enum; -use LaravelEnso\Enums\Traits\Select; +use LaravelEnso\Enums\Traits\Select as Options; use LaravelEnso\Forms\Services\Builder; use LaravelEnso\Helpers\Services\Obj; use Mockery; @@ -191,9 +192,9 @@ class FormTestModel extends Model public $test_field; } -enum FormTestEnum: int +enum FormTestEnum: int implements Select { - use Select; + use Options; public const Active = 1; public const InActive = 0;