Skip to content

[Uid] Add NilUlid #41802

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 27, 2021
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
5 changes: 5 additions & 0 deletions src/Symfony/Component/Uid/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

5.4
---

* Add `NilUlid`

5.3
---

Expand Down
20 changes: 20 additions & 0 deletions src/Symfony/Component/Uid/NilUlid.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Uid;

class NilUlid extends Ulid
{
public function __construct()
{
$this->uid = parent::NIL;
}
}
19 changes: 19 additions & 0 deletions src/Symfony/Component/Uid/Tests/UlidTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\Uid\Tests;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Uid\NilUlid;
use Symfony\Component\Uid\Tests\Fixtures\CustomUlid;
use Symfony\Component\Uid\Ulid;
use Symfony\Component\Uid\UuidV4;
Expand Down Expand Up @@ -242,4 +243,22 @@ public function testFromStringBase58Padding()
{
$this->assertInstanceOf(Ulid::class, Ulid::fromString('111111111u9QRyVM94rdmZ'));
}

/**
* @testWith ["00000000-0000-0000-0000-000000000000"]
* ["1111111111111111111111"]
* ["00000000000000000000000000"]
*/
public function testNilUlid(string $ulid)
{
$ulid = Ulid::fromString($ulid);

$this->assertInstanceOf(NilUlid::class, $ulid);
$this->assertSame('00000000000000000000000000', (string) $ulid);
}

public function testNewNilUlid()
{
$this->assertSame('00000000000000000000000000', (string) new NilUlid());
}
}
10 changes: 9 additions & 1 deletion src/Symfony/Component/Uid/Ulid.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
*/
class Ulid extends AbstractUid
{
private const NIL = '00000000000000000000000000';
protected const NIL = '00000000000000000000000000';

private static $time = '';
private static $rand = [];
Expand Down Expand Up @@ -71,6 +71,10 @@ public static function fromString(string $ulid): parent
}

if (16 !== \strlen($ulid)) {
if (self::NIL === $ulid) {
return new NilUlid();
}

return new static($ulid);
}

Expand All @@ -85,6 +89,10 @@ public static function fromString(string $ulid): parent
base_convert(substr($ulid, 27, 5), 16, 32)
);

if (self::NIL === $ulid) {
return new NilUlid();
}

$u = new static(self::NIL);
$u->uid = strtr($ulid, 'abcdefghijklmnopqrstuv', 'ABCDEFGHJKMNPQRSTVWXYZ');

Expand Down