Skip to content

Commit

Permalink
[11.x] Introduce HasUniqueStringIds (#53280)
Browse files Browse the repository at this point in the history
* introduce HasUniqueStringIds

* formatting

---------

Co-authored-by: Taylor Otwell <taylor@laravel.com>
  • Loading branch information
cosmastech and taylorotwell authored Oct 29, 2024
1 parent bb16825 commit bc9b279
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 126 deletions.
68 changes: 5 additions & 63 deletions src/Illuminate/Database/Eloquent/Concerns/HasUlids.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,10 @@

trait HasUlids
{
/**
* Initialize the trait.
*
* @return void
*/
public function initializeHasUlids()
{
$this->usesUniqueIds = true;
}
use HasUniqueStringIds;

/**
* Get the columns that should receive a unique identifier.
*
* @return array
*/
public function uniqueIds()
{
return [$this->getKeyName()];
}

/**
* Generate a new ULID for the model.
* Generate a new unique key for the model.
*
* @return string
*/
Expand All @@ -38,53 +20,13 @@ public function newUniqueId()
}

/**
* Retrieve the model for a bound value.
* Determine if given key is valid.
*
* @param \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Relations\Relation<*, *, *> $query
* @param mixed $value
* @param string|null $field
* @return \Illuminate\Contracts\Database\Eloquent\Builder
*
* @throws \Illuminate\Database\Eloquent\ModelNotFoundException
*/
public function resolveRouteBindingQuery($query, $value, $field = null)
{
if ($field && in_array($field, $this->uniqueIds()) && ! Str::isUlid($value)) {
throw (new ModelNotFoundException)->setModel(get_class($this), $value);
}

if (! $field && in_array($this->getRouteKeyName(), $this->uniqueIds()) && ! Str::isUlid($value)) {
throw (new ModelNotFoundException)->setModel(get_class($this), $value);
}

return parent::resolveRouteBindingQuery($query, $value, $field);
}

/**
* Get the auto-incrementing key type.
*
* @return string
*/
public function getKeyType()
{
if (in_array($this->getKeyName(), $this->uniqueIds())) {
return 'string';
}

return $this->keyType;
}

/**
* Get the value indicating whether the IDs are incrementing.
*
* @return bool
*/
public function getIncrementing()
protected function isValidUniqueId($value): bool
{
if (in_array($this->getKeyName(), $this->uniqueIds())) {
return false;
}

return $this->incrementing;
return Str::isUlid($value);
}
}
97 changes: 97 additions & 0 deletions src/Illuminate/Database/Eloquent/Concerns/HasUniqueStringIds.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php

namespace Illuminate\Database\Eloquent\Concerns;

use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Support\Str;

trait HasUniqueStringIds
{
/**
* Generate a new unique key for the model.
*
* @return mixed
*/
abstract public function newUniqueId();

/**
* Determine if given key is valid.
*
* @param mixed $value
* @return bool
*/
abstract protected function isValidUniqueId($value): bool;

/**
* Initialize the trait.
*
* @return void
*/
public function initializeHasUniqueStringIds()
{
$this->usesUniqueIds = true;
}

/**
* Get the columns that should receive a unique identifier.
*
* @return array
*/
public function uniqueIds()
{
return [$this->getKeyName()];
}


/**
* Retrieve the model for a bound value.
*
* @param \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Relations\Relation<*, *, *> $query
* @param mixed $value
* @param string|null $field
* @return \Illuminate\Contracts\Database\Eloquent\Builder
*
* @throws \Illuminate\Database\Eloquent\ModelNotFoundException
*/
public function resolveRouteBindingQuery($query, $value, $field = null)
{
if ($field && in_array($field, $this->uniqueIds()) && ! $this->isValidUniqueId($value)) {
throw (new ModelNotFoundException)->setModel(get_class($this), $value);
}

if (! $field && in_array($this->getRouteKeyName(), $this->uniqueIds()) && ! $this->isValidUniqueId($value)) {
throw (new ModelNotFoundException)->setModel(get_class($this), $value);
}

return parent::resolveRouteBindingQuery($query, $value, $field);
}

/**
* Get the auto-incrementing key type.
*
* @return string
*/
public function getKeyType()
{
if (in_array($this->getKeyName(), $this->uniqueIds())) {
return 'string';
}

return $this->keyType;
}

/**
* Get the value indicating whether the IDs are incrementing.
*
* @return bool
*/
public function getIncrementing()
{
if (in_array($this->getKeyName(), $this->uniqueIds())) {
return false;
}

return $this->incrementing;
}

}
68 changes: 5 additions & 63 deletions src/Illuminate/Database/Eloquent/Concerns/HasUuids.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,10 @@

trait HasUuids
{
/**
* Initialize the trait.
*
* @return void
*/
public function initializeHasUuids()
{
$this->usesUniqueIds = true;
}
use HasUniqueStringIds;

/**
* Get the columns that should receive a unique identifier.
*
* @return array
*/
public function uniqueIds()
{
return [$this->getKeyName()];
}

/**
* Generate a new UUID for the model.
* Generate a new unique key for the model.
*
* @return string
*/
Expand All @@ -38,53 +20,13 @@ public function newUniqueId()
}

/**
* Retrieve the model for a bound value.
* Determine if given key is valid.
*
* @param \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Relations\Relation<*, *, *> $query
* @param mixed $value
* @param string|null $field
* @return \Illuminate\Contracts\Database\Eloquent\Builder
*
* @throws \Illuminate\Database\Eloquent\ModelNotFoundException
*/
public function resolveRouteBindingQuery($query, $value, $field = null)
{
if ($field && in_array($field, $this->uniqueIds()) && ! Str::isUuid($value)) {
throw (new ModelNotFoundException)->setModel(get_class($this), $value);
}

if (! $field && in_array($this->getRouteKeyName(), $this->uniqueIds()) && ! Str::isUuid($value)) {
throw (new ModelNotFoundException)->setModel(get_class($this), $value);
}

return parent::resolveRouteBindingQuery($query, $value, $field);
}

/**
* Get the auto-incrementing key type.
*
* @return string
*/
public function getKeyType()
{
if (in_array($this->getKeyName(), $this->uniqueIds())) {
return 'string';
}

return $this->keyType;
}

/**
* Get the value indicating whether the IDs are incrementing.
*
* @return bool
*/
public function getIncrementing()
protected function isValidUniqueId($value): bool
{
if (in_array($this->getKeyName(), $this->uniqueIds())) {
return false;
}

return $this->incrementing;
return Str::isUuid($value);
}
}

0 comments on commit bc9b279

Please sign in to comment.