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 fa0e5a0

Browse filesBrowse files
committed
feature #41802 [Uid] Add NilUlid (fancyweb)
This PR was merged into the 5.4 branch. Discussion ---------- [Uid] Add NilUlid | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | - | License | MIT | Doc PR | - `NilUlid` is not really official but I think it would be useful. The goal is to be able to do `new NilUlid();` instead of `new Ulid('00000000000000000000000000');` and therefore to detect it more easily (like for the `NilUuid`). Currently I'm creating the class manually. My use case is using it as a placeholder. Commits ------- f1c0cce [Uid] Add NilUlid
2 parents 0a5d30d + f1c0cce commit fa0e5a0
Copy full SHA for fa0e5a0

File tree

Expand file treeCollapse file tree

4 files changed

+53
-1
lines changed
Filter options
Expand file treeCollapse file tree

4 files changed

+53
-1
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
+19Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Uid/Ulid.php
+9-1Lines 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)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.