Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions src/core/src/Database/Eloquent/Concerns/HasUlids.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

declare(strict_types=1);

namespace Hypervel\Database\Eloquent\Concerns;

use Hyperf\Stringable\Str;

trait HasUlids
{
/**
* Add a unique identifier to the model before it is created.
*/
public function creating(): void
{
foreach ($this->uniqueIds() as $column) {
if (empty($model->{$column})) {
$this->{$column} = $this->newUniqueId();
}
}
}

/**
* Generate a new UUID for the model.
*/
public function newUniqueId(): string
{
return strtolower((string) Str::ulid());
}

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

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

return $this->keyType;
}

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

return $this->incrementing;
}
}
62 changes: 62 additions & 0 deletions src/core/src/Database/Eloquent/Concerns/HasUuids.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

declare(strict_types=1);

namespace Hypervel\Database\Eloquent\Concerns;

use Hyperf\Stringable\Str;

trait HasUuids
{
/**
* Add a unique identifier to the model before it is created.
*/
public function creating(): void
{
foreach ($this->uniqueIds() as $column) {
if (empty($model->{$column})) {
$this->{$column} = $this->newUniqueId();
}
}
}

/**
* Generate a new UUID for the model.
*/
public function newUniqueId(): string
{
return (string) Str::orderedUuid();
}

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

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

return $this->keyType;
}

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

return $this->incrementing;
}
}
2 changes: 1 addition & 1 deletion src/foundation/src/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class Application extends Container implements ApplicationContract
*
* @var string
*/
public const VERSION = '0.1.5';
public const VERSION = '0.1.6';

/**
* The base path for the Hypervel installation.
Expand Down