-
Notifications
You must be signed in to change notification settings - Fork 11k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
48 changed files
with
692 additions
and
211 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
src/Illuminate/Database/Eloquent/Concerns/HasUniqueStringIds.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
<?php | ||
|
||
namespace Illuminate\Database\Eloquent\Concerns; | ||
|
||
use Illuminate\Database\Eloquent\ModelNotFoundException; | ||
|
||
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.