Skip to content

Commit

Permalink
Use FormRequest
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
francoism90 authored Nov 27, 2024
1 parent c6fa0c0 commit 193e58f
Showing 1 changed file with 26 additions and 31 deletions.
57 changes: 26 additions & 31 deletions src/Forms/Concerns/WithAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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();
}
}

0 comments on commit 193e58f

Please sign in to comment.