@@ -38,7 +38,12 @@ final class Route
3838 /**
3939 * @var array<string>
4040 */
41- private $ attributes = [];
41+ private array $ attributes = [];
42+
43+ /**
44+ * @var array<string, string>
45+ */
46+ private array $ wheres = [];
4247
4348 /**
4449 * Route constructor.
@@ -51,7 +56,7 @@ final class Route
5156 * ]
5257 * @param array $methods
5358 */
54- public function __construct (string $ name , string $ path , $ handler , array $ methods = ['GET ' ])
59+ public function __construct (string $ name , string $ path , $ handler , array $ methods = ['GET ' , ' HEAD ' ])
5560 {
5661 if ($ methods === []) {
5762 throw new InvalidArgumentException ('HTTP methods argument was empty; must contain at least one method ' );
@@ -60,6 +65,10 @@ public function __construct(string $name, string $path, $handler, array $methods
6065 $ this ->path = Helper::trimPath ($ path );
6166 $ this ->handler = $ handler ;
6267 $ this ->methods = $ methods ;
68+
69+ if (in_array ('GET ' , $ this ->methods ) && !in_array ('HEAD ' , $ this ->methods )) {
70+ $ this ->methods [] = 'HEAD ' ;
71+ }
6372 }
6473
6574 public function match (string $ path ): bool
@@ -70,16 +79,22 @@ public function match(string $path): bool
7079 $ regex = str_replace ($ variable , '(?P< ' . $ varName . '>[^/]++) ' , $ regex );
7180 }
7281
73- if (preg_match ('#^ ' . $ regex . '$#sD ' , Helper::trimPath ($ path ), $ matches )) {
74- $ values = array_filter ($ matches , static function ($ key ) {
75- return is_string ($ key );
76- }, ARRAY_FILTER_USE_KEY );
77- foreach ($ values as $ key => $ value ) {
78- $ this ->attributes [$ key ] = $ value ;
82+ if (!preg_match ('#^ ' . $ regex . '$#sD ' , Helper::trimPath ($ path ), $ matches )) {
83+ return false ;
84+ }
85+
86+ $ values = array_filter ($ matches , static function ($ key ) {
87+ return is_string ($ key );
88+ }, ARRAY_FILTER_USE_KEY );
89+
90+ foreach ($ values as $ key => $ value ) {
91+ if (array_key_exists ($ key , $ this ->wheres ) && !preg_match ('/^ ' .$ this ->wheres [$ key ].'$/ ' , $ value )) {
92+ return false ;
7993 }
80- return true ;
94+ $ this -> attributes [ $ key ] = $ value ;
8195 }
82- return false ;
96+
97+ return true ;
8398 }
8499
85100 public function getName (): string
@@ -120,4 +135,41 @@ public function getAttributes(): array
120135 {
121136 return $ this ->attributes ;
122137 }
138+
139+ public function whereNumber (...$ parameters ): self
140+ {
141+ $ this ->assignExprToParameters ($ parameters , '[0-9]+ ' );
142+ return $ this ;
143+ }
144+
145+ public function whereSlug (...$ parameters ): self
146+ {
147+ $ this ->assignExprToParameters ($ parameters , '[a-z0-9-]+ ' );
148+ return $ this ;
149+ }
150+
151+ public function whereAlphaNumeric (...$ parameters ): self
152+ {
153+ $ this ->assignExprToParameters ($ parameters , '[a-zA-Z0-9]+ ' );
154+ return $ this ;
155+ }
156+
157+ public function whereAlpha (...$ parameters ): self
158+ {
159+ $ this ->assignExprToParameters ($ parameters , '[a-zA-Z]+ ' );
160+ return $ this ;
161+ }
162+
163+ public function where (string $ parameter , string $ expression ): self
164+ {
165+ $ this ->wheres [$ parameter ] = $ expression ;
166+ return $ this ;
167+ }
168+
169+ private function assignExprToParameters (array $ parameters , string $ expression ): void
170+ {
171+ foreach ($ parameters as $ parameter ) {
172+ $ this ->where ($ parameter , $ expression );
173+ }
174+ }
123175}
0 commit comments