Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commite86ffe3

Browse files
rela589nnicolas-grekas
authored andcommitted
[Uid] Add component-specific exception classes
1 parent1088f53 commite86ffe3

17 files changed

+135
-33
lines changed

‎src/Symfony/Component/Uid/AbstractUid.php

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespaceSymfony\Component\Uid;
1313

14+
useSymfony\Component\Uid\Exception\InvalidArgumentException;
15+
1416
/**
1517
* @author Nicolas Grekas <p@tchwork.com>
1618
*/
@@ -29,41 +31,41 @@ abstract public static function isValid(string $uid): bool;
2931
/**
3032
* Creates an AbstractUid from an identifier represented in any of the supported formats.
3133
*
32-
* @throws\InvalidArgumentException When the passed value is not valid
34+
* @throws InvalidArgumentException When the passed value is not valid
3335
*/
3436
abstractpublicstaticfunctionfromString(string$uid):static;
3537

3638
/**
37-
* @throws\InvalidArgumentException When the passed value is not valid
39+
* @throws InvalidArgumentException When the passed value is not valid
3840
*/
3941
publicstaticfunctionfromBinary(string$uid):static
4042
{
4143
if (16 !==\strlen($uid)) {
42-
thrownew\InvalidArgumentException('Invalid binary uid provided.');
44+
thrownewInvalidArgumentException('Invalid binary uid provided.');
4345
}
4446

4547
returnstatic::fromString($uid);
4648
}
4749

4850
/**
49-
* @throws\InvalidArgumentException When the passed value is not valid
51+
* @throws InvalidArgumentException When the passed value is not valid
5052
*/
5153
publicstaticfunctionfromBase58(string$uid):static
5254
{
5355
if (22 !==\strlen($uid)) {
54-
thrownew\InvalidArgumentException('Invalid base-58 uid provided.');
56+
thrownewInvalidArgumentException('Invalid base-58 uid provided.');
5557
}
5658

5759
returnstatic::fromString($uid);
5860
}
5961

6062
/**
61-
* @throws\InvalidArgumentException When the passed value is not valid
63+
* @throws InvalidArgumentException When the passed value is not valid
6264
*/
6365
publicstaticfunctionfromBase32(string$uid):static
6466
{
6567
if (26 !==\strlen($uid)) {
66-
thrownew\InvalidArgumentException('Invalid base-32 uid provided.');
68+
thrownewInvalidArgumentException('Invalid base-32 uid provided.');
6769
}
6870

6971
returnstatic::fromString($uid);
@@ -72,12 +74,12 @@ public static function fromBase32(string $uid): static
7274
/**
7375
* @param string $uid A valid RFC 9562/4122 uid
7476
*
75-
* @throws\InvalidArgumentException When the passed value is not valid
77+
* @throws InvalidArgumentException When the passed value is not valid
7678
*/
7779
publicstaticfunctionfromRfc4122(string$uid):static
7880
{
7981
if (36 !==\strlen($uid)) {
80-
thrownew\InvalidArgumentException('Invalid RFC4122 uid provided.');
82+
thrownewInvalidArgumentException('Invalid RFC4122 uid provided.');
8183
}
8284

8385
returnstatic::fromString($uid);

‎src/Symfony/Component/Uid/BinaryUtil.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespaceSymfony\Component\Uid;
1313

14+
useSymfony\Component\Uid\Exception\InvalidArgumentException;
15+
1416
/**
1517
* @internal
1618
*
@@ -162,7 +164,7 @@ public static function dateTimeToHex(\DateTimeInterface $time): string
162164
{
163165
if (\PHP_INT_SIZE >=8) {
164166
if (-self::TIME_OFFSET_INT >$time = (int)$time->format('Uu0')) {
165-
thrownew\InvalidArgumentException('The given UUID date cannot be earlier than 1582-10-15.');
167+
thrownewInvalidArgumentException('The given UUID date cannot be earlier than 1582-10-15.');
166168
}
167169

168170
returnstr_pad(dechex(self::TIME_OFFSET_INT +$time),16,'0', \STR_PAD_LEFT);
@@ -171,7 +173,7 @@ public static function dateTimeToHex(\DateTimeInterface $time): string
171173
$time =$time->format('Uu0');
172174
$negative ='-' ===$time[0];
173175
if ($negative &&self::TIME_OFFSET_INT <$time =substr($time,1)) {
174-
thrownew\InvalidArgumentException('The given UUID date cannot be earlier than 1582-10-15.');
176+
thrownewInvalidArgumentException('The given UUID date cannot be earlier than 1582-10-15.');
175177
}
176178
$time =self::fromBase($time,self::BASE10);
177179
$time =str_pad($time,8,"\0", \STR_PAD_LEFT);

‎src/Symfony/Component/Uid/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
7.3
5+
---
6+
7+
* Add component-specific exception hierarchy
8+
49
7.2
510
---
611

‎src/Symfony/Component/Uid/Command/GenerateUuidCommand.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
useSymfony\Component\Console\Output\ConsoleOutputInterface;
2121
useSymfony\Component\Console\Output\OutputInterface;
2222
useSymfony\Component\Console\Style\SymfonyStyle;
23+
useSymfony\Component\Uid\Exception\LogicException;
2324
useSymfony\Component\Uid\Factory\UuidFactory;
2425
useSymfony\Component\Uid\Uuid;
2526

@@ -146,7 +147,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
146147
$create =function ()use ($namespace,$name):Uuid {
147148
try {
148149
$factory =$this->factory->nameBased($namespace);
149-
}catch (\LogicException) {
150+
}catch (LogicException) {
150151
thrownew \InvalidArgumentException('Missing namespace: use the "--namespace" option or configure a default namespace in the underlying factory.');
151152
}
152153

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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+
namespaceSymfony\Component\Uid\Exception;
13+
14+
class InvalidArgumentExceptionextends \InvalidArgumentException
15+
{
16+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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+
namespaceSymfony\Component\Uid\Exception;
13+
14+
class InvalidUlidExceptionextends InvalidArgumentException
15+
{
16+
publicfunction__construct(string$value)
17+
{
18+
parent::__construct(\sprintf('Invalid ULID: "%s".',$value));
19+
}
20+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
namespaceSymfony\Component\Uid\Exception;
13+
14+
class InvalidUuidExceptionextends InvalidArgumentException
15+
{
16+
publicfunction__construct(
17+
publicreadonlyint$type,
18+
string$value,
19+
) {
20+
parent::__construct(\sprintf('Invalid UUID%s: "%s".',$type ?'v'.$type :'',$value));
21+
}
22+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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+
namespaceSymfony\Component\Uid\Exception;
13+
14+
class LogicExceptionextends \LogicException
15+
{
16+
}

‎src/Symfony/Component/Uid/Factory/UuidFactory.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespaceSymfony\Component\Uid\Factory;
1313

14+
useSymfony\Component\Uid\Exception\LogicException;
1415
useSymfony\Component\Uid\Uuid;
1516
useSymfony\Component\Uid\UuidV1;
1617
useSymfony\Component\Uid\UuidV4;
@@ -67,12 +68,15 @@ public function timeBased(Uuid|string|null $node = null): TimeBasedUuidFactory
6768
returnnewTimeBasedUuidFactory($this->timeBasedClass,$node);
6869
}
6970

71+
/**
72+
* @throws LogicException When no namespace is defined
73+
*/
7074
publicfunctionnameBased(Uuid|string|null$namespace =null):NameBasedUuidFactory
7175
{
7276
$namespace ??=$this->nameBasedNamespace;
7377

7478
if (null ===$namespace) {
75-
thrownew\LogicException(\sprintf('A namespace should be defined when using "%s()".',__METHOD__));
79+
thrownewLogicException(\sprintf('A namespace should be defined when using "%s()".',__METHOD__));
7680
}
7781

7882
returnnewNameBasedUuidFactory($this->nameBasedClass,$this->getNamespace($namespace));

‎src/Symfony/Component/Uid/Tests/Factory/UlidFactoryTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespaceSymfony\Component\Uid\Tests\Factory;
1313

1414
usePHPUnit\Framework\TestCase;
15+
useSymfony\Component\Uid\Exception\InvalidArgumentException;
1516
useSymfony\Component\Uid\Factory\UlidFactory;
1617

1718
finalclass UlidFactoryTestextends TestCase
@@ -36,7 +37,7 @@ public function testCreate()
3637

3738
publicfunctiontestCreateWithInvalidTimestamp()
3839
{
39-
$this->expectException(\InvalidArgumentException::class);
40+
$this->expectException(InvalidArgumentException::class);
4041
$this->expectExceptionMessage('The timestamp must be positive.');
4142

4243
(newUlidFactory())->create(new \DateTimeImmutable('@-1000'));

‎src/Symfony/Component/Uid/Tests/Factory/UuidFactoryTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespaceSymfony\Component\Uid\Tests\Factory;
1313

1414
usePHPUnit\Framework\TestCase;
15+
useSymfony\Component\Uid\Exception\InvalidArgumentException;
1516
useSymfony\Component\Uid\Factory\UuidFactory;
1617
useSymfony\Component\Uid\NilUuid;
1718
useSymfony\Component\Uid\Uuid;
@@ -81,7 +82,7 @@ public function testCreateTimed()
8182

8283
publicfunctiontestInvalidCreateTimed()
8384
{
84-
$this->expectException(\InvalidArgumentException::class);
85+
$this->expectException(InvalidArgumentException::class);
8586
$this->expectExceptionMessage('The given UUID date cannot be earlier than 1582-10-15.');
8687

8788
(newUuidFactory())->timeBased()->create(new \DateTimeImmutable('@-12219292800.001000'));

‎src/Symfony/Component/Uid/Tests/UlidTest.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
namespaceSymfony\Component\Uid\Tests;
1313

1414
usePHPUnit\Framework\TestCase;
15+
useSymfony\Component\Uid\Exception\InvalidArgumentException;
16+
useSymfony\Component\Uid\Exception\InvalidUlidException;
1517
useSymfony\Component\Uid\MaxUlid;
1618
useSymfony\Component\Uid\NilUlid;
1719
useSymfony\Component\Uid\Tests\Fixtures\CustomUlid;
@@ -41,7 +43,7 @@ public function testGenerate()
4143

4244
publicfunctiontestWithInvalidUlid()
4345
{
44-
$this->expectException(\InvalidArgumentException::class);
46+
$this->expectException(InvalidUlidException::class);
4547
$this->expectExceptionMessage('Invalid ULID: "this is not a ulid".');
4648

4749
newUlid('this is not a ulid');
@@ -151,7 +153,7 @@ public function testFromBinary()
151153
*/
152154
publicfunctiontestFromBinaryInvalidFormat(string$ulid)
153155
{
154-
$this->expectException(\InvalidArgumentException::class);
156+
$this->expectException(InvalidArgumentException::class);
155157

156158
Ulid::fromBinary($ulid);
157159
}
@@ -178,7 +180,7 @@ public function testFromBase58()
178180
*/
179181
publicfunctiontestFromBase58InvalidFormat(string$ulid)
180182
{
181-
$this->expectException(\InvalidArgumentException::class);
183+
$this->expectException(InvalidArgumentException::class);
182184

183185
Ulid::fromBase58($ulid);
184186
}
@@ -205,7 +207,7 @@ public function testFromBase32()
205207
*/
206208
publicfunctiontestFromBase32InvalidFormat(string$ulid)
207209
{
208-
$this->expectException(\InvalidArgumentException::class);
210+
$this->expectException(InvalidArgumentException::class);
209211

210212
Ulid::fromBase32($ulid);
211213
}
@@ -232,7 +234,7 @@ public function testFromRfc4122()
232234
*/
233235
publicfunctiontestFromRfc4122InvalidFormat(string$ulid)
234236
{
235-
$this->expectException(\InvalidArgumentException::class);
237+
$this->expectException(InvalidArgumentException::class);
236238

237239
Ulid::fromRfc4122($ulid);
238240
}

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespaceSymfony\Component\Uid\Tests;
1313

1414
usePHPUnit\Framework\TestCase;
15+
useSymfony\Component\Uid\Exception\InvalidArgumentException;
1516
useSymfony\Component\Uid\MaxUuid;
1617
useSymfony\Component\Uid\NilUuid;
1718
useSymfony\Component\Uid\Tests\Fixtures\CustomUuid;
@@ -35,7 +36,7 @@ class UuidTest extends TestCase
3536
*/
3637
publicfunctiontestConstructorWithInvalidUuid(string$uuid)
3738
{
38-
$this->expectException(\InvalidArgumentException::class);
39+
$this->expectException(InvalidArgumentException::class);
3940
$this->expectExceptionMessage('Invalid UUID: "'.$uuid.'".');
4041

4142
Uuid::fromString($uuid);
@@ -58,7 +59,7 @@ public function testInvalidVariant(string $uuid)
5859
$uuid = (string)$uuid;
5960
$class = Uuid::class.'V'.$uuid[14];
6061

61-
$this->expectException(\InvalidArgumentException::class);
62+
$this->expectException(InvalidArgumentException::class);
6263
$this->expectExceptionMessage('Invalid UUIDv'.$uuid[14].': "'.$uuid.'".');
6364

6465
new$class($uuid);
@@ -381,7 +382,7 @@ public function testFromBinary()
381382
*/
382383
publicfunctiontestFromBinaryInvalidFormat(string$ulid)
383384
{
384-
$this->expectException(\InvalidArgumentException::class);
385+
$this->expectException(InvalidArgumentException::class);
385386

386387
Uuid::fromBinary($ulid);
387388
}
@@ -408,7 +409,7 @@ public function testFromBase58()
408409
*/
409410
publicfunctiontestFromBase58InvalidFormat(string$ulid)
410411
{
411-
$this->expectException(\InvalidArgumentException::class);
412+
$this->expectException(InvalidArgumentException::class);
412413

413414
Uuid::fromBase58($ulid);
414415
}
@@ -435,7 +436,7 @@ public function testFromBase32()
435436
*/
436437
publicfunctiontestFromBase32InvalidFormat(string$ulid)
437438
{
438-
$this->expectException(\InvalidArgumentException::class);
439+
$this->expectException(InvalidArgumentException::class);
439440

440441
Uuid::fromBase32($ulid);
441442
}
@@ -462,7 +463,7 @@ public function testFromRfc4122()
462463
*/
463464
publicfunctiontestFromRfc4122InvalidFormat(string$ulid)
464465
{
465-
$this->expectException(\InvalidArgumentException::class);
466+
$this->expectException(InvalidArgumentException::class);
466467

467468
Uuid::fromRfc4122($ulid);
468469
}
@@ -509,7 +510,7 @@ public function testV1ToV6()
509510

510511
publicfunctiontestV1ToV7BeforeUnixEpochThrows()
511512
{
512-
$this->expectException(\InvalidArgumentException::class);
513+
$this->expectException(InvalidArgumentException::class);
513514
$this->expectExceptionMessage('Cannot convert UUID to v7: its timestamp is before the Unix epoch.');
514515

515516
(newUuidV1('9aba8000-ff00-11b0-b3db-3b3fc83afdfc'))->toV7();// Timestamp is 1969-01-01 00:00:00.0000000

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp