Skip to content
This repository has been archived by the owner on Feb 17, 2022. It is now read-only.

Commit

Permalink
Merge pull request #21 from DarkGhostHunter/master
Browse files Browse the repository at this point in the history
Minor rework with breaking changes
  • Loading branch information
DarkGhostHunter committed Apr 25, 2021
2 parents b3e791c + db4fd23 commit 4ac9a36
Show file tree
Hide file tree
Showing 10 changed files with 182 additions and 379 deletions.
38 changes: 27 additions & 11 deletions README.md
Expand Up @@ -64,7 +64,7 @@ This may come handy in situations when the user presses a wrong button into an R
```php
<?php

use \DarkGhostHunter\RutUtils\Rut;
use DarkGhostHunter\RutUtils\Rut;

$rut = Rut::make(request()->input('rut'));
```
Expand Down Expand Up @@ -362,9 +362,13 @@ If you plan to use the Number as an index, which may speed up queries to look fo

## RUT trait for Models

You can add the `HasRut` trait to a model that has a RUT in its columns. This is the recommended way to use RUT in your application models.
This package contains two traits: `HasRut` and `RoutesRut`.

> To use this trait, ensure your model saves the RUT Number and RUT Verification digit in separate columns. If that's not your case, use your own scopes and helpers.
> To use these traits, ensure your model saves the RUT Number and RUT Verification digit in separate columns.
### `HasRut`

This trait conveniently adds a RUT Scope to a model that has a RUT in its columns, and the `rut` property which returns a `Rut` instance.

```php
<?php
Expand All @@ -382,7 +386,7 @@ class User extends Authenticatable
}
```

With that, you will have access to convenient RUT queries:
With that, you will have access to convenient RUT queries shorthands:

* `findRut()`: Finds a record by the given RUT.
* `findManyRut()`: Finds many records by the given RUTs.
Expand All @@ -393,15 +397,13 @@ With that, you will have access to convenient RUT queries:

> These RUT queries work over the RUT Number for convenience, as the RUT Verification Digit should be verified on persistence.
### `rut` property

Additionally, this trait includes the `rut` property which is dynamically created from the RUT Number and RUT Verification Digit columns.
The `rut` property is dynamically created from the RUT Number and RUT Verification Digit columns, which uses a caster underneath.

```php
echo $user->rut; // "20490006-K"
```

### Configuring the RUT columns
#### Configuring the RUT columns

By convention, the trait uses `rut_num` and `rut_vd` as the default columns to retrieve and save the RUT Number and RUT Verification Digit, respectively.

Expand All @@ -419,10 +421,12 @@ class User extends Authenticatable
}
```

## Route Model Binding by RUT
### `RoutesRut`

You can use the `RoutesRut` trait to override the `resolveRouteBinding()` method of your Eloquent Model to look for its RUT if the field to identify is `rut`.

> Ensure you also add the `HasRut` trait, as it will use the added query scopes.
```php
use DarkGhostHunter\Lararut\HasRut;
use DarkGhostHunter\Lararut\RoutesRut;
Expand All @@ -437,10 +441,12 @@ class User extends Authenticatable
Then, you will be able to use Route Model Binding in your routes by issuing the `rut` as the name of the field to use to retrieve the model instance from the database.

```php
Route::get('usuario/{user:rut}', [UserController::class, 'show']);
Route::get('usuario/{user:rut}', function (User $user) {
return $user;
});
```

> If the RUT is invalid, the model won't be found, so there is no need to validate it.
> If the RUT is invalid, the model won't be found, so there is no need to validate the rut while route-binding.
## Rut Collection

Expand Down Expand Up @@ -487,6 +493,16 @@ class LogFailedAttempt
$user->notify(new ProbablyForgotHisPassword());
}
}

/**
* Creates many RUTs.
*
* @return array|\DarkGhostHunter\RutUtils\Rut
*/
public function generateRuts()
{
return rut()->generate(100);
}
}
```

Expand Down
46 changes: 46 additions & 0 deletions src/Casts/CastRut.php
@@ -0,0 +1,46 @@
<?php

namespace DarkGhostHunter\Lararut\Casts;

use DarkGhostHunter\RutUtils\Rut;
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;

class CastRut implements CastsAttributes
{
/**
* Transform the attribute from the underlying model values.
*
* @param \Illuminate\Database\Eloquent\Model $model
* @param string $key
* @param mixed $value
* @param array $attributes
*
* @return mixed
*/
public function get($model, string $key, $value, array $attributes)
{
return Rut::make($attributes[$model->getRutNumColumn()], $attributes[$model->getRutVdColumn()]);
}

/**
* Transform the attribute to its underlying model values.
*
* @param \Illuminate\Database\Eloquent\Model $model
* @param string $key
* @param mixed $value
* @param array $attributes
*
* @return mixed
*/
public function set($model, string $key, $value, array $attributes)
{
if (!$value instanceof Rut) {
$value = Rut::make($value);
}

$attributes[$model->getRutNumColumn()] = $value->num;
$attributes[$model->getRutVdColumn()] = $value->vd;

return $attributes;
}
}
92 changes: 0 additions & 92 deletions src/FindsByRut.php

This file was deleted.

25 changes: 12 additions & 13 deletions src/HasRut.php
Expand Up @@ -2,8 +2,6 @@

namespace DarkGhostHunter\Lararut;

use DarkGhostHunter\RutUtils\Rut;

/**
* @method \Illuminate\Database\Eloquent\Collection|static[]|static|null findRut(mixed $rut, array $columns = [])
* @method \Illuminate\Database\Eloquent\Collection|static[] findManyRut(\Illuminate\Contracts\Support\Arrayable|iterable|array $ruts, array $columns = [])
Expand All @@ -17,7 +15,18 @@
trait HasRut
{
/**
* Boot the soft deleting trait for a model.
* Initialize the HasRut trait.
*
* @return void
* @internal
*/
public function initializeHasRut(): void
{
$this->mergeCasts(['rut' => Casts\CastRut::class]);
}

/**
* Boot the HasRut trait.
*
* @return void
* @internal
Expand Down Expand Up @@ -56,14 +65,4 @@ public function getQualifiedRutNumColumn(): string
{
return $this->qualifyColumn($this->getRutNumColumn());
}

/**
* Returns the RUT of the user as a Rut instance.
*
* @return \DarkGhostHunter\RutUtils\Rut
*/
protected function getRutAttribute(): Rut
{
return Rut::make($this->getAttribute($this->getRutNumColumn()), $this->getAttribute($this->getRutVdColumn()));
}
}
34 changes: 0 additions & 34 deletions src/QueriesRut.php

This file was deleted.

5 changes: 5 additions & 0 deletions src/RoutesRut.php
Expand Up @@ -4,6 +4,7 @@

use DarkGhostHunter\RutUtils\Exceptions\InvalidRutException;
use Illuminate\Database\Eloquent\Model;
use RuntimeException;

trait RoutesRut
{
Expand All @@ -18,6 +19,10 @@ trait RoutesRut
public function resolveRouteBinding($value, $field = null): ?Model
{
if ($field === 'rut') {
if (!in_array(HasRut::class, class_uses_recursive($this), true)) {
throw new RuntimeException('The model ' . get_class($this) . ' must use the `HasRut` trait.');
}

try {
return $this->findRut($value);
} catch (InvalidRutException $e) {
Expand Down

0 comments on commit 4ac9a36

Please sign in to comment.