Skip to content

Commit 640d6bb

Browse files
authored
Merge pull request #42 from hypervel/feature/new-has-uuids-trait
improve: add new `HasUuids` and `HasUlids` traits
2 parents c94802d + ae68519 commit 640d6bb

File tree

2 files changed

+124
-0
lines changed

2 files changed

+124
-0
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Hypervel\Database\Eloquent\Concerns;
6+
7+
use Hyperf\Stringable\Str;
8+
9+
trait HasUlids
10+
{
11+
/**
12+
* Add a unique identifier to the model before it is created.
13+
*/
14+
public function creating(): void
15+
{
16+
foreach ($this->uniqueIds() as $column) {
17+
if (empty($model->{$column})) {
18+
$this->{$column} = $this->newUniqueId();
19+
}
20+
}
21+
}
22+
23+
/**
24+
* Generate a new UUID for the model.
25+
*/
26+
public function newUniqueId(): string
27+
{
28+
return strtolower((string) Str::ulid());
29+
}
30+
31+
/**
32+
* Get the columns that should receive a unique identifier.
33+
*/
34+
public function uniqueIds(): array
35+
{
36+
return [$this->getKeyName()];
37+
}
38+
39+
/**
40+
* Get the auto-incrementing key type.
41+
*/
42+
public function getKeyType(): string
43+
{
44+
if (in_array($this->getKeyName(), $this->uniqueIds())) {
45+
return 'string';
46+
}
47+
48+
return $this->keyType;
49+
}
50+
51+
/**
52+
* Get the value indicating whether the IDs are incrementing.
53+
*/
54+
public function getIncrementing(): bool
55+
{
56+
if (in_array($this->getKeyName(), $this->uniqueIds())) {
57+
return false;
58+
}
59+
60+
return $this->incrementing;
61+
}
62+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Hypervel\Database\Eloquent\Concerns;
6+
7+
use Hyperf\Stringable\Str;
8+
9+
trait HasUuids
10+
{
11+
/**
12+
* Add a unique identifier to the model before it is created.
13+
*/
14+
public function creating(): void
15+
{
16+
foreach ($this->uniqueIds() as $column) {
17+
if (empty($model->{$column})) {
18+
$this->{$column} = $this->newUniqueId();
19+
}
20+
}
21+
}
22+
23+
/**
24+
* Generate a new UUID for the model.
25+
*/
26+
public function newUniqueId(): string
27+
{
28+
return (string) Str::orderedUuid();
29+
}
30+
31+
/**
32+
* Get the columns that should receive a unique identifier.
33+
*/
34+
public function uniqueIds(): array
35+
{
36+
return [$this->getKeyName()];
37+
}
38+
39+
/**
40+
* Get the auto-incrementing key type.
41+
*/
42+
public function getKeyType(): string
43+
{
44+
if (in_array($this->getKeyName(), $this->uniqueIds())) {
45+
return 'string';
46+
}
47+
48+
return $this->keyType;
49+
}
50+
51+
/**
52+
* Get the value indicating whether the IDs are incrementing.
53+
*/
54+
public function getIncrementing(): bool
55+
{
56+
if (in_array($this->getKeyName(), $this->uniqueIds())) {
57+
return false;
58+
}
59+
60+
return $this->incrementing;
61+
}
62+
}

0 commit comments

Comments
 (0)