File tree Expand file tree Collapse file tree 4 files changed +66
-1
lines changed
src/Symfony/Component/Uid Expand file tree Collapse file tree 4 files changed +66
-1
lines changed Original file line number Diff line number Diff line change 14
14
/**
15
15
* @author Nicolas Grekas <p@tchwork.com>
16
16
*/
17
- abstract class AbstractUid implements \JsonSerializable, \Stringable
17
+ abstract class AbstractUid implements \JsonSerializable, \Stringable, HashableInterface
18
18
{
19
19
/**
20
20
* The identifier in its canonic representation.
@@ -159,6 +159,11 @@ public function equals(mixed $other): bool
159
159
return $ this ->uid === $ other ->uid ;
160
160
}
161
161
162
+ public function hash (): string
163
+ {
164
+ return $ this ->uid ;
165
+ }
166
+
162
167
public function compare (self $ other ): int
163
168
{
164
169
return (\strlen ($ this ->uid ) - \strlen ($ other ->uid )) ?: ($ this ->uid <=> $ other ->uid );
Original file line number Diff line number Diff line change 1
1
CHANGELOG
2
2
=========
3
3
4
+ 7.2
5
+ ---
6
+
7
+ * Make ` AbstractUid ` implement ` Ds\Hashable ` if available
8
+
4
9
7.1
5
10
---
6
11
Original file line number Diff line number Diff line change
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
+ if (interface_exists (\Ds \Hashable::class)) {
15
+ /**
16
+ * @internal
17
+ */
18
+ interface HashableInterface extends \Ds \Hashable
19
+ {
20
+ public function hash (): string ;
21
+ }
22
+ } else {
23
+ /**
24
+ * @internal
25
+ */
26
+ interface HashableInterface
27
+ {
28
+ public function equals (mixed $ other ): bool ;
29
+
30
+ public function hash (): string ;
31
+ }
32
+ }
Original file line number Diff line number Diff line change @@ -248,6 +248,29 @@ 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
+
259
+ /** @requires extension ds */
260
+ public function testDsCompatibility ()
261
+ {
262
+ $ uuid1 = new UuidV4 (self ::A_UUID_V4 );
263
+ $ uuid2 = new UuidV4 (self ::A_UUID_V4 );
264
+
265
+ $ set = new \Ds \Set ();
266
+ $ set ->add ($ uuid1 );
267
+ $ set ->add ($ uuid2 );
268
+
269
+ $ this ->assertTrue ($ set ->contains ($ uuid1 ));
270
+ $ this ->assertTrue ($ set ->contains ($ uuid2 ));
271
+ $ this ->assertCount (1 , $ set );
272
+ }
273
+
251
274
public function testCompare ()
252
275
{
253
276
$ uuids = [];
You can’t perform that action at this time.
0 commit comments