|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * |
| 5 | + * This file is part of simple-mvc-rest-api for PHP. |
| 6 | + * |
| 7 | + */ |
| 8 | +namespace Router; |
| 9 | + |
| 10 | +final class Route { |
| 11 | + |
| 12 | + /** |
| 13 | + * Http Method. |
| 14 | + * |
| 15 | + * @var string |
| 16 | + */ |
| 17 | + private $method; |
| 18 | + |
| 19 | + /** |
| 20 | + * The path for this route. |
| 21 | + * |
| 22 | + * @var string |
| 23 | + */ |
| 24 | + private $pattern; |
| 25 | + |
| 26 | + /** |
| 27 | + * The action, controller, callable. this route points to. |
| 28 | + * |
| 29 | + * @var mixed |
| 30 | + */ |
| 31 | + private $callback; |
| 32 | + |
| 33 | + /** |
| 34 | + * Allows these HTTP methods. |
| 35 | + * |
| 36 | + * @var array |
| 37 | + */ |
| 38 | + private $list_method = ['GET', 'POST', 'PUT', 'DELETE', 'OPTION']; |
| 39 | + |
| 40 | + /** |
| 41 | + * construct function |
| 42 | + */ |
| 43 | + public function __construct(String $method, String $pattern, $callback) { |
| 44 | + $this->method = $this->validateMethod(strtoupper($method)); |
| 45 | + $this->pattern = $pattern; |
| 46 | + $this->callback = $callback; |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * check valid method |
| 51 | + */ |
| 52 | + private function validateMethod(string $method) { |
| 53 | + if (in_array($method, $this->list_method)) |
| 54 | + return $method; |
| 55 | + |
| 56 | + throw new Exception('Invalid Method Name'); |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * get method |
| 61 | + */ |
| 62 | + public function getMethod() { |
| 63 | + return $this->method; |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * get pattern |
| 68 | + */ |
| 69 | + public function getPattern() { |
| 70 | + return $this->pattern; |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * get callback |
| 75 | + */ |
| 76 | + public function getCallback() { |
| 77 | + return $this->callback; |
| 78 | + } |
| 79 | +} |
0 commit comments