Skip to content

Commit bea54e6

Browse files
committed
[Routing] Add Requirement::POSITIVE_INT for common ids and pagination
1 parent a057029 commit bea54e6

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

src/Symfony/Component/Routing/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
6.2
5+
---
6+
7+
* Add `Requirement::POSITIVE_INT` for common ids and pagination
8+
49
6.1
510
---
611

src/Symfony/Component/Routing/Requirement/Requirement.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ enum Requirement
2020
public const CATCH_ALL = '.+';
2121
public const DATE_YMD = '[0-9]{4}-(?:0[1-9]|1[012])-(?:0[1-9]|[12][0-9]|(?<!02-)3[01])'; // YYYY-MM-DD
2222
public const DIGITS = '[0-9]+';
23+
public const POSITIVE_INT = '[1-9][0-9]*';
2324
public const UID_BASE32 = '[0-9A-HJKMNP-TV-Z]{26}';
2425
public const UID_BASE58 = '[1-9A-HJ-NP-Za-km-z]{22}';
2526
public const UID_RFC4122 = '[0-9a-f]{8}(?:-[0-9a-f]{4}){3}-[0-9a-f]{12}';

src/Symfony/Component/Routing/Tests/Requirement/RequirementTest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ public function testDateYmdKO(string $date)
109109

110110
/**
111111
* @testWith ["0"]
112+
* ["012"]
112113
* ["1"]
113114
* ["42"]
114115
* ["42198"]
@@ -136,6 +137,36 @@ public function testDigitsKO(string $digits)
136137
);
137138
}
138139

140+
/**
141+
* @testWith ["1"]
142+
* ["42"]
143+
* ["42198"]
144+
* ["999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999"]
145+
*/
146+
public function testPositiveIntOK(string $digits)
147+
{
148+
$this->assertMatchesRegularExpression(
149+
(new Route('/{digits}', [], ['digits' => Requirement::POSITIVE_INT]))->compile()->getRegex(),
150+
'/'.$digits,
151+
);
152+
}
153+
154+
/**
155+
* @testWith [""]
156+
* ["0"]
157+
* ["045"]
158+
* ["foo"]
159+
* ["-1"]
160+
* ["3.14"]
161+
*/
162+
public function testPositiveIntKO(string $digits)
163+
{
164+
$this->assertDoesNotMatchRegularExpression(
165+
(new Route('/{digits}', [], ['digits' => Requirement::POSITIVE_INT]))->compile()->getRegex(),
166+
'/'.$digits,
167+
);
168+
}
169+
139170
/**
140171
* @testWith ["00000000000000000000000000"]
141172
* ["ZZZZZZZZZZZZZZZZZZZZZZZZZZ"]

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