Skip to content
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
7 changes: 6 additions & 1 deletion src/Symfony/Component/Uid/AbstractUid.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
/**
* @author Nicolas Grekas <p@tchwork.com>
*/
abstract class AbstractUid implements \JsonSerializable, \Stringable
abstract class AbstractUid implements \JsonSerializable, \Stringable, HashableInterface
{
/**
* The identifier in its canonic representation.
Expand Down Expand Up @@ -159,6 +159,11 @@ public function equals(mixed $other): bool
return $this->uid === $other->uid;
}

public function hash(): string
{
return $this->uid;
}

public function compare(self $other): int
{
return (\strlen($this->uid) - \strlen($other->uid)) ?: ($this->uid <=> $other->uid);
Expand Down
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
=========

7.2
---

* Make `AbstractUid` implement `Ds\Hashable` if available

7.1
---

Expand Down
32 changes: 32 additions & 0 deletions src/Symfony/Component/Uid/HashableInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?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;

if (interface_exists(\Ds\Hashable::class)) {
/**
* @internal
*/
interface HashableInterface extends \Ds\Hashable
{
public function hash(): string;
}
} else {
/**
* @internal
*/
interface HashableInterface
{
public function equals(mixed $other): bool;

public function hash(): string;
}
}
23 changes: 23 additions & 0 deletions src/Symfony/Component/Uid/Tests/UuidTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,29 @@ public static function provideInvalidEqualType(): iterable
yield [new \stdClass()];
}

public function testHashable()
{
$uuid1 = new UuidV4(self::A_UUID_V4);
$uuid2 = new UuidV4(self::A_UUID_V4);

$this->assertSame($uuid1->hash(), $uuid2->hash());
}

/** @requires extension ds */
public function testDsCompatibility()
{
$uuid1 = new UuidV4(self::A_UUID_V4);
$uuid2 = new UuidV4(self::A_UUID_V4);

$set = new \Ds\Set();
$set->add($uuid1);
$set->add($uuid2);

$this->assertTrue($set->contains($uuid1));
$this->assertTrue($set->contains($uuid2));
$this->assertCount(1, $set);
}

public function testCompare()
{
$uuids = [];
Expand Down