8000 [Uid] make `Uuid::equals()` accept any types of argument for more fle… · symfony/symfony@46721c1 · GitHub
[go: up one dir, main page]

Skip to content

Commit 46721c1

Browse files
hhamonnicolas-grekas
authored andcommitted
[Uid] make Uuid::equals() accept any types of argument for more flexibility
1 parent 14c95a9 commit 46721c1

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,22 @@ public function testEquals()
9494
$this->assertFalse($uuid1->equals($uuid2));
9595
}
9696

97+
/**
98+
* @dataProvider provideInvalidEqualType
99+
*/
100+
public function testEqualsAgainstOtherType($other)
101+
{
102+
$this->assertFalse((new Uuid(self::A_UUID_V4))->equals($other));
103+
}
104+
105+
public function provideInvalidEqualType()
106+
{
107+
yield [null];
108+
yield [self::A_UUID_V1];
109+
yield [self::A_UUID_V4];
110+
yield [new \stdClass()];
111+
}
112+
97113
public function testCompare()
98114
{
99115
$uuids = [];

src/Symfony/Component/Uid/Uuid.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public function __construct(string $uuid = null)
4242
throw new \InvalidArgumentException(sprintf('Invalid UUID: "%s".', $uuid));
4343
}
4444

45-
$this->uuid = $uuid;
45+
$this->uuid = strtr($uuid, 'ABCDEF', 'abcdef');
4646
}
4747

4848
public static function v1(): self
@@ -85,8 +85,15 @@ public function isNull(): bool
8585
return uuid_is_null($this->uuid);
8686
}
8787

88-
public function equals(self $other): bool
88+
/**
89+
* Returns whether the argument is of class Uuid and contains the same value as the current instance.
90+
*/
91+
public function equals($other): bool
8992
{
93+
if (!$other instanceof self) {
94+
return false;
95+
}
96+
9097
return 0 === uuid_compare($this->uuid, $other->uuid);
9198
}
9299

0 commit comments

Comments
 (0)
0