Skip to content

Commit fac0c0c

Browse files
committed
wip
1 parent d459bdb commit fac0c0c

File tree

4 files changed

+79
-0
lines changed

4 files changed

+79
-0
lines changed

src/Uuid.php

+23
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Ramsey\Uuid\Codec\CodecInterface;
2020
use Ramsey\Uuid\Converter\NumberConverterInterface;
2121
use Ramsey\Uuid\Converter\TimeConverterInterface;
22+
use Ramsey\Uuid\Exception\InvalidUuidStringException;
2223
use Ramsey\Uuid\Exception\UnsupportedOperationException;
2324
use Ramsey\Uuid\Fields\FieldsInterface;
2425
use Ramsey\Uuid\Lazy\LazyUuidFromString;
@@ -494,6 +495,28 @@ public static function fromString(string $uuid): UuidInterface
494495
return self::getFactory()->fromString($uuid);
495496
}
496497

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+
497520
/**
498521
* Creates a UUID from a DateTimeInterface instance
499522
*

src/UuidFactory.php

+13
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Ramsey\Uuid\Codec\CodecInterface;
2020
use Ramsey\Uuid\Converter\NumberConverterInterface;
2121
use Ramsey\Uuid\Converter\TimeConverterInterface;
22+
use Ramsey\Uuid\Exception\InvalidUuidStringException;
2223
use Ramsey\Uuid\Generator\DceSecurityGeneratorInterface;
2324
use Ramsey\Uuid\Generator\DefaultTimeGenerator;
2425
use Ramsey\Uuid\Generator\NameGeneratorInterface;
@@ -279,6 +280,18 @@ public function fromString(string $uuid): UuidInterface
279280
return $this->codec->decode($uuid);
280281
}
281282

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+
282295
/**
283296
* @psalm-pure
284297
*/

src/UuidFactoryInterface.php

+15
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
namespace Ramsey\Uuid;
1616

1717
use DateTimeInterface;
18+
use Ramsey\Uuid\Exception\InvalidUuidStringException;
1819
use Ramsey\Uuid\Type\Hexadecimal;
1920
use Ramsey\Uuid\Type\Integer as IntegerObject;
2021
use Ramsey\Uuid\Validator\ValidatorInterface;
@@ -80,6 +81,20 @@ public function fromInteger(string $integer): UuidInterface;
8081
*/
8182
public function fromString(string $uuid): UuidInterface;
8283

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+
8398
/**
8499
* Returns the validator to use for the factory
85100
*

tests/UuidTest.php

+28
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,34 @@ public function testFromString(): void
7373
Uuid::fromString('ff6f8cb0-c57d-11e1-9b21-0800200c9a66')
7474
->toString()
7575
);
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');
76104
}
77105

78106
public function testFromHexadecimal(): void

0 commit comments

Comments
 (0)