From 193e58fdc37509e655068b6ff5b71a4459e33b68 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20M=2E?= Date: Wed, 27 Nov 2024 15:52:30 +0100 Subject: [PATCH] Use FormRequest By using FormRequest, it offers features like collection and fluent, and other methods. It also now calls `$this->validate()`, to make sure the value is validated. --- src/Forms/Concerns/WithAttributes.php | 57 ++++++++++++--------------- 1 file changed, 26 insertions(+), 31 deletions(-) diff --git a/src/Forms/Concerns/WithAttributes.php b/src/Forms/Concerns/WithAttributes.php index 238a1c7..1c36b6a 100644 --- a/src/Forms/Concerns/WithAttributes.php +++ b/src/Forms/Concerns/WithAttributes.php @@ -2,43 +2,34 @@ namespace Foxws\WireUse\Forms\Concerns; +use Illuminate\Foundation\Http\FormRequest; use Illuminate\Support\Collection; use Illuminate\Support\Fluent; trait WithAttributes { - protected function keys(): array - { - return array_keys($this->all()); - } - public function fill($values) { - $values = $this->callHook('beforeFill', $values); + if (method_exists($this, 'beforeFill')) { + $values = $this->beforeFill($values); + } return parent::fill($values); } public function get(string $key, mixed $default = null): mixed { - return $this->getPropertyValue($key) ?: $default; + return $this->request()->get($key, $default); } public function has(...$properties): bool { - return $this->toCollection() - ->has($properties); + return $this->request()->hasAny($properties); } - public function contains(string $property, mixed $args): bool + public function contains(string $key, mixed $value = null): bool { - $propertyValue = $this->get($property); - - if (is_array($propertyValue)) { - return in_array($args, $propertyValue); - } - - return $propertyValue === $args; + return $this->collect()->contains($key, $value); } public function is(string $property, mixed $args = null): bool @@ -53,16 +44,12 @@ public function isStrict(string $property, mixed $args = null): bool public function filled(...$properties): bool { - return $this->toCollection($properties) - ->filter() - ->isNotEmpty(); + return $this->request()->filled($properties); } public function blank(...$properties): bool { - return $this->toCollection($properties) - ->filter() - ->isEmpty(); + return ! $this->filled($properties); } public function clear(bool $submit = true): void @@ -76,17 +63,25 @@ public function clear(bool $submit = true): void } } - protected function toCollection(...$properties): Collection + protected function keys(): array + { + return $this->request()->keys(); + } + + protected function request(): FormRequest + { + $this->validate(); + + return (new FormRequest())->merge($this->all()); + } + + protected function collect(): Collection { - return $properties - ? new Collection($this->only(...$properties)) - : new Collection($this->all()); + return $this->request()->collect(); } - protected function toFluent(...$properties): Fluent + protected function fluent(): Fluent { - return $properties - ? new Fluent($this->only(...$properties)) - : new Fluent($this->all()); + return request()->fluent(); } }