Skip to content

Commit 193e58f

Browse files
authored
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.
1 parent c6fa0c0 commit 193e58f

File tree

1 file changed

+26
-31
lines changed

1 file changed

+26
-31
lines changed

src/Forms/Concerns/WithAttributes.php

+26-31
Original file line numberDiff line numberDiff line change
@@ -2,43 +2,34 @@
22

33
namespace Foxws\WireUse\Forms\Concerns;
44

5+
use Illuminate\Foundation\Http\FormRequest;
56
use Illuminate\Support\Collection;
67
use Illuminate\Support\Fluent;
78

89
trait WithAttributes
910
{
10-
protected function keys(): array
11-
{
12-
return array_keys($this->all());
13-
}
14-
1511
public function fill($values)
1612
{
17-
$values = $this->callHook('beforeFill', $values);
13+
if (method_exists($this, 'beforeFill')) {
14+
$values = $this->beforeFill($values);
15+
}
1816

1917
return parent::fill($values);
2018
}
2119

2220
public function get(string $key, mixed $default = null): mixed
2321
{
24-
return $this->getPropertyValue($key) ?: $default;
22+
return $this->request()->get($key, $default);
2523
}
2624

2725
public function has(...$properties): bool
2826
{
29-
return $this->toCollection()
30-
->has($properties);
27+
return $this->request()->hasAny($properties);
3128
}
3229

33-
public function contains(string $property, mixed $args): bool
30+
public function contains(string $key, mixed $value = null): bool
3431
{
35-
$propertyValue = $this->get($property);
36-
37-
if (is_array($propertyValue)) {
38-
return in_array($args, $propertyValue);
39-
}
40-
41-
return $propertyValue === $args;
32+
return $this->collect()->contains($key, $value);
4233
}
4334

4435
public function is(string $property, mixed $args = null): bool
@@ -53,16 +44,12 @@ public function isStrict(string $property, mixed $args = null): bool
5344

5445
public function filled(...$properties): bool
5546
{
56-
return $this->toCollection($properties)
57-
->filter()
58-
->isNotEmpty();
47+
return $this->request()->filled($properties);
5948
}
6049

6150
public function blank(...$properties): bool
6251
{
63-
return $this->toCollection($properties)
64-
->filter()
65-
->isEmpty();
52+
return ! $this->filled($properties);
6653
}
6754

6855
public function clear(bool $submit = true): void
@@ -76,17 +63,25 @@ public function clear(bool $submit = true): void
7663
}
7764
}
7865

79-
protected function toCollection(...$properties): Collection
66+
protected function keys(): array
67+
{
68+
return $this->request()->keys();
69+
}
70+
71+
protected function request(): FormRequest
72+
{
73+
$this->validate();
74+
75+
return (new FormRequest())->merge($this->all());
76+
}
77+
78+
protected function collect(): Collection
8079
{
81-
return $properties
82-
? new Collection($this->only(...$properties))
83-
: new Collection($this->all());
80+
return $this->request()->collect();
8481
}
8582

86-
protected function toFluent(...$properties): Fluent
83+
protected function fluent(): Fluent
8784
{
88-
return $properties
89-
? new Fluent($this->only(...$properties))
90-
: new Fluent($this->all());
85+
return request()->fluent();
9186
}
9287
}

0 commit comments

Comments
 (0)