Skip to content

Commit bc9b279

Browse files
[11.x] Introduce HasUniqueStringIds (#53280)
* introduce HasUniqueStringIds * formatting --------- Co-authored-by: Taylor Otwell <[email protected]>
1 parent bb16825 commit bc9b279

File tree

3 files changed

+107
-126
lines changed

3 files changed

+107
-126
lines changed

src/Illuminate/Database/Eloquent/Concerns/HasUlids.php

Lines changed: 5 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,10 @@
77

88
trait HasUlids
99
{
10-
/**
11-
* Initialize the trait.
12-
*
13-
* @return void
14-
*/
15-
public function initializeHasUlids()
16-
{
17-
$this->usesUniqueIds = true;
18-
}
10+
use HasUniqueStringIds;
1911

2012
/**
21-
* Get the columns that should receive a unique identifier.
22-
*
23-
* @return array
24-
*/
25-
public function uniqueIds()
26-
{
27-
return [$this->getKeyName()];
28-
}
29-
30-
/**
31-
* Generate a new ULID for the model.
13+
* Generate a new unique key for the model.
3214
*
3315
* @return string
3416
*/
@@ -38,53 +20,13 @@ public function newUniqueId()
3820
}
3921

4022
/**
41-
* Retrieve the model for a bound value.
23+
* Determine if given key is valid.
4224
*
43-
* @param \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Relations\Relation<*, *, *> $query
4425
* @param mixed $value
45-
* @param string|null $field
46-
* @return \Illuminate\Contracts\Database\Eloquent\Builder
47-
*
48-
* @throws \Illuminate\Database\Eloquent\ModelNotFoundException
49-
*/
50-
public function resolveRouteBindingQuery($query, $value, $field = null)
51-
{
52-
if ($field && in_array($field, $this->uniqueIds()) && ! Str::isUlid($value)) {
53-
throw (new ModelNotFoundException)->setModel(get_class($this), $value);
54-
}
55-
56-
if (! $field && in_array($this->getRouteKeyName(), $this->uniqueIds()) && ! Str::isUlid($value)) {
57-
throw (new ModelNotFoundException)->setModel(get_class($this), $value);
58-
}
59-
60-
return parent::resolveRouteBindingQuery($query, $value, $field);
61-
}
62-
63-
/**
64-
* Get the auto-incrementing key type.
65-
*
66-
* @return string
67-
*/
68-
public function getKeyType()
69-
{
70-
if (in_array($this->getKeyName(), $this->uniqueIds())) {
71-
return 'string';
72-
}
73-
74-
return $this->keyType;
75-
}
76-
77-
/**
78-
* Get the value indicating whether the IDs are incrementing.
79-
*
8026
* @return bool
8127
*/
82-
public function getIncrementing()
28+
protected function isValidUniqueId($value): bool
8329
{
84-
if (in_array($this->getKeyName(), $this->uniqueIds())) {
85-
return false;
86-
}
87-
88-
return $this->incrementing;
30+
return Str::isUlid($value);
8931
}
9032
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
<?php
2+
3+
namespace Illuminate\Database\Eloquent\Concerns;
4+
5+
use Illuminate\Database\Eloquent\ModelNotFoundException;
6+
use Illuminate\Support\Str;
7+
8+
trait HasUniqueStringIds
9+
{
10+
/**
11+
* Generate a new unique key for the model.
12+
*
13+
* @return mixed
14+
*/
15+
abstract public function newUniqueId();
16+
17+
/**
18+
* Determine if given key is valid.
19+
*
20+
* @param mixed $value
21+
* @return bool
22+
*/
23+
abstract protected function isValidUniqueId($value): bool;
24+
25+
/**
26+
* Initialize the trait.
27+
*
28+
* @return void
29+
*/
30+
public function initializeHasUniqueStringIds()
31+
{
32+
$this->usesUniqueIds = true;
33+
}
34+
35+
/**
36+
* Get the columns that should receive a unique identifier.
37+
*
38+
* @return array
39+
*/
40+
public function uniqueIds()
41+
{
42+
return [$this->getKeyName()];
43+
}
44+
45+
46+
/**
47+
* Retrieve the model for a bound value.
48+
*
49+
* @param \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Relations\Relation<*, *, *> $query
50+
* @param mixed $value
51+
* @param string|null $field
52+
* @return \Illuminate\Contracts\Database\Eloquent\Builder
53+
*
54+
* @throws \Illuminate\Database\Eloquent\ModelNotFoundException
55+
*/
56+
public function resolveRouteBindingQuery($query, $value, $field = null)
57+
{
58+
if ($field && in_array($field, $this->uniqueIds()) && ! $this->isValidUniqueId($value)) {
59+
throw (new ModelNotFoundException)->setModel(get_class($this), $value);
60+
}
61+
62+
if (! $field && in_array($this->getRouteKeyName(), $this->uniqueIds()) && ! $this->isValidUniqueId($value)) {
63+
throw (new ModelNotFoundException)->setModel(get_class($this), $value);
64+
}
65+
66+
return parent::resolveRouteBindingQuery($query, $value, $field);
67+
}
68+
69+
/**
70+
* Get the auto-incrementing key type.
71+
*
72+
* @return string
73+
*/
74+
public function getKeyType()
75+
{
76+
if (in_array($this->getKeyName(), $this->uniqueIds())) {
77+
return 'string';
78+
}
79+
80+
return $this->keyType;
81+
}
82+
83+
/**
84+
* Get the value indicating whether the IDs are incrementing.
85+
*
86+
* @return bool
87+
*/
88+
public function getIncrementing()
89+
{
90+
if (in_array($this->getKeyName(), $this->uniqueIds())) {
91+
return false;
92+
}
93+
94+
return $this->incrementing;
95+
}
96+
97+
}

src/Illuminate/Database/Eloquent/Concerns/HasUuids.php

Lines changed: 5 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,10 @@
77

88
trait HasUuids
99
{
10-
/**
11-
* Initialize the trait.
12-
*
13-
* @return void
14-
*/
15-
public function initializeHasUuids()
16-
{
17-
$this->usesUniqueIds = true;
18-
}
10+
use HasUniqueStringIds;
1911

2012
/**
21-
* Get the columns that should receive a unique identifier.
22-
*
23-
* @return array
24-
*/
25-
public function uniqueIds()
26-
{
27-
return [$this->getKeyName()];
28-
}
29-
30-
/**
31-
* Generate a new UUID for the model.
13+
* Generate a new unique key for the model.
3214
*
3315
* @return string
3416
*/
@@ -38,53 +20,13 @@ public function newUniqueId()
3820
}
3921

4022
/**
41-
* Retrieve the model for a bound value.
23+
* Determine if given key is valid.
4224
*
43-
* @param \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Relations\Relation<*, *, *> $query
4425
* @param mixed $value
45-
* @param string|null $field
46-
* @return \Illuminate\Contracts\Database\Eloquent\Builder
47-
*
48-
* @throws \Illuminate\Database\Eloquent\ModelNotFoundException
49-
*/
50-
public function resolveRouteBindingQuery($query, $value, $field = null)
51-
{
52-
if ($field && in_array($field, $this->uniqueIds()) && ! Str::isUuid($value)) {
53-
throw (new ModelNotFoundException)->setModel(get_class($this), $value);
54-
}
55-
56-
if (! $field && in_array($this->getRouteKeyName(), $this->uniqueIds()) && ! Str::isUuid($value)) {
57-
throw (new ModelNotFoundException)->setModel(get_class($this), $value);
58-
}
59-
60-
return parent::resolveRouteBindingQuery($query, $value, $field);
61-
}
62-
63-
/**
64-
* Get the auto-incrementing key type.
65-
*
66-
* @return string
67-
*/
68-
public function getKeyType()
69-
{
70-
if (in_array($this->getKeyName(), $this->uniqueIds())) {
71-
return 'string';
72-
}
73-
74-
return $this->keyType;
75-
}
76-
77-
/**
78-
* Get the value indicating whether the IDs are incrementing.
79-
*
8026
* @return bool
8127
*/
82-
public function getIncrementing()
28+
protected function isValidUniqueId($value): bool
8329
{
84-
if (in_array($this->getKeyName(), $this->uniqueIds())) {
85-
return false;
86-
}
87-
88-
return $this->incrementing;
30+
return Str::isUuid($value);
8931
}
9032
}

0 commit comments

Comments
 (0)