/src/Sonno/Configuration/Route.php

https://github.com/360i/sonno · PHP · 415 lines · 138 code · 40 blank · 237 comment · 3 complexity · c972f9f1d3d1bb42e79a477a74a1a14e MD5 · raw file

  1. <?php
  2. /**
  3. * @category Sonno
  4. * @package Sonno\Configuration
  5. * @author Dave Hauenstein <davehauenstein@gmail.com>
  6. * @author Tharsan Bhuvanendran <me@tharsan.com>
  7. * @author 360i <sonno@360i.com>
  8. * @copyright Copyright (c) 2011 360i LLC (http://360i.com)
  9. * @license http://sonno.360i.com/LICENSE.txt New BSD License
  10. */
  11. namespace Sonno\Configuration;
  12. /**
  13. * An immutable class to represent a route. A route is unique and defined by
  14. * four pieces of data: Path, Http Method, Produces, and Consumes.
  15. *
  16. * @category Sonno
  17. * @package Sonno\Configuration
  18. * @author Dave Hauenstein <davehauenstein@gmail.com>
  19. */
  20. class Route
  21. {
  22. /**
  23. * Class to instantiate for a particular route/http method.
  24. *
  25. * @var string
  26. */
  27. protected $_resourceClassName;
  28. /**
  29. * Key value pairs of instance-variable => context-type. Available context
  30. * types are:
  31. * - Request: An instance of Sonno\Http\Request\Request representing the
  32. * current http request.
  33. *
  34. * @var array
  35. */
  36. protected $_contexts = array();
  37. /**
  38. * Method to call for a particular route/http method.
  39. *
  40. * @var string
  41. */
  42. protected $_resourceMethodName;
  43. /**
  44. * Path to match a class#method to
  45. * (segment extracted from the class level).
  46. *
  47. * @var string
  48. */
  49. protected $_classPath;
  50. /**
  51. * Path to match a class#method to
  52. * (segment extracted from the method level).
  53. *
  54. * @var string
  55. */
  56. protected $_methodPath;
  57. /**
  58. * @see \Sonno\Annotation\HttpMethod
  59. * @see \Sonno\Annotation\DELETE
  60. * @see \Sonno\Annotation\GET
  61. * @see \Sonno\Annotation\HEAD
  62. * @see \Sonno\Annotation\OPTIONS
  63. * @see \Sonno\Annotation\POST
  64. * @see \Sonno\Annotation\PUT
  65. * @var string
  66. */
  67. protected $_httpMethod;
  68. /**
  69. * @see \Sonno\Annotation\Consumes
  70. * @var string
  71. */
  72. protected $_consumes = array();
  73. /**
  74. * @see \Sonno\Annotation\Produces
  75. * @var string
  76. */
  77. protected $_produces = array();
  78. /**
  79. * @see \Sonno\Annotation\CookieParam
  80. * @var array<string>
  81. */
  82. protected $_cookieParams = array();
  83. /**
  84. * @see \Sonno\Annotation\FormParam
  85. * @var array<string>
  86. */
  87. protected $_formParams = array();
  88. /**
  89. * @see \Sonno\Annotation\HeaderParam
  90. * @var array<string>
  91. */
  92. protected $_headerParams = array();
  93. /**
  94. * @see \Sonno\Annotation\PathParam
  95. * @var array<string>
  96. */
  97. protected $_pathParams = array();
  98. /**
  99. * @see \Sonno\Annotation\QueryParam
  100. * @var array<string>
  101. */
  102. protected $_queryParams = array();
  103. /**
  104. * Construct a new Route configuration object.
  105. *
  106. * @param $params array
  107. */
  108. public function __construct($params)
  109. {
  110. $this->_setParams($params);
  111. }
  112. /**
  113. * Getter for _classPath property.
  114. *
  115. * @return string _classPath property.
  116. */
  117. public function getClassPath()
  118. {
  119. return $this->_classPath;
  120. }
  121. /**
  122. * Getter for _methodPath property.
  123. *
  124. * @return string _methodPath property.
  125. */
  126. public function getMethodPath()
  127. {
  128. return $this->_methodPath;
  129. }
  130. /**
  131. * Getter for the complete Path value.
  132. *
  133. * @return string The complete path.
  134. */
  135. public function getPath()
  136. {
  137. return $this->_classPath . $this->_methodPath;
  138. }
  139. /**
  140. * Getter for _httpMethod property.
  141. *
  142. * @return string _httpMethod property.
  143. */
  144. public function getHttpMethod()
  145. {
  146. return $this->_httpMethod;
  147. }
  148. /**
  149. * Getter for _consumes property.
  150. *
  151. * @return array _consumes property.
  152. */
  153. public function getConsumes()
  154. {
  155. return $this->_consumes;
  156. }
  157. /**
  158. * Getter for _produces property.
  159. *
  160. * @return array _produces property.
  161. */
  162. public function getProduces()
  163. {
  164. return $this->_produces;
  165. }
  166. /**
  167. * Getter for _contexts property.
  168. *
  169. * @return array _contexts property.
  170. */
  171. public function getContexts()
  172. {
  173. return $this->_contexts;
  174. }
  175. /**
  176. * Getter for _cookieParams property.
  177. *
  178. * @return array _cookieParams property.
  179. */
  180. public function getCookieParams()
  181. {
  182. return $this->_cookieParams;
  183. }
  184. /**
  185. * Getter for _formParams property.
  186. *
  187. * @return array _formParams property.
  188. */
  189. public function getFormParams()
  190. {
  191. return $this->_formParams;
  192. }
  193. /**
  194. * Getter for _headerParams property.
  195. *
  196. * @return array _headerParams property.
  197. */
  198. public function getHeaderParams()
  199. {
  200. return $this->_headerParams;
  201. }
  202. /**
  203. * Getter for _pathParams property.
  204. *
  205. * @return array _pathParams property.
  206. */
  207. public function getPathParams()
  208. {
  209. return $this->_pathParams;
  210. }
  211. /**
  212. * Getter for _queryParams property.
  213. *
  214. * @return array _queryParams property.
  215. */
  216. public function getQueryParams()
  217. {
  218. return $this->_queryParams;
  219. }
  220. /**
  221. * Getter for _resourceClassName property.
  222. *
  223. * @return string _resourceClassName property.
  224. */
  225. public function getResourceClassName()
  226. {
  227. return $this->_resourceClassName;
  228. }
  229. /**
  230. * Getter for _resourceMethodName property.
  231. *
  232. * @return string _resourceMethodName property.
  233. */
  234. public function getResourceMethodName()
  235. {
  236. return $this->_resourceMethodName;
  237. }
  238. /**
  239. * Setter for _classPath property. Will trim trailing forward slash '/'
  240. * and ensure a forward slash '/' exists at the beginning of the string.
  241. *
  242. * @param $path string Value for $_classPath property.
  243. * @return \Sonno\Configuration\Route Implements fluent interface.
  244. */
  245. protected function _setClassPath($path)
  246. {
  247. $this->_classPath = '/' . trim($path, '/');
  248. return $this;
  249. }
  250. /**
  251. * Setter for _methodPath property. Will trim trailing forward slash '/'
  252. * and ensure a forward slash '/' exists at the beginning of the string.
  253. *
  254. * @param $path string Value for $_methodPath property.
  255. * @return \Sonno\Configuration\Route Implements fluent interface.
  256. */
  257. protected function _setMethodPath($path)
  258. {
  259. $this->_methodPath = '/' . trim($path, '/');
  260. return $this;
  261. }
  262. /**
  263. * Setter for _httpMethod property. Will ensure that http method is
  264. * uppercase.
  265. *
  266. * @param $httpMethod string Value for $_httpMethod property.
  267. * @return \Sonno\Configuration\Route Implements fluent interface.
  268. */
  269. protected function _setHttpMethod($httpMethod)
  270. {
  271. $this->_httpMethod = strtoupper($httpMethod);
  272. return $this;
  273. }
  274. /**
  275. * Setter for _contexts property. Will ensure that contexts are always
  276. * an array.
  277. *
  278. * @param $contexts array Value for $_contexts property.
  279. * @return \Sonno\Configuration\Route Implements fluent interface.
  280. */
  281. protected function _setContexts(array $contexts)
  282. {
  283. $this->_contexts = $contexts;
  284. return $this;
  285. }
  286. /**
  287. * Setter for _cookieParams property.
  288. *
  289. * @param $cookieParams array Value for $_cookieParams property.
  290. * @return \Sonno\Configuration\Route Implements fluent interface.
  291. */
  292. protected function _setCookieParams(array $cookieParams)
  293. {
  294. $this->_cookieParams = $cookieParams;
  295. return $this;
  296. }
  297. /**
  298. * Setter for _formParams property.
  299. *
  300. * @param $formParams array Value for $_formParams property.
  301. * @return \Sonno\Configuration\Route Implements fluent interface.
  302. */
  303. protected function _setFormParams(array $formParams)
  304. {
  305. $this->_formParams = $formParams;
  306. return $this;
  307. }
  308. /**
  309. * Setter for _headerParams property.
  310. *
  311. * @param $headerParams array Value for $_headerParams property.
  312. * @return \Sonno\Configuration\Route Implements fluent interface.
  313. */
  314. protected function _setHeaderParams(array $headerParams)
  315. {
  316. $this->_headerParams = $headerParams;
  317. return $this;
  318. }
  319. /**
  320. * Setter for _pathParams property.
  321. *
  322. * @param $pathParams array Value for $_pathParams property.
  323. * @return \Sonno\Configuration\Route Implements fluent interface.
  324. */
  325. protected function _setPathParams(array $pathParams)
  326. {
  327. $this->_pathParams = $pathParams;
  328. return $this;
  329. }
  330. /**
  331. * Setter for _queryParams property.
  332. *
  333. * @param $queryParams array Value for $_queryParams property.
  334. * @return \Sonno\Configuration\Route Implements fluent interface.
  335. */
  336. protected function _setQueryParams(array $queryParams)
  337. {
  338. $this->_queryParams = $queryParams;
  339. return $this;
  340. }
  341. /**
  342. * Setter for all properties. Supported params are:
  343. *
  344. * - classPath
  345. * - methodPath
  346. * - contexts
  347. * - method
  348. * - httpMethod
  349. * - consumes
  350. * - produces
  351. * - cookieParams
  352. * - formParams
  353. * - headerParams
  354. * - pathParams
  355. * - queryParams
  356. *
  357. * @param $params array Key/Value pairs for all other params.
  358. * @return \Sonno\Configuration\Route Implements fluent interface.
  359. */
  360. protected function _setParams($params)
  361. {
  362. foreach ($params as $key => $val) {
  363. $prop = '_' . $key;
  364. if (property_exists($this, $prop)) {
  365. $method = '_set' . ucfirst($key);
  366. if (method_exists($this, $method)) {
  367. $this->{$method}($val);
  368. } else {
  369. $this->{$prop} = $val;
  370. }
  371. }
  372. }
  373. return $this;
  374. }
  375. }