File tree Expand file tree Collapse file tree 3 files changed +20
-2
lines changed Expand file tree Collapse file tree 3 files changed +20
-2
lines changed Original file line number Diff line number Diff line change @@ -5,6 +5,7 @@ CHANGELOG
5
5
-----
6
6
7
7
* 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
8
9
9
10
5.1.0
10
11
-----
Original file line number Diff line number Diff line change @@ -553,12 +553,18 @@ private function extractInlineDefaultsAndRequirements(string $pattern): string
553
553
554
554
private function sanitizeRequirement (string $ key , string $ regex )
555
555
{
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
+ }
558
562
}
559
563
560
564
if ('$ ' === substr ($ regex , -1 )) {
561
565
$ regex = substr ($ regex , 0 , -1 );
566
+ } elseif (\strlen ($ regex ) - 2 === strpos ($ regex , '\\z ' )) {
567
+ $ regex = substr ($ regex , 0 , -2 );
562
568
}
563
569
564
570
if ('' === $ regex ) {
Original file line number Diff line number Diff line change @@ -122,6 +122,14 @@ public function testRequirement()
122
122
$ this ->assertTrue ($ route ->hasRequirement ('foo ' ), '->hasRequirement() return true if requirement is set ' );
123
123
}
124
124
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
+
125
133
/**
126
134
* @dataProvider getInvalidRequirements
127
135
*/
@@ -139,6 +147,9 @@ public function getInvalidRequirements()
139
147
['^$ ' ],
140
148
['^ ' ],
141
149
['$ ' ],
150
+ ['\A\z ' ],
151
+ ['\A ' ],
152
+ ['\z ' ],
142
153
];
143
154
}
144
155
You can’t perform that action at this time.
0 commit comments