|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Effectra\Router; |
| 6 | + |
| 7 | +/** |
| 8 | + * Class RouteGroup |
| 9 | + * @package Effectra\Router |
| 10 | + */ |
| 11 | +class RouteGroup |
| 12 | +{ |
| 13 | + /** |
| 14 | + * RouteGroup constructor. |
| 15 | + * |
| 16 | + * @param string $prePath The prefix path for routes in this group. |
| 17 | + * @param string $controller The controller class name. |
| 18 | + * @param Route $router The Route instance for defining routes. |
| 19 | + */ |
| 20 | + public function __construct( |
| 21 | + protected string $prePath, |
| 22 | + protected string $controller, |
| 23 | + protected Route $router |
| 24 | + ) { |
| 25 | + } |
| 26 | + |
| 27 | + /** |
| 28 | + * Define a GET route. |
| 29 | + * |
| 30 | + * @param string $pattern The route pattern. |
| 31 | + * @param string $method The controller method to call. |
| 32 | + * @return void |
| 33 | + */ |
| 34 | + public function get(string $pattern, string $method): void |
| 35 | + { |
| 36 | + $this->router->get($this->prePath . $pattern, [$this->controller, $method]); |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Define a POST route. |
| 41 | + * |
| 42 | + * @param string $pattern The route pattern. |
| 43 | + * @param string $method The controller method to call. |
| 44 | + * @return void |
| 45 | + */ |
| 46 | + public function post(string $pattern, string $method): void |
| 47 | + { |
| 48 | + $this->router->post($this->prePath . $pattern, [$this->controller, $method]); |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Define a PUT route. |
| 53 | + * |
| 54 | + * @param string $pattern The route pattern. |
| 55 | + * @param string $method The controller method to call. |
| 56 | + * @return void |
| 57 | + */ |
| 58 | + public function put(string $pattern, string $method): void |
| 59 | + { |
| 60 | + $this->router->put($this->prePath . $pattern, [$this->controller, $method]); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Define a DELETE route. |
| 65 | + * |
| 66 | + * @param string $pattern The route pattern. |
| 67 | + * @param string $method The controller method to call. |
| 68 | + * @return void |
| 69 | + */ |
| 70 | + public function delete(string $pattern, string $method): void |
| 71 | + { |
| 72 | + $this->router->delete($this->prePath . $pattern, [$this->controller, $method]); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Define a PATCH route. |
| 77 | + * |
| 78 | + * @param string $pattern The route pattern. |
| 79 | + * @param string $method The controller method to call. |
| 80 | + * @return void |
| 81 | + */ |
| 82 | + public function patch(string $pattern, string $method): void |
| 83 | + { |
| 84 | + $this->router->patch($this->prePath . $pattern, [$this->controller, $method]); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Define an OPTIONS route. |
| 89 | + * |
| 90 | + * @param string $pattern The route pattern. |
| 91 | + * @param string $method The controller method to call. |
| 92 | + * @return void |
| 93 | + */ |
| 94 | + public function options(string $pattern, string $method): void |
| 95 | + { |
| 96 | + $this->router->options($this->prePath . $pattern, [$this->controller, $method]); |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Define a route that responds to any HTTP method. |
| 101 | + * |
| 102 | + * @param string $pattern The route pattern. |
| 103 | + * @param string $method The controller method to call. |
| 104 | + * @return void |
| 105 | + */ |
| 106 | + public function any(string $pattern, string $method): void |
| 107 | + { |
| 108 | + $this->router->any($this->prePath . $pattern, [$this->controller, $method]); |
| 109 | + } |
| 110 | +} |
0 commit comments