File tree 4 files changed +79
-0
lines changed
4 files changed +79
-0
lines changed Original file line number Diff line number Diff line change 19
19
use Ramsey \Uuid \Codec \CodecInterface ;
20
20
use Ramsey \Uuid \Converter \NumberConverterInterface ;
21
21
use Ramsey \Uuid \Converter \TimeConverterInterface ;
22
+ use Ramsey \Uuid \Exception \InvalidUuidStringException ;
22
23
use Ramsey \Uuid \Exception \UnsupportedOperationException ;
23
24
use Ramsey \Uuid \Fields \FieldsInterface ;
24
25
use Ramsey \Uuid \Lazy \LazyUuidFromString ;
@@ -494,6 +495,28 @@ public static function fromString(string $uuid): UuidInterface
494
495
return self ::getFactory ()->fromString ($ uuid );
495
496
}
496
497
498
+ /**
499
+ * Creates a UUID from a valid string representation, validated against the isValid method
500
+ *
501
+ * @param string $uuid A valid UUID string representation
502
+ *
503
+ * @return UuidInterface A UuidInterface instance created from a valid UUID
504
+ * string representation
505
+ *
506
+ * @throws InvalidUuidStringException
507
+ *
508
+ * @psalm-pure note: changing the internal factory is an edge case not covered by purity invariants,
509
+ * but under constant factory setups, this method operates in functionally pure manners
510
+ */
511
+ public static function fromStrictString (string $ uuid ): UuidInterface
512
+ {
513
+ if (! self ::isValid ($ uuid )) {
514
+ throw new InvalidUuidStringException ('Invalid UUID string: ' . $ uuid );
515
+ }
516
+
517
+ return self ::fromString ($ uuid );
518
+ }
519
+
497
520
/**
498
521
* Creates a UUID from a DateTimeInterface instance
499
522
*
Original file line number Diff line number Diff line change 19
19
use Ramsey \Uuid \Codec \CodecInterface ;
20
20
use Ramsey \Uuid \Converter \NumberConverterInterface ;
21
21
use Ramsey \Uuid \Converter \TimeConverterInterface ;
22
+ use Ramsey \Uuid \Exception \InvalidUuidStringException ;
22
23
use Ramsey \Uuid \Generator \DceSecurityGeneratorInterface ;
23
24
use Ramsey \Uuid \Generator \DefaultTimeGenerator ;
24
25
use Ramsey \Uuid \Generator \NameGeneratorInterface ;
@@ -279,6 +280,18 @@ public function fromString(string $uuid): UuidInterface
279
280
return $ this ->codec ->decode ($ uuid );
280
281
}
281
282
283
+ /**
284
+ * @psalm-pure
285
+ */
286
+ public function fromStrictString (string $ uuid ): UuidInterface
287
+ {
288
+ if (! $ this ->getValidator ()->validate ($ uuid )) {
289
+ throw new InvalidUuidStringException ('Invalid UUID string: ' . $ uuid );
290
+ }
291
+
292
+ return $ this ->codec ->decode ($ uuid );
293
+ }
294
+
282
295
/**
283
296
* @psalm-pure
284
297
*/
Original file line number Diff line number Diff line change 15
15
namespace Ramsey \Uuid ;
16
16
17
17
use DateTimeInterface ;
18
+ use Ramsey \Uuid \Exception \InvalidUuidStringException ;
18
19
use Ramsey \Uuid \Type \Hexadecimal ;
19
20
use Ramsey \Uuid \Type \Integer as IntegerObject ;
20
21
use Ramsey \Uuid \Validator \ValidatorInterface ;
@@ -80,6 +81,20 @@ public function fromInteger(string $integer): UuidInterface;
80
81
*/
81
82
public function fromString (string $ uuid ): UuidInterface ;
82
83
84
+ /**
85
+ * Creates a UUID from a valid string representation, validated against the isValid method
86
+ *
87
+ * @param string $uuid A valid UUID string representation
88
+ *
89
+ * @return UuidInterface A UuidInterface instance created from a valid UUID
90
+ * string representation
91
+ *
92
+ * @throws InvalidUuidStringException
93
+ *
94
+ * @psalm-pure
95
+ */
96
+ public function fromStrictString (string $ uuid ): UuidInterface ;
97
+
83
98
/**
84
99
* Returns the validator to use for the factory
85
100
*
Original file line number Diff line number Diff line change @@ -73,6 +73,34 @@ public function testFromString(): void
73
73
Uuid::fromString ('ff6f8cb0-c57d-11e1-9b21-0800200c9a66 ' )
74
74
->toString ()
75
75
);
76
+
77
+ $ this ->assertSame (
78
+ '00000000-0000-0000-0000-000000000000 ' ,
79
+ Uuid::fromString ('00000000000000000000000000000000 ' )
80
+ ->toString ()
81
+ );
82
+ }
83
+
84
+ public function testFromStrictString (): void
85
+ {
86
+ $ this ->assertSame (
87
+ 'ff6f8cb0-c57d-11e1-9b21-0800200c9a66 ' ,
88
+ Uuid::fromStrictString ('ff6f8cb0-c57d-11e1-9b21-0800200c9a66 ' )
89
+ ->toString ()
90
+ );
91
+
92
+ $ this ->assertSame (
93
+ '00000000-0000-0000-0000-000000000000 ' ,
94
+ Uuid::fromStrictString ('00000000-0000-0000-0000-000000000000 ' )
95
+ ->toString ()
96
+ );
97
+ }
98
+
99
+ public function testFromStrictStringWithInvalidUuidString (): void
100
+ {
101
+ $ this ->expectException (InvalidUuidStringException::class);
102
+
103
+ Uuid::fromStrictString ('00000000000000000000000000000000 ' );
76
104
}
77
105
78
106
public function testFromHexadecimal (): void
You can’t perform that action at this time.
0 commit comments