File tree 4 files changed +55
-1
lines changed
src/Symfony/Component/Uid
4 files changed +55
-1
lines changed Original file line number Diff line number Diff line change 11
11
12
12
namespace Symfony \Component \Uid ;
13
13
14
+ use Ds \Hashable ;
15
+
16
+ // ensure we have _some_ version of the Ds\Hashable interface
17
+ require_once __DIR__ .'/DsHashablePolyfill.php ' ;
18
+
14
19
/**
15
20
* @author Nicolas Grekas <p@tchwork.com>
16
21
*/
17
- abstract class AbstractUid implements \JsonSerializable, \Stringable
22
+ abstract class AbstractUid implements \JsonSerializable, \Stringable, Hashable
18
23
{
19
24
/**
20
25
* The identifier in its canonic representation.
@@ -159,6 +164,11 @@ public function equals(mixed $other): bool
159
164
return $ this ->uid === $ other ->uid ;
160
165
}
161
166
167
+ public function hash (): mixed
168
+ {
169
+ return $ this ->uid ;
170
+ }
171
+
162
172
public function compare (self $ other ): int
163
173
{
164
174
return (\strlen ($ this ->uid ) - \strlen ($ other ->uid )) ?: ($ this ->uid <=> $ other ->uid );
Original file line number Diff line number Diff line change @@ -6,6 +6,7 @@ CHANGELOG
6
6
7
7
* Add ` UuidV1::toV6() ` , ` UuidV1::toV7() ` and ` UuidV6::toV7() `
8
8
* Add ` AbstractUid::toString() `
9
+ * Make ` AbstractUid ` implement ` Ds\Hashable ` if available
9
10
10
11
6.2
11
12
---
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ declare (strict_types=1 );
4
+
5
+ /*
6
+ * This file is part of the Symfony package.
7
+ *
8
+ * (c) Fabien Potencier <fabien@symfony.com>
9
+ *
10
+ * For the full copyright and license information, please view the LICENSE
11
+ * file that was distributed with this source code.
12
+ */
13
+
14
+ namespace Symfony \Component \Uid ;
15
+
16
+ interface DsHashablePolyfill
17
+ {
18
+ public function equals (mixed $ other ): bool ;
19
+
20
+ public function hash (): mixed ;
21
+ }
22
+
23
+ if (!interface_exists (\Ds \Hashable::class)) {
24
+ class_alias (DsHashablePolyfill::class, \Ds \Hashable::class);
25
+ }
Original file line number Diff line number Diff line change @@ -248,6 +248,24 @@ public static function provideInvalidEqualType(): iterable
248
248
yield [new \stdClass ()];
249
249
}
250
250
251
+ public function testHashable ()
252
+ {
253
+ $ uuid1 = new UuidV4 (self ::A_UUID_V4 );
254
+ $ uuid2 = new UuidV4 (self ::A_UUID_V4 );
255
+
256
+ $ this ->assertSame ($ uuid1 ->hash (), $ uuid2 ->hash ());
257
+
258
+ if (\extension_loaded ('ds ' )) {
259
+ $ set = new \Ds \Set ();
260
+ $ set ->add ($ uuid1 );
261
+ $ set ->add ($ uuid2 );
262
+
263
+ $ this ->assertTrue ($ set ->contains ($ uuid1 ));
264
+ $ this ->assertTrue ($ set ->contains ($ uuid2 ));
265
+ $ this ->assertSame (1 , $ set ->count ());
266
+ }
267
+ }
268
+
251
269
public function testCompare ()
252
270
{
253
271
$ uuids = [];
You can’t perform that action at this time.
0 commit comments