Skip to content

Commit

Permalink
Code refactoring (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
francoism90 authored Oct 31, 2024
1 parent be4a382 commit c811a34
Show file tree
Hide file tree
Showing 15 changed files with 123 additions and 447 deletions.
12 changes: 8 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
{
"name": "foxws/wireuse",
"description": "Collection of essential Livewire utilities",
"description": "Collection of useful Livewire utilities",
"keywords": [
"foxws",
"wireuse",
"laravel",
"livewire",
"wireuse"
"utilities",
"traits",
"helpers"
],
"homepage": "https://github.com/foxws/wireuse",
"license": "MIT",
Expand All @@ -18,13 +21,14 @@
],
"require": {
"php": "^8.2",
"illuminate/cache": "^10.0|^11.0",
"illuminate/contracts": "^10.0|^11.0",
"illuminate/support": "^10.0|^11.0",
"illuminate/view": "^10.0|^11.0",
"laravel/scout": "^10.0|^11.0",
"livewire/livewire": "^3.4",
"spatie/laravel-package-tools": "^1.16.5",
"artesaos/seotools": "^1.3"
"artesaos/seotools": "^1.3",
"spatie/laravel-package-tools": "^1.16.5"
},
"require-dev": {
"larastan/larastan": "^2.9",
Expand Down
15 changes: 8 additions & 7 deletions src/Auth/Concerns/WithAuthentication.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,24 @@

namespace Foxws\WireUse\Auth\Concerns;

use Illuminate\Foundation\Auth\User;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Support\Facades\Auth;

trait WithAuthentication
{
protected function isAuthenticated(): bool
{
return auth()->check();
return Auth::check();
}

protected function getAuthId(): int|string|null
{
return auth()->id();
return Auth::id();
}

protected function getAuthModel(): ?User
protected function getAuthModel(): ?Authenticatable
{
return auth()->user();
return Auth::getUser();
}

protected function getAuthKey(): int|string|null
Expand All @@ -28,11 +29,11 @@ protected function getAuthKey(): int|string|null

protected function can(string $ability, mixed $arguments = []): bool
{
return $this->getAuthModel()?->can($ability, $arguments);
return Auth::user()?->can($ability, $arguments);
}

protected function cannot(string $ability, mixed $arguments = []): bool
{
return $this->getAuthModel()?->cannot($ability, $arguments);
return Auth::user()?->cannot($ability, $arguments);
}
}
92 changes: 92 additions & 0 deletions src/Forms/Concerns/WithAttributes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php

namespace Foxws\WireUse\Forms\Concerns;

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);

return parent::fill($values);
}

public function get(string $key, mixed $default = null): mixed
{
return $this->getPropertyValue($key) ?: $default;
}

public function has(...$properties): bool
{
return $this->toCollection()
->has($properties);
}

public function contains(string $property, mixed $args): bool
{
$propertyValue = $this->get($property);

if (is_array($propertyValue)) {
return in_array($args, $propertyValue);
}

return $propertyValue === $args;
}

public function is(string $property, mixed $args = null): bool
{
return $this->get($property) == $args;
}

public function isStrict(string $property, mixed $args = null): bool
{
return $this->get($property) === $args;
}

public function filled(...$properties): bool
{
return $this->toCollection($properties)
->filter()
->isNotEmpty();
}

public function blank(...$properties): bool
{
return $this->toCollection($properties)
->filter()
->isEmpty();
}

public function clear(bool $submit = true): void
{
$properties = $this->keys();

$this->reset($properties);

if ($submit && method_exists($this, 'submit')) {
$this->submit();
}
}

protected function toCollection(...$properties): Collection
{
return $properties
? new Collection($this->only(...$properties))
: new Collection($this->all());
}

protected function toFluent(...$properties): Fluent
{
return $properties
? new Fluent($this->only(...$properties))
: new Fluent($this->all());
}
}
87 changes: 2 additions & 85 deletions src/Forms/Support/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@

use Foxws\WireUse\Auth\Concerns\WithAuthorization;
use Foxws\WireUse\Exceptions\RateLimitedException;
use Foxws\WireUse\Forms\Concerns\WithAttributes;
use Foxws\WireUse\Forms\Concerns\WithSession;
use Foxws\WireUse\Forms\Concerns\WithThrottle;
use Foxws\WireUse\Forms\Concerns\WithValidation;
use Foxws\WireUse\Support\Concerns\WithHooks;
use Foxws\WireUse\Views\Concerns\WithHash;
use Illuminate\Support\Collection;
use Illuminate\Support\Fluent;
use Livewire\Form as BaseForm;

abstract class Form extends BaseForm
{
use WithAttributes;
use WithAuthorization;
use WithHash;
use WithHooks;
Expand Down Expand Up @@ -49,87 +49,4 @@ protected function handle(): void
{
//
}

protected function keys(): array
{
return array_keys($this->all());
}

public function fill($values)
{
$values = $this->callHook('beforeFill', $values);

return parent::fill($values);
}

public function get(string $key, mixed $default = null): mixed
{
return $this->getPropertyValue($key) ?: $default;
}

public function has(...$properties): bool
{
return $this->toCollection()
->has($properties);
}

public function contains(string $property, mixed $args): bool
{
$propertyValue = $this->get($property);

if (is_array($propertyValue)) {
return in_array($args, $propertyValue);
}

return $propertyValue === $args;
}

public function is(string $property, mixed $args = null): bool
{
return $this->get($property) == $args;
}

public function isStrict(string $property, mixed $args = null): bool
{
return $this->get($property) === $args;
}

public function filled(...$properties): bool
{
return $this->toCollection($properties)
->filter()
->isNotEmpty();
}

public function blank(...$properties): bool
{
return $this->toCollection($properties)
->filter()
->isEmpty();
}

public function clear(bool $submit = true): void
{
$properties = $this->keys();

$this->reset($properties);

if ($submit && method_exists($this, 'submit')) {
$this->submit();
}
}

protected function toCollection(...$properties): Collection
{
return $properties
? new Collection($this->only(...$properties))
: new Collection($this->all());
}

protected function toFluent(...$properties): Fluent
{
return $properties
? new Fluent($this->only(...$properties))
: new Fluent($this->all());
}
}
Loading

0 comments on commit c811a34

Please sign in to comment.