diff --git a/src/Symfony/Component/Routing/CHANGELOG.md b/src/Symfony/Component/Routing/CHANGELOG.md index 06536a9f718b..45b0f2306ca2 100644 --- a/src/Symfony/Component/Routing/CHANGELOG.md +++ b/src/Symfony/Component/Routing/CHANGELOG.md @@ -5,6 +5,7 @@ CHANGELOG ----- * Added support for inline definition of requirements and defaults for host + * Added support for `\A` and `\z` as regex start and end for route requirement 5.1.0 ----- diff --git a/src/Symfony/Component/Routing/Route.php b/src/Symfony/Component/Routing/Route.php index 08120c428f09..9c852669da66 100644 --- a/src/Symfony/Component/Routing/Route.php +++ b/src/Symfony/Component/Routing/Route.php @@ -553,12 +553,18 @@ private function extractInlineDefaultsAndRequirements(string $pattern): string private function sanitizeRequirement(string $key, string $regex) { - if ('' !== $regex && '^' === $regex[0]) { - $regex = (string) substr($regex, 1); // returns false for a single character + if ('' !== $regex) { + if ('^' === $regex[0]) { + $regex = substr($regex, 1); + } elseif (0 === strpos($regex, '\\A')) { + $regex = substr($regex, 2); + } } if ('$' === substr($regex, -1)) { $regex = substr($regex, 0, -1); + } elseif (\strlen($regex) - 2 === strpos($regex, '\\z')) { + $regex = substr($regex, 0, -2); } if ('' === $regex) { diff --git a/src/Symfony/Component/Routing/Tests/RouteTest.php b/src/Symfony/Component/Routing/Tests/RouteTest.php index 863e65c34749..5ad1306d5d74 100644 --- a/src/Symfony/Component/Routing/Tests/RouteTest.php +++ b/src/Symfony/Component/Routing/Tests/RouteTest.php @@ -122,6 +122,14 @@ public function testRequirement() $this->assertTrue($route->hasRequirement('foo'), '->hasRequirement() return true if requirement is set'); } + public function testRequirementAlternativeStartAndEndRegexSyntax() + { + $route = new Route('/{foo}'); + $route->setRequirement('foo', '\A\d+\z'); + $this->assertEquals('\d+', $route->getRequirement('foo'), '->setRequirement() removes \A and \z from the path'); + $this->assertTrue($route->hasRequirement('foo')); + } + /** * @dataProvider getInvalidRequirements */ @@ -139,6 +147,9 @@ public function getInvalidRequirements() ['^$'], ['^'], ['$'], + ['\A\z'], + ['\A'], + ['\z'], ]; } pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy