|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Fady\Routing; |
| 4 | + |
| 5 | +/** |
| 6 | + * Class Route |
| 7 | + * @package Fady\Routing |
| 8 | + */ |
| 9 | +class Route |
| 10 | +{ |
| 11 | + /** |
| 12 | + * @var string |
| 13 | + */ |
| 14 | + protected $action; |
| 15 | + |
| 16 | + /** |
| 17 | + * @var string |
| 18 | + */ |
| 19 | + protected $controller; |
| 20 | + |
| 21 | + /** |
| 22 | + * @var string |
| 23 | + */ |
| 24 | + protected $url; |
| 25 | + |
| 26 | + /** |
| 27 | + * @var string |
| 28 | + */ |
| 29 | + protected $routeName; |
| 30 | + |
| 31 | + /** |
| 32 | + * @var array |
| 33 | + */ |
| 34 | + protected $varsNames = []; |
| 35 | + |
| 36 | + /** |
| 37 | + * @var array |
| 38 | + */ |
| 39 | + protected $vars = []; |
| 40 | + |
| 41 | + /** |
| 42 | + * Route constructor. |
| 43 | + * @param $url |
| 44 | + * @param $controller |
| 45 | + * @param $action |
| 46 | + * @param array $varsNames |
| 47 | + */ |
| 48 | + public function __construct($url, $controller, $action, array $varsNames = []) |
| 49 | + { |
| 50 | + $this->setUrl($url) |
| 51 | + ->setController($controller) |
| 52 | + ->setAction($action) |
| 53 | + ->setVarsNames($varsNames); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * @return bool |
| 58 | + */ |
| 59 | + public function hasVars() |
| 60 | + { |
| 61 | + return !empty($this->varsNames); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * @param $url |
| 66 | + * @return null| |
| 67 | + */ |
| 68 | + public function match($url) |
| 69 | + { |
| 70 | + if (preg_match('`^' . $this->url . '$`', $url, $matches)) { |
| 71 | + return $matches; |
| 72 | + } |
| 73 | + return null; |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * @param string $action |
| 78 | + * @return Route |
| 79 | + */ |
| 80 | + public function setAction(string $action) |
| 81 | + { |
| 82 | + if (is_string($action)) { |
| 83 | + $this->action = $action; |
| 84 | + } |
| 85 | + return $this; |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * @param string $controller |
| 90 | + * @return Route |
| 91 | + */ |
| 92 | + public function setController(string $controller) |
| 93 | + { |
| 94 | + if (is_string($controller)) { |
| 95 | + $this->controller = $controller; |
| 96 | + } |
| 97 | + return $this; |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * @param string $url |
| 102 | + * @return Route |
| 103 | + */ |
| 104 | + public function setUrl(string $url) |
| 105 | + { |
| 106 | + if (is_string($url)) { |
| 107 | + $this->url = $url; |
| 108 | + } |
| 109 | + return $this; |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * @param array $varsNames |
| 114 | + * @return Route |
| 115 | + */ |
| 116 | + public function setVarsNames(array $varsNames) |
| 117 | + { |
| 118 | + $this->varsNames = $varsNames; |
| 119 | + return $this; |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * @param array $vars |
| 124 | + * @return Route |
| 125 | + */ |
| 126 | + public function setVars(array $vars) |
| 127 | + { |
| 128 | + $this->vars = $vars; |
| 129 | + return $this; |
| 130 | + } |
| 131 | + |
| 132 | + /** |
| 133 | + * @return string |
| 134 | + */ |
| 135 | + public function getAction() |
| 136 | + { |
| 137 | + return $this->action; |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * @return string |
| 142 | + */ |
| 143 | + public function getController() |
| 144 | + { |
| 145 | + return $this->controller; |
| 146 | + } |
| 147 | + |
| 148 | + /** |
| 149 | + * @return array |
| 150 | + */ |
| 151 | + public function getVars() |
| 152 | + { |
| 153 | + return $this->vars; |
| 154 | + } |
| 155 | + |
| 156 | + /** |
| 157 | + * @return array |
| 158 | + */ |
| 159 | + public function getVarsNames() |
| 160 | + { |
| 161 | + return $this->varsNames; |
| 162 | + } |
| 163 | + |
| 164 | + /** |
| 165 | + * @return mixed |
| 166 | + */ |
| 167 | + public function getRouteName() |
| 168 | + { |
| 169 | + return $this->routeName; |
| 170 | + } |
| 171 | + |
| 172 | + /** |
| 173 | + * @param string $routeName |
| 174 | + * @return Route |
| 175 | + */ |
| 176 | + public function setRouteName(string $routeName) |
| 177 | + { |
| 178 | + $this->routeName = $routeName; |
| 179 | + return $this; |
| 180 | + } |
| 181 | + |
| 182 | + |
| 183 | +} |
0 commit comments