Skip to content

Commit f752eee

Browse files
zlodesfabpot
authored andcommitted
[Router] allow to use \A and \z as regex start and end
1 parent e411c96 commit f752eee

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

src/Symfony/Component/Routing/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ CHANGELOG
55
-----
66

77
* Added support for inline definition of requirements and defaults for host
8+
* Added support for `\A` and `\z` as regex start and end for route requirement
89

910
5.1.0
1011
-----

src/Symfony/Component/Routing/Route.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -553,12 +553,18 @@ private function extractInlineDefaultsAndRequirements(string $pattern): string
553553

554554
private function sanitizeRequirement(string $key, string $regex)
555555
{
556-
if ('' !== $regex && '^' === $regex[0]) {
557-
$regex = (string) substr($regex, 1); // returns false for a single character
556+
if ('' !== $regex) {
557+
if ('^' === $regex[0]) {
558+
$regex = substr($regex, 1);
559+
} elseif (0 === strpos($regex, '\\A')) {
560+
$regex = substr($regex, 2);
561+
}
558562
}
559563

560564
if ('$' === substr($regex, -1)) {
561565
$regex = substr($regex, 0, -1);
566+
} elseif (\strlen($regex) - 2 === strpos($regex, '\\z')) {
567+
$regex = substr($regex, 0, -2);
562568
}
563569

564570
if ('' === $regex) {

src/Symfony/Component/Routing/Tests/RouteTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,14 @@ public function testRequirement()
122122
$this->assertTrue($route->hasRequirement('foo'), '->hasRequirement() return true if requirement is set');
123123
}
124124

125+
public function testRequirementAlternativeStartAndEndRegexSyntax()
126+
{
127+
$route = new Route('/{foo}');
128+
$route->setRequirement('foo', '\A\d+\z');
129+
$this->assertEquals('\d+', $route->getRequirement('foo'), '->setRequirement() removes \A and \z from the path');
130+
$this->assertTrue($route->hasRequirement('foo'));
131+
}
132+
125133
/**
126134
* @dataProvider getInvalidRequirements
127135
*/
@@ -139,6 +147,9 @@ public function getInvalidRequirements()
139147
['^$'],
140148
['^'],
141149
['$'],
150+
['\A\z'],
151+
['\A'],
152+
['\z'],
142153
];
143154
}
144155

0 commit comments

Comments
 (0)
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