[Uid] make Uuid::equals() accept any types of argument for more flexibility

This commit is contained in:
Hugo Hamon 2020-03-13 11:29:57 +01:00 committed by Nicolas Grekas
parent 14c95a9d8c
commit 46721c19f9
2 changed files with 25 additions and 2 deletions

View File

@ -94,6 +94,22 @@ class UuidTest extends TestCase
$this->assertFalse($uuid1->equals($uuid2));
}
/**
* @dataProvider provideInvalidEqualType
*/
public function testEqualsAgainstOtherType($other)
{
$this->assertFalse((new Uuid(self::A_UUID_V4))->equals($other));
}
public function provideInvalidEqualType()
{
yield [null];
yield [self::A_UUID_V1];
yield [self::A_UUID_V4];
yield [new \stdClass()];
}
public function testCompare()
{
$uuids = [];

View File

@ -42,7 +42,7 @@ class Uuid implements \JsonSerializable
throw new \InvalidArgumentException(sprintf('Invalid UUID: "%s".', $uuid));
}
$this->uuid = $uuid;
$this->uuid = strtr($uuid, 'ABCDEF', 'abcdef');
}
public static function v1(): self
@ -85,8 +85,15 @@ class Uuid implements \JsonSerializable
return uuid_is_null($this->uuid);
}
public function equals(self $other): bool
/**
* Returns whether the argument is of class Uuid and contains the same value as the current instance.
*/
public function equals($other): bool
{
if (!$other instanceof self) {
return false;
}
return 0 === uuid_compare($this->uuid, $other->uuid);
}