Skip to content

Commit

Permalink
UUID v7
Browse files Browse the repository at this point in the history
  • Loading branch information
mabar committed Sep 13, 2022
1 parent 0f3d36f commit 8524508
Show file tree
Hide file tree
Showing 10 changed files with 23 additions and 20 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
"orisai/utils": "^1.0.0",
"symfony/console": "^6.0.0",
"symfony/filesystem": "^6.0.0",
"symfony/uid": "^6.0.0",
"symfony/uid": "^6.2.0",
"tracy/tracy": "^2.8.0",
"webmozart/glob": "^4.3.0"
},
Expand Down
3 changes: 3 additions & 0 deletions src/Admin/Presenter/@layout.latte
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,6 @@
</p>
{/ifset}
{/define}

{$var|urlUid}
{$var|urlUuid}
4 changes: 2 additions & 2 deletions src/Email/DB/Email.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use DateTimeImmutable;
use Nextras\Orm\Entity\Entity;
use OriCMF\User\DB\User;
use Symfony\Component\Uid\Ulid;
use Symfony\Component\Uid\UuidV7;

/**
* @property-read string $id {primary}
Expand All @@ -26,7 +26,7 @@ public function __construct(string $emailAddress, string $type, User $user)
{
parent::__construct();

$this->setReadOnlyValue('id', (new Ulid())->toRfc4122());
$this->setReadOnlyValue('id', (new UuidV7())->toRfc4122());
$this->emailAddress = $emailAddress;
$this->type = $type;
$this->setReadOnlyValue('user', $user);
Expand Down
4 changes: 2 additions & 2 deletions src/Password/DB/Password.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use DateTimeImmutable;
use Nextras\Orm\Entity\Entity;
use OriCMF\User\DB\User;
use Symfony\Component\Uid\Ulid;
use Symfony\Component\Uid\UuidV7;

/**
* @property-read string $id {primary}
Expand All @@ -20,7 +20,7 @@ public function __construct(string $encodedPassword, User $user)
{
parent::__construct();

$this->setReadOnlyValue('id', (new Ulid())->toRfc4122());
$this->setReadOnlyValue('id', (new UuidV7())->toRfc4122());
$this->encodedPassword = $encodedPassword;
$this->setReadOnlyValue('user', $user);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Role/DB/Role.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use DateTimeImmutable;
use Nextras\Orm\Entity\Entity;
use OriCMF\ORM\Wrapper\JsonWrapper;
use Symfony\Component\Uid\Ulid;
use Symfony\Component\Uid\UuidV7;

/**
* @uses JsonWrapper
Expand All @@ -22,7 +22,7 @@ public function __construct(string $name, bool $isImmutable = false)
{
parent::__construct();

$this->setReadOnlyValue('id', (new Ulid())->toRfc4122());
$this->setReadOnlyValue('id', (new UuidV7())->toRfc4122());
$this->name = $name;
$this->setReadOnlyValue('isImmutable', $isImmutable);
$this->privileges = [];
Expand Down
10 changes: 5 additions & 5 deletions src/UI/IDs/UrlUlid.php → src/UI/IDs/UrlUid.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@

use InvalidArgumentException;
use Nette\Application\BadRequestException;
use Symfony\Component\Uid\Ulid;
use Symfony\Component\Uid\UuidV7;

/**
* Convert between url-friendly (base58) and storage-friendly (rfc4122) version of ulid
*/
final class UrlUlid
final class UrlUid
{

private function __construct(private readonly Ulid $ulid)
private function __construct(private readonly UuidV7 $ulid)
{
}

Expand All @@ -22,15 +22,15 @@ private function __construct(private readonly Ulid $ulid)
public static function fromUri(string $parameter): self
{
try {
return new self(Ulid::fromBase58($parameter));
return new self(UuidV7::fromBase58($parameter));
} catch (InvalidArgumentException) {
throw new BadRequestException();
}
}

public static function fromStorage(string $parameter): self
{
return new self(Ulid::fromRfc4122($parameter));
return new self(UuidV7::fromRfc4122($parameter));
}

public function toUri(): string
Expand Down
2 changes: 1 addition & 1 deletion src/UI/Template/DI/UITemplateExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public static function prepareTemplate(

public static function installExtension(Engine $engine): void
{
$engine->addFilter('urlUlid', UIFilters::urlUlid(...));
$engine->addFilter('urlUid', UIFilters::urlUid(...));
$engine->onCompile[] = static function (Engine $engine): void {
UIMacros::install($engine->getCompiler());
};
Expand Down
6 changes: 3 additions & 3 deletions src/UI/Template/UIFilters.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@

namespace OriCMF\UI\Template;

use Symfony\Component\Uid\Ulid;
use Symfony\Component\Uid\UuidV7;

/**
* @internal
*/
final class UIFilters
{

public static function urlUlid(string $ulid): string
public static function urlUid(string $ulid): string
{
return Ulid::fromRfc4122($ulid)->toBase58();
return UuidV7::fromRfc4122($ulid)->toBase58();
}

}
4 changes: 2 additions & 2 deletions src/User/DB/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use OriCMF\Email\DB\Email;
use OriCMF\ORM\Wrapper\BackedEnumWrapper;
use OriCMF\Role\DB\Role;
use Symfony\Component\Uid\Ulid;
use Symfony\Component\Uid\UuidV7;
use function random_int;

/**
Expand All @@ -35,7 +35,7 @@ final class User extends Entity
public function __construct(string $fullName, string|null $type = self::TypeReal)
{
parent::__construct();
$this->setReadOnlyValue('id', (new Ulid())->toRfc4122());
$this->setReadOnlyValue('id', (new UuidV7())->toRfc4122());

$this->fullName = $fullName;
$this->userName = Strings::webalize("$fullName-" . random_int(100, 9_999));
Expand Down
4 changes: 2 additions & 2 deletions stubs/intellij-latte-pro.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
</tag>
</tags>
<filters>
<!-- ULID -->
<filter name="urlUlid" description="Convert UUID from RFC 4122 (UUID-like) format to more URL-friendly base58"/>
<!-- UID -->
<filter name="urlUid" description="Convert UUID from RFC 4122 (UUID-like) format to more URL-friendly base58"/>
</filters>
</latte>

0 comments on commit 8524508

Please sign in to comment.