PageRenderTime 24ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/library/Zend/Navigation/Page/Mvc.php

https://github.com/mrbanzai/zf2
PHP | 409 lines | 160 code | 44 blank | 205 comment | 25 complexity | bab6a0087cf972bac9a90eb9a0916ac3 MD5 | raw file
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Navigation
  17. * @subpackage Page
  18. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. /**
  22. * @namespace
  23. */
  24. namespace Zend\Navigation\Page;
  25. use Zend\Mvc\Router\RouteMatch,
  26. Zend\Navigation\AbstractPage,
  27. Zend\Navigation\Exception,
  28. Zend\View\Helper\Url as UrlHelper;
  29. /**
  30. * Represents a page that is defined using module, controller, action, route
  31. * name and route params to assemble the href
  32. *
  33. * @category Zend
  34. * @package Zend_Navigation
  35. * @subpackage Page
  36. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  37. * @license http://framework.zend.com/license/new-bsd New BSD License
  38. */
  39. class Mvc extends AbstractPage
  40. {
  41. /**
  42. * Action name to use when assembling URL
  43. *
  44. * @var string
  45. */
  46. protected $action;
  47. /**
  48. * @var bool
  49. */
  50. protected $active = false;
  51. /**
  52. * Controller name to use when assembling URL
  53. *
  54. * @var string
  55. */
  56. protected $controller;
  57. /**
  58. * Params to use when assembling URL
  59. *
  60. * @see getHref()
  61. * @var array
  62. */
  63. protected $params = array();
  64. /**
  65. * Route name to use when assembling URL
  66. *
  67. * @see getHref()
  68. * @var string
  69. */
  70. protected $route;
  71. /**
  72. * Cached href
  73. *
  74. * The use of this variable minimizes execution time when getHref() is
  75. * called more than once during the lifetime of a request. If a property
  76. * is updated, the cache is invalidated.
  77. *
  78. * @var string
  79. */
  80. protected $hrefCache;
  81. /**
  82. * Route matches; used for routing parameters and testing validity
  83. *
  84. * @var RouteMatch
  85. */
  86. protected $routeMatch;
  87. /**
  88. * Action helper for assembling URLs
  89. *
  90. * @see getHref()
  91. * @var UrlHelper
  92. */
  93. protected $urlHelper = null;
  94. // Accessors:
  95. /**
  96. * Returns whether page should be considered active or not
  97. *
  98. * This method will compare the page properties against the route matches
  99. * composed in the object.
  100. *
  101. * @param bool $recursive [optional] whether page should be considered
  102. * active if any child pages are active. Default is
  103. * false.
  104. * @return bool whether page should be considered active or not
  105. */
  106. public function isActive($recursive = false)
  107. {
  108. if (!$this->active) {
  109. $reqParams = array();
  110. if ($this->routeMatch instanceof RouteMatch) {
  111. $reqParams = $this->routeMatch->getParams();
  112. }
  113. $myParams = $this->params;
  114. if (null !== $this->controller) {
  115. $myParams['controller'] = $this->controller;
  116. } else {
  117. /**
  118. * @todo In ZF1, this was configurable and pulled from the front controller
  119. */
  120. $myParams['controller'] = 'index';
  121. }
  122. if (null !== $this->action) {
  123. $myParams['action'] = $this->action;
  124. } else {
  125. /**
  126. * @todo In ZF1, this was configurable and pulled from the front controller
  127. */
  128. $myParams['action'] = 'action';
  129. }
  130. if (count(array_intersect_assoc($reqParams, $myParams)) ==
  131. count($myParams)) {
  132. $this->active = true;
  133. return true;
  134. }
  135. }
  136. return parent::isActive($recursive);
  137. }
  138. /**
  139. * Returns href for this page
  140. *
  141. * This method uses {@link Zend_Controller_Action_Helper_Url} to assemble
  142. * the href based on the page's properties.
  143. *
  144. * @return string page href
  145. */
  146. public function getHref()
  147. {
  148. if ($this->hrefCache) {
  149. return $this->hrefCache;
  150. }
  151. if (null === $this->urlHelper) {
  152. throw new Exception\DomainException(__METHOD__ . ' cannot execute as no Zend\View\Helper\Url instance is composed');
  153. }
  154. $params = $this->getParams();
  155. if (($param = $this->getController()) != null) {
  156. $params['controller'] = $param;
  157. }
  158. if (($param = $this->getAction()) != null) {
  159. $params['action'] = $param;
  160. }
  161. $helper = $this->urlHelper;
  162. $url = $helper(
  163. $this->getRoute(),
  164. $params
  165. );
  166. // Add the fragment identifier if it is set
  167. $fragment = $this->getFragment();
  168. if (null !== $fragment) {
  169. $url .= '#' . $fragment;
  170. }
  171. return $this->hrefCache = $url;
  172. }
  173. /**
  174. * Sets action name to use when assembling URL
  175. *
  176. * @see getHref()
  177. *
  178. * @param string $action action name
  179. * @return Mvc fluent interface, returns self
  180. * @throws Exception\InvalidArgumentException if invalid $action is given
  181. */
  182. public function setAction($action)
  183. {
  184. if (null !== $action && !is_string($action)) {
  185. throw new Exception\InvalidArgumentException(
  186. 'Invalid argument: $action must be a string or null');
  187. }
  188. $this->action = $action;
  189. $this->hrefCache = null;
  190. return $this;
  191. }
  192. /**
  193. * Returns action name to use when assembling URL
  194. *
  195. * @see getHref()
  196. *
  197. * @return string|null action name
  198. */
  199. public function getAction()
  200. {
  201. return $this->action;
  202. }
  203. /**
  204. * Sets controller name to use when assembling URL
  205. *
  206. * @see getHref()
  207. *
  208. * @param string|null $controller controller name
  209. * @return Mvc fluent interface, returns self
  210. * @throws Exception\InvalidArgumentException if invalid controller name is given
  211. */
  212. public function setController($controller)
  213. {
  214. if (null !== $controller && !is_string($controller)) {
  215. throw new Exception\InvalidArgumentException(
  216. 'Invalid argument: $controller must be a string or null');
  217. }
  218. $this->controller = $controller;
  219. $this->hrefCache = null;
  220. return $this;
  221. }
  222. /**
  223. * Returns controller name to use when assembling URL
  224. *
  225. * @see getHref()
  226. *
  227. * @return string|null controller name or null
  228. */
  229. public function getController()
  230. {
  231. return $this->controller;
  232. }
  233. /**
  234. * Sets module name to use when assembling URL
  235. *
  236. * @see getHref()
  237. *
  238. * @param string|null $module module name
  239. * @return Mvc fluent interface, returns self
  240. * @throws Exception\InvalidArgumentException if invalid module name is given
  241. */
  242. public function setModule($module)
  243. {
  244. if (null !== $module && !is_string($module)) {
  245. throw new Exception\InvalidArgumentException(
  246. 'Invalid argument: $module must be a string or null');
  247. }
  248. $this->module = $module;
  249. $this->hrefCache = null;
  250. return $this;
  251. }
  252. /**
  253. * Returns module name to use when assembling URL
  254. *
  255. * @see getHref()
  256. *
  257. * @return string|null module name or null
  258. */
  259. public function getModule()
  260. {
  261. return $this->module;
  262. }
  263. /**
  264. * Sets params to use when assembling URL
  265. *
  266. * @see getHref()
  267. *
  268. * @param array|null $params [optional] page params. Default is null
  269. * which sets no params.
  270. * @return \Zend\Navigation\Page\Mvc fluent interface, returns self
  271. */
  272. public function setParams(array $params = null)
  273. {
  274. if (null === $params) {
  275. $this->params = array();
  276. } else {
  277. // TODO: do this more intelligently?
  278. $this->params = $params;
  279. }
  280. $this->hrefCache = null;
  281. return $this;
  282. }
  283. /**
  284. * Returns params to use when assembling URL
  285. *
  286. * @see getHref()
  287. *
  288. * @return array page params
  289. */
  290. public function getParams()
  291. {
  292. return $this->params;
  293. }
  294. /**
  295. * Sets route name to use when assembling URL
  296. *
  297. * @see getHref()
  298. *
  299. * @param string $route route name to use when assembling URL
  300. * @return Mvc fluent interface, returns self
  301. * @throws Exception\InvalidArgumentException if invalid $route is given
  302. */
  303. public function setRoute($route)
  304. {
  305. if (null !== $route && (!is_string($route) || strlen($route) < 1)) {
  306. throw new Exception\InvalidArgumentException(
  307. 'Invalid argument: $route must be a non-empty string or null');
  308. }
  309. $this->route = $route;
  310. $this->hrefCache = null;
  311. return $this;
  312. }
  313. /**
  314. * Returns route name to use when assembling URL
  315. *
  316. * @see getHref()
  317. *
  318. * @return string route name
  319. */
  320. public function getRoute()
  321. {
  322. return $this->route;
  323. }
  324. /**
  325. * Set route match object from which parameters will be retrieved
  326. *
  327. * @param RouteMatch $matches
  328. * @return Mvc
  329. */
  330. public function setRouteMatch(RouteMatch $matches)
  331. {
  332. $this->routeMatch = $matches;
  333. return $this;
  334. }
  335. /**
  336. * Sets action helper for assembling URLs
  337. *
  338. * @see getHref()
  339. *
  340. * @param UrlHelper $uh URL plugin
  341. * @return Mvc
  342. */
  343. public function setUrlHelper(UrlHelper $helper)
  344. {
  345. $this->urlHelper = $helper;
  346. return $this;
  347. }
  348. // Public methods:
  349. /**
  350. * Returns an array representation of the page
  351. *
  352. * @return array associative array containing all page properties
  353. */
  354. public function toArray()
  355. {
  356. return array_merge(
  357. parent::toArray(),
  358. array(
  359. 'action' => $this->getAction(),
  360. 'controller' => $this->getController(),
  361. 'params' => $this->getParams(),
  362. 'route' => $this->getRoute(),
  363. ));
  364. }
  365. }