8000 [Routing] Add EnumRequirement to help generate route requirements from a \BackedEnum by fancyweb · Pull Request #45803 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Routing] Add EnumRequirement to help generate route requirements from a \BackedEnum #45803

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

Merged
merged 1 commit into from
Apr 12, 2022
Merged
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
1 change: 1 addition & 0 deletions src/Symfony/Component/Routing/CHANGELOG.md
8000
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ CHANGELOG
* Allow using UTF-8 parameter names
* Support the `attribute` type (alias of `annotation`) in annotation loaders
* Already encoded slashes are not decoded nor double-encoded anymore when generating URLs (query parameters)
* Add `EnumRequirement` to help generate route requirements from a `\BackedEnum`

5.3
---
Expand Down
50 changes: 50 additions & 0 deletions src/Symfony/Component/Routing/Requirement/EnumRequirement.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?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\Routing\Requirement;

use Symfony\Component\Routing\Exception\InvalidArgumentException;

final class EnumRequirement implements \Stringable
{
/**
* @var string[]
*/
private readonly array $values;

/**
* @template T of \BackedEnum
* @param class-string<T> $enum
* @param T ...$cases
*/
public function __construct(string $enum, \BackedEnum ...$cases)
{
if (!\is_subclass_of($enum, \BackedEnum::class, true)) {
throw new InvalidArgumentException(sprintf('"%s" is not a \BackedEnum class.', $enum));
}

foreach ($cases as $case) {
if (!$case instanceof $enum) {
throw new InvalidArgumentException(sprintf('"%s::%s" is not a case of "%s".', \get_class($case), $case->name, $enum));
}
}

$this->values = array_unique(array_map(
static fn (\BackedEnum $e): string => $e->value,
$cases ?: $enum::cases(),
));
}

public function __toString(): string
{
return implode('|', array_map(preg_quote(...), $this->values));
}
}
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 <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

8000 namespace Symfony\Component\Routing\Tests\Fixtures\Enum;

enum TestIntBackedEnum: int
{
case Hearts = 10;
case Diamonds = 20;
case Clubs = 30;
case Spades = 40;
}
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 <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\Routing\Tests\Fixtures\Enum;

enum TestStringBackedEnum: string
{
case Hearts = 'hearts';
case Diamonds = 'diamonds';
case Clubs = 'clubs';
case Spades = 'spades';
}
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 <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\Routing\Tests\Fixtures\Enum;

enum TestStringBackedEnum2: string
{
case Hearts = 'hearts';
case Diamonds = 'diamonds';
case Clubs = 'clubs';
case Spades = 'spa|des';
}
20 changes: 20 additions & 0 deletions src/Symfony/Component/Routing/Tests/Fixtures/Enum/TestUnitEnum.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 <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\Routing\Tests\Fixtures\Enum;

enum TestUnitEnum
{
case Hearts;
case Diamonds;
case Clubs;
case Spades;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?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\Routing\Tests\Requirement;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Routing\Exception\InvalidArgumentException;
use Symfony\Component\Routing\Requirement\EnumRequirement;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\Tests\Fixtures\Enum\TestIntBackedEnum;
use Symfony\Component\Routing\Tests\Fixtures\Enum\TestStringBackedEnum;
use Symfony\Component\Routing\Tests\Fixtures\Enum\TestStringBackedEnum2;
use Symfony\Component\Routing\Tests\Fixtures\Enum\TestUnitEnum;

class EnumRequirementTest extends TestCase
{
public function testNotABackedEnum()
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('"Symfony\Component\Routing\Tests\Fixtures\Enum\TestUnitEnum" is not a \BackedEnum class.');

new EnumRequirement(TestUnitEnum::class);
}

public function testCaseFromAnotherEnum()
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('"Symfony\Component\Routing\Tests\Fixtures\Enum\TestStringBackedEnum2::Spades" is not a case of "Symfony\Component\Routing\Tests\Fixtures\Enum\TestStringBackedEnum".');

new EnumRequirement(TestStringBackedEnum::class, TestStringBackedEnum::Diamonds, TestStringBackedEnum2::Spades);
}

/**
* @dataProvider provideToString
*/
public function testToString(string $expected, string $enum, \BackedEnum ...$cases)
{
$this->assertSame($expected, (string) new EnumRequirement($enum, ...$cases));
}

public function provideToString()
{
return [
['hearts|diamonds|clubs|spades', TestStringBackedEnum::class],
['10|20|30|40', TestIntBackedEnum::class],
['diamonds|spades', TestStringBackedEnum::class, TestStringBackedEnum::Diamonds, TestStringBackedEnum::Spades],
['hearts|diamonds|clubs|spa\|des', TestStringBackedEnum2::class],
];
}

public function testInRoute()
{
$this->assertSame([
'bar' => 'hearts|diamonds|clubs|spades',
], (new Route(
path: '/foo/{bar}',
requirements: [
'bar' => new EnumRequirement(TestStringBackedEnum::class),
],
))->getRequirements());
}
}
0