Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 1c598e6

Browse filesBrowse files
committed
[Uid] Add NilUlid
1 parent 2fb272e commit 1c598e6
Copy full SHA for 1c598e6

File tree

4 files changed

+58
-2
lines changed
Filter options

4 files changed

+58
-2
lines changed

‎src/Symfony/Component/Uid/CHANGELOG.md

Copy file name to clipboardExpand all lines: src/Symfony/Component/Uid/CHANGELOG.md
+5Lines 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

‎src/Symfony/Component/Uid/NilUlid.php

Copy file name to clipboard
+20Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Uid/Tests/UlidTest.php
+23Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,12 @@
1212
namespace Symfony\Component\Uid\Tests;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Uid\NilUlid;
16+
use Symfony\Component\Uid\NilUuid;
1517
use Symfony\Component\Uid\Tests\Fixtures\CustomUlid;
1618
use Symfony\Component\Uid\Ulid;
19+
use Symfony\Component\Uid\Uuid;
20+
use Symfony\Component\Uid\UuidV1;
1721
use Symfony\Component\Uid\UuidV4;
1822

1923
class UlidTest extends TestCase
@@ -237,4 +241,23 @@ public function testFromStringOnExtendedClassReturnsStatic()
237241
{
238242
$this->assertInstanceOf(CustomUlid::class, CustomUlid::fromString((new CustomUlid())->toBinary()));
239243
}
244+
245+
246+
/**
247+
* @testWith ["00000000-0000-0000-0000-000000000000"]
248+
* ["1111111111111111111111"]
249+
* ["00000000000000000000000000"]
250+
*/
251+
public function testNilUlid(string $ulid)
252+
{
253+
$ulid = Ulid::fromString($ulid);
254+
255+
$this->assertInstanceOf(NilUlid::class, $ulid);
256+
$this->assertSame('00000000000000000000000000', (string) $ulid);
257+
}
258+
259+
public function testNewNilUlid()
260+
{
261+
$this->assertSame('00000000000000000000000000', (string) new NilUlid());
262+
}
240263
}

‎src/Symfony/Component/Uid/Ulid.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Uid/Ulid.php
+10-2Lines changed: 10 additions & 2 deletions
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 = [];
@@ -67,10 +67,14 @@ public static function fromString(string $ulid): parent
6767
if (36 === \strlen($ulid) && Uuid::isValid($ulid)) {
6868
$ulid = (new Uuid($ulid))->toBinary();
6969
} elseif (22 === \strlen($ulid) && 22 === strspn($ulid, BinaryUtil::BASE58[''])) {
70-
$ulid = BinaryUtil::fromBase($ulid, BinaryUtil::BASE58);
70+
$ulid = str_pad(BinaryUtil::fromBase($ulid, BinaryUtil::BASE58), 16, "\0", \STR_PAD_LEFT);
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)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.