PageRenderTime 73ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/zfs/library/Zend/Navigation/Container.php

https://github.com/razvanp/zfs
PHP | 503 lines | 227 code | 54 blank | 222 comment | 25 complexity | 0aa989e71cdb05718a535fb8d9db0b29 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. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id: Container.php 23775 2011-03-01 17:25:24Z ralph $
  20. */
  21. /**
  22. * Zend_Navigation_Container
  23. *
  24. * Container class for Zend_Navigation_Page classes.
  25. *
  26. * @category Zend
  27. * @package Zend_Navigation
  28. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  29. * @license http://framework.zend.com/license/new-bsd New BSD License
  30. */
  31. abstract class Zend_Navigation_Container implements RecursiveIterator, Countable
  32. {
  33. /**
  34. * Contains sub pages
  35. *
  36. * @var array
  37. */
  38. protected $_pages = array();
  39. /**
  40. * An index that contains the order in which to iterate pages
  41. *
  42. * @var array
  43. */
  44. protected $_index = array();
  45. /**
  46. * Whether index is dirty and needs to be re-arranged
  47. *
  48. * @var bool
  49. */
  50. protected $_dirtyIndex = false;
  51. // Internal methods:
  52. /**
  53. * Sorts the page index according to page order
  54. *
  55. * @return void
  56. */
  57. protected function _sort()
  58. {
  59. if ($this->_dirtyIndex) {
  60. $newIndex = array();
  61. $index = 0;
  62. foreach ($this->_pages as $hash => $page) {
  63. $order = $page->getOrder();
  64. if ($order === null) {
  65. $newIndex[$hash] = $index;
  66. $index++;
  67. } else {
  68. $newIndex[$hash] = $order;
  69. }
  70. }
  71. asort($newIndex);
  72. $this->_index = $newIndex;
  73. $this->_dirtyIndex = false;
  74. }
  75. }
  76. // Public methods:
  77. /**
  78. * Notifies container that the order of pages are updated
  79. *
  80. * @return void
  81. */
  82. public function notifyOrderUpdated()
  83. {
  84. $this->_dirtyIndex = true;
  85. }
  86. /**
  87. * Adds a page to the container
  88. *
  89. * This method will inject the container as the given page's parent by
  90. * calling {@link Zend_Navigation_Page::setParent()}.
  91. *
  92. * @param Zend_Navigation_Page|array|Zend_Config $page page to add
  93. * @return Zend_Navigation_Container fluent interface,
  94. * returns self
  95. * @throws Zend_Navigation_Exception if page is invalid
  96. */
  97. public function addPage($page)
  98. {
  99. if ($page === $this) {
  100. require_once 'Zend/Navigation/Exception.php';
  101. throw new Zend_Navigation_Exception(
  102. 'A page cannot have itself as a parent');
  103. }
  104. if (is_array($page) || $page instanceof Zend_Config) {
  105. require_once 'Zend/Navigation/Page.php';
  106. $page = Zend_Navigation_Page::factory($page);
  107. } elseif (!$page instanceof Zend_Navigation_Page) {
  108. require_once 'Zend/Navigation/Exception.php';
  109. throw new Zend_Navigation_Exception(
  110. 'Invalid argument: $page must be an instance of ' .
  111. 'Zend_Navigation_Page or Zend_Config, or an array');
  112. }
  113. $hash = $page->hashCode();
  114. if (array_key_exists($hash, $this->_index)) {
  115. // page is already in container
  116. return $this;
  117. }
  118. // adds page to container and sets dirty flag
  119. $this->_pages[$hash] = $page;
  120. $this->_index[$hash] = $page->getOrder();
  121. $this->_dirtyIndex = true;
  122. // inject self as page parent
  123. $page->setParent($this);
  124. return $this;
  125. }
  126. /**
  127. * Adds several pages at once
  128. *
  129. * @param array|Zend_Config $pages pages to add
  130. * @return Zend_Navigation_Container fluent interface, returns self
  131. * @throws Zend_Navigation_Exception if $pages is not array or Zend_Config
  132. */
  133. public function addPages($pages)
  134. {
  135. if ($pages instanceof Zend_Config) {
  136. $pages = $pages->toArray();
  137. }
  138. if (!is_array($pages)) {
  139. require_once 'Zend/Navigation/Exception.php';
  140. throw new Zend_Navigation_Exception(
  141. 'Invalid argument: $pages must be an array or an ' .
  142. 'instance of Zend_Config');
  143. }
  144. foreach ($pages as $page) {
  145. $this->addPage($page);
  146. }
  147. return $this;
  148. }
  149. /**
  150. * Sets pages this container should have, removing existing pages
  151. *
  152. * @param array $pages pages to set
  153. * @return Zend_Navigation_Container fluent interface, returns self
  154. */
  155. public function setPages(array $pages)
  156. {
  157. $this->removePages();
  158. return $this->addPages($pages);
  159. }
  160. /**
  161. * Returns pages in the container
  162. *
  163. * @return array array of Zend_Navigation_Page instances
  164. */
  165. public function getPages()
  166. {
  167. return $this->_pages;
  168. }
  169. /**
  170. * Removes the given page from the container
  171. *
  172. * @param Zend_Navigation_Page|int $page page to remove, either a page
  173. * instance or a specific page order
  174. * @return bool whether the removal was
  175. * successful
  176. */
  177. public function removePage($page)
  178. {
  179. if ($page instanceof Zend_Navigation_Page) {
  180. $hash = $page->hashCode();
  181. } elseif (is_int($page)) {
  182. $this->_sort();
  183. if (!$hash = array_search($page, $this->_index)) {
  184. return false;
  185. }
  186. } else {
  187. return false;
  188. }
  189. if (isset($this->_pages[$hash])) {
  190. unset($this->_pages[$hash]);
  191. unset($this->_index[$hash]);
  192. $this->_dirtyIndex = true;
  193. return true;
  194. }
  195. return false;
  196. }
  197. /**
  198. * Removes all pages in container
  199. *
  200. * @return Zend_Navigation_Container fluent interface, returns self
  201. */
  202. public function removePages()
  203. {
  204. $this->_pages = array();
  205. $this->_index = array();
  206. return $this;
  207. }
  208. /**
  209. * Checks if the container has the given page
  210. *
  211. * @param Zend_Navigation_Page $page page to look for
  212. * @param bool $recursive [optional] whether to search
  213. * recursively. Default is false.
  214. * @return bool whether page is in container
  215. */
  216. public function hasPage(Zend_Navigation_Page $page, $recursive = false)
  217. {
  218. if (array_key_exists($page->hashCode(), $this->_index)) {
  219. return true;
  220. } elseif ($recursive) {
  221. foreach ($this->_pages as $childPage) {
  222. if ($childPage->hasPage($page, true)) {
  223. return true;
  224. }
  225. }
  226. }
  227. return false;
  228. }
  229. /**
  230. * Returns true if container contains any pages
  231. *
  232. * @return bool whether container has any pages
  233. */
  234. public function hasPages()
  235. {
  236. return count($this->_index) > 0;
  237. }
  238. /**
  239. * Returns a child page matching $property == $value, or null if not found
  240. *
  241. * @param string $property name of property to match against
  242. * @param mixed $value value to match property against
  243. * @return Zend_Navigation_Page|null matching page or null
  244. */
  245. public function findOneBy($property, $value)
  246. {
  247. $iterator = new RecursiveIteratorIterator($this,
  248. RecursiveIteratorIterator::SELF_FIRST);
  249. foreach ($iterator as $page) {
  250. if ($page->get($property) == $value) {
  251. return $page;
  252. }
  253. }
  254. return null;
  255. }
  256. /**
  257. * Returns all child pages matching $property == $value, or an empty array
  258. * if no pages are found
  259. *
  260. * @param string $property name of property to match against
  261. * @param mixed $value value to match property against
  262. * @return array array containing only Zend_Navigation_Page
  263. * instances
  264. */
  265. public function findAllBy($property, $value)
  266. {
  267. $found = array();
  268. $iterator = new RecursiveIteratorIterator($this,
  269. RecursiveIteratorIterator::SELF_FIRST);
  270. foreach ($iterator as $page) {
  271. if ($page->get($property) == $value) {
  272. $found[] = $page;
  273. }
  274. }
  275. return $found;
  276. }
  277. /**
  278. * Returns page(s) matching $property == $value
  279. *
  280. * @param string $property name of property to match against
  281. * @param mixed $value value to match property against
  282. * @param bool $all [optional] whether an array of all matching
  283. * pages should be returned, or only the first.
  284. * If true, an array will be returned, even if not
  285. * matching pages are found. If false, null will
  286. * be returned if no matching page is found.
  287. * Default is false.
  288. * @return Zend_Navigation_Page|null matching page or null
  289. */
  290. public function findBy($property, $value, $all = false)
  291. {
  292. if ($all) {
  293. return $this->findAllBy($property, $value);
  294. } else {
  295. return $this->findOneBy($property, $value);
  296. }
  297. }
  298. /**
  299. * Magic overload: Proxy calls to finder methods
  300. *
  301. * Examples of finder calls:
  302. * <code>
  303. * // METHOD // SAME AS
  304. * $nav->findByLabel('foo'); // $nav->findOneBy('label', 'foo');
  305. * $nav->findOneByLabel('foo'); // $nav->findOneBy('label', 'foo');
  306. * $nav->findAllByClass('foo'); // $nav->findAllBy('class', 'foo');
  307. * </code>
  308. *
  309. * @param string $method method name
  310. * @param array $arguments method arguments
  311. * @throws Zend_Navigation_Exception if method does not exist
  312. */
  313. public function __call($method, $arguments)
  314. {
  315. if (@preg_match('/(find(?:One|All)?By)(.+)/', $method, $match)) {
  316. return $this->{$match[1]}($match[2], $arguments[0]);
  317. }
  318. require_once 'Zend/Navigation/Exception.php';
  319. throw new Zend_Navigation_Exception(sprintf(
  320. 'Bad method call: Unknown method %s::%s',
  321. get_class($this),
  322. $method));
  323. }
  324. /**
  325. * Returns an array representation of all pages in container
  326. *
  327. * @return array
  328. */
  329. public function toArray()
  330. {
  331. $pages = array();
  332. $this->_dirtyIndex = true;
  333. $this->_sort();
  334. $indexes = array_keys($this->_index);
  335. foreach ($indexes as $hash) {
  336. $pages[] = $this->_pages[$hash]->toArray();
  337. }
  338. return $pages;
  339. }
  340. // RecursiveIterator interface:
  341. /**
  342. * Returns current page
  343. *
  344. * Implements RecursiveIterator interface.
  345. *
  346. * @return Zend_Navigation_Page current page or null
  347. * @throws Zend_Navigation_Exception if the index is invalid
  348. */
  349. public function current()
  350. {
  351. $this->_sort();
  352. current($this->_index);
  353. $hash = key($this->_index);
  354. if (isset($this->_pages[$hash])) {
  355. return $this->_pages[$hash];
  356. } else {
  357. require_once 'Zend/Navigation/Exception.php';
  358. throw new Zend_Navigation_Exception(
  359. 'Corruption detected in container; ' .
  360. 'invalid key found in internal iterator');
  361. }
  362. }
  363. /**
  364. * Returns hash code of current page
  365. *
  366. * Implements RecursiveIterator interface.
  367. *
  368. * @return string hash code of current page
  369. */
  370. public function key()
  371. {
  372. $this->_sort();
  373. return key($this->_index);
  374. }
  375. /**
  376. * Moves index pointer to next page in the container
  377. *
  378. * Implements RecursiveIterator interface.
  379. *
  380. * @return void
  381. */
  382. public function next()
  383. {
  384. $this->_sort();
  385. next($this->_index);
  386. }
  387. /**
  388. * Sets index pointer to first page in the container
  389. *
  390. * Implements RecursiveIterator interface.
  391. *
  392. * @return void
  393. */
  394. public function rewind()
  395. {
  396. $this->_sort();
  397. reset($this->_index);
  398. }
  399. /**
  400. * Checks if container index is valid
  401. *
  402. * Implements RecursiveIterator interface.
  403. *
  404. * @return bool
  405. */
  406. public function valid()
  407. {
  408. $this->_sort();
  409. return current($this->_index) !== false;
  410. }
  411. /**
  412. * Proxy to hasPages()
  413. *
  414. * Implements RecursiveIterator interface.
  415. *
  416. * @return bool whether container has any pages
  417. */
  418. public function hasChildren()
  419. {
  420. return $this->hasPages();
  421. }
  422. /**
  423. * Returns the child container.
  424. *
  425. * Implements RecursiveIterator interface.
  426. *
  427. * @return Zend_Navigation_Page|null
  428. */
  429. public function getChildren()
  430. {
  431. $hash = key($this->_index);
  432. if (isset($this->_pages[$hash])) {
  433. return $this->_pages[$hash];
  434. }
  435. return null;
  436. }
  437. // Countable interface:
  438. /**
  439. * Returns number of pages in container
  440. *
  441. * Implements Countable interface.
  442. *
  443. * @return int number of pages in the container
  444. */
  445. public function count()
  446. {
  447. return count($this->_index);
  448. }
  449. }