8000 [Uid] Add NilUlid · symfony/symfony@f1c0cce · GitHub
[go: up one dir, main page]

Skip to content

Commit f1c0cce

Browse files
committed
[Uid] Add NilUlid
1 parent d679ac5 commit f1c0cce

File tree

4 files changed

+53
-1
lines changed

4 files changed

+53
-1
lines changed

src/Symfony/Component/Uid/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
5.4
5+
---
6+
7+
* Add `NilUlid`
8+
49
5.3
510
---
611

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Uid;
13+
14+
class NilUlid extends Ulid
15+
{
16+
public function __construct()
17+
{
18+
$this->uid = parent::NIL;
19+
}
20+
}

src/Symfony/Component/Uid/Tests/UlidTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Uid\Tests;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Uid\NilUlid;
1516
use Symfony\Component\Uid\Tests\Fixtures\CustomUlid;
1617
use Symfony\Component\Uid\Ulid;
1718
use Symfony\Component\Uid\UuidV4;
@@ -242,4 +243,22 @@ public function testFromStringBase58Padding()
242243
{
243244
$this->assertInstanceOf(Ulid::class, Ulid::fromString('111111111u9QRyVM94rdmZ'));
244245
}
246+
247+
/**
248+
* @testWith ["00000000-0000-0000-0000-000000000000"]
249+
* ["1111111111111111111111"]
250+
* ["00000000000000000000000000"]
251+
*/
252+
public function testNilUlid(string $ulid)
253+
{
254+
$ulid = Ulid::fromString($ulid);
255+
256+
$this->assertInstanceOf(NilUlid::class, $ulid);
257+
$this->assertSame('00000000000000000000000000', (string) $ulid);
258+
}
259+
260+
public function testNewNilUlid()
261+
{
262+
$this->assertSame('00000000000000000000000000', (string) new NilUlid());
263+
}
245264
}

src/Symfony/Component/Uid/Ulid.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
*/
2121
class Ulid extends AbstractUid
2222
{
23-
private const NIL = '00000000000000000000000000';
23+
protected const NIL = '00000000000000000000000000';
2424

2525
private static $time = '';
2626
private static $rand = [];
@@ -71,6 +71,10 @@ public static function fromString(string $ulid): parent
7171
}
7272

7373
if (16 !== \strlen($ulid)) {
74+
if (self::NIL === $ulid) {
75+
return new NilUlid();
76+
}
77+
7478
return new static($ulid);
7579
}
7680

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

92+
if (self::NIL === $ulid) {
93+
return new NilUlid();
94+
}
95+
8896
$u = new static(self::NIL);
8997
$u->uid = strtr($ulid, 'abcdefghijklmnopqrstuv', 'ABCDEFGHJKMNPQRSTVWXYZ');
9098

0 commit comments

Comments
 (0)
0