Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[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 all 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 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
27 changes: 27 additions & 0 deletions src/Symfony/Component/Uid/InvalidUidException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

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

namespace Symfony\Component\Uid;

abstract class InvalidUidException extends \InvalidArgumentException
{
public function __construct(
private readonly string $value,
string $message,
) {
parent::__construct($message);
}

public function getValue(): string
{
return $this->value;
}
}
20 changes: 20 additions & 0 deletions src/Symfony/Component/Uid/InvalidUlidException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* 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 InvalidUidException
{
public function __construct(string $value)
{
parent::__construct($value, \sprintf('Invalid ULID: "%s".', $value));
}
}
20 changes: 20 additions & 0 deletions src/Symfony/Component/Uid/InvalidUuidException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* 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 InvalidUidException
{
public function __construct(int $type, string $value)
{
parent::__construct($value, \sprintf('Invalid UUID%s: "%s".', $type ? 'v'.$type : '', $value));
}
}
3 changes: 2 additions & 1 deletion 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 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 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