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

Appearance settings

[Uid] Use more concrete exception classes #60154

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions 6 src/Symfony/Component/Uid/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
CHANGELOG
=========

7.3
---

* Add `Symfony\Component\Uid\InvalidUuidException`
* Add `Symfony\Component\Uid\InvalidUlidException`

7.2
---

Expand Down
26 changes: 26 additions & 0 deletions 26 src/Symfony/Component/Uid/InvalidUlidException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Uid;

final class InvalidUlidException extends \InvalidArgumentException
{
public function __construct(
private readonly string $value,
OskarStark marked this conversation as resolved.
Show resolved Hide resolved
) {
parent::__construct(\sprintf('Invalid ULID: "%s".', $this->value));
}

public function getValue(): string
OskarStark marked this conversation as resolved.
Show resolved Hide resolved
{
return $this->value;
}
}
32 changes: 32 additions & 0 deletions 32 src/Symfony/Component/Uid/InvalidUuidException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Uid;

final class InvalidUuidException extends \InvalidArgumentException
{
public function __construct(
private readonly int $type,
private readonly string $value,
) {
parent::__construct(\sprintf('Invalid UUID%s: "%s".', $this->type ? 'v'.$this->type : '', $this->value));
}

public function getType(): int
{
return $this->type;
}

public function getValue(): string
{
return $this->value;
}
}
3 changes: 2 additions & 1 deletion 3 src/Symfony/Component/Uid/Tests/UlidTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\Uid\Tests;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Uid\InvalidUlidException;
use Symfony\Component\Uid\MaxUlid;
use Symfony\Component\Uid\NilUlid;
use Symfony\Component\Uid\Tests\Fixtures\CustomUlid;
Expand Down Expand Up @@ -41,7 +42,7 @@ public function testGenerate()

public function testWithInvalidUlid()
{
$this->expectException(\InvalidArgumentException::class);
$this->expectException(InvalidUlidException::class);
$this->expectExceptionMessage('Invalid ULID: "this is not a ulid".');

new Ulid('this is not a ulid');
Expand Down
2 changes: 1 addition & 1 deletion 2 src/Symfony/Component/Uid/Ulid.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function __construct(?string $ulid = null)
$this->uid = $ulid;
} else {
if (!self::isValid($ulid)) {
throw new \InvalidArgumentException(\sprintf('Invalid ULID: "%s".', $ulid));
throw new InvalidUlidException($ulid);
}

$this->uid = strtoupper($ulid);
Expand Down
4 changes: 2 additions & 2 deletions 4 src/Symfony/Component/Uid/Uuid.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ public function __construct(string $uuid, bool $checkVariant = false)
$type = preg_match('{^[0-9a-f]{8}(?:-[0-9a-f]{4}){3}-[0-9a-f]{12}$}Di', $uuid) ? (int) $uuid[14] : false;

if (false === $type || (static::TYPE ?: $type) !== $type) {
throw new \InvalidArgumentException(\sprintf('Invalid UUID%s: "%s".', static::TYPE ? 'v'.static::TYPE : '', $uuid));
throw new InvalidUuidException(static::TYPE, $uuid);
}

$this->uid = strtolower($uuid);

if ($checkVariant && !\in_array($this->uid[19], ['8', '9', 'a', 'b'], true)) {
throw new \InvalidArgumentException(\sprintf('Invalid UUID%s: "%s".', static::TYPE ? 'v'.static::TYPE : '', $uuid));
throw new InvalidUuidException(static::TYPE, $uuid);
}
}

Expand Down
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.