Container.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  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-2012 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id$
  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-2012 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|Zend_Navigation_Container $pages pages to add
  130. * @return Zend_Navigation_Container fluent interface,
  131. * returns self
  132. * @throws Zend_Navigation_Exception if $pages is not
  133. * array, Zend_Config or
  134. * Zend_Navigation_Container
  135. */
  136. public function addPages($pages)
  137. {
  138. if ($pages instanceof Zend_Config) {
  139. $pages = $pages->toArray();
  140. }
  141. if ($pages instanceof Zend_Navigation_Container) {
  142. $pages = iterator_to_array($pages);
  143. }
  144. if (!is_array($pages)) {
  145. require_once 'Zend/Navigation/Exception.php';
  146. throw new Zend_Navigation_Exception(
  147. 'Invalid argument: $pages must be an array, an ' .
  148. 'instance of Zend_Config or an instance of ' .
  149. 'Zend_Navigation_Container');
  150. }
  151. foreach ($pages as $page) {
  152. $this->addPage($page);
  153. }
  154. return $this;
  155. }
  156. /**
  157. * Sets pages this container should have, removing existing pages
  158. *
  159. * @param array $pages pages to set
  160. * @return Zend_Navigation_Container fluent interface, returns self
  161. */
  162. public function setPages(array $pages)
  163. {
  164. $this->removePages();
  165. return $this->addPages($pages);
  166. }
  167. /**
  168. * Returns pages in the container
  169. *
  170. * @return array array of Zend_Navigation_Page instances
  171. */
  172. public function getPages()
  173. {
  174. return $this->_pages;
  175. }
  176. /**
  177. * Removes the given page from the container
  178. *
  179. * @param Zend_Navigation_Page|int $page page to remove, either a page
  180. * instance or a specific page order
  181. * @return bool whether the removal was
  182. * successful
  183. */
  184. public function removePage($page)
  185. {
  186. if ($page instanceof Zend_Navigation_Page) {
  187. $hash = $page->hashCode();
  188. } elseif (is_int($page)) {
  189. $this->_sort();
  190. if (!$hash = array_search($page, $this->_index)) {
  191. return false;
  192. }
  193. } else {
  194. return false;
  195. }
  196. if (isset($this->_pages[$hash])) {
  197. unset($this->_pages[$hash]);
  198. unset($this->_index[$hash]);
  199. $this->_dirtyIndex = true;
  200. return true;
  201. }
  202. return false;
  203. }
  204. /**
  205. * Removes all pages in container
  206. *
  207. * @return Zend_Navigation_Container fluent interface, returns self
  208. */
  209. public function removePages()
  210. {
  211. $this->_pages = array();
  212. $this->_index = array();
  213. return $this;
  214. }
  215. /**
  216. * Checks if the container has the given page
  217. *
  218. * @param Zend_Navigation_Page $page page to look for
  219. * @param bool $recursive [optional] whether to search
  220. * recursively. Default is false.
  221. * @return bool whether page is in container
  222. */
  223. public function hasPage(Zend_Navigation_Page $page, $recursive = false)
  224. {
  225. if (array_key_exists($page->hashCode(), $this->_index)) {
  226. return true;
  227. } elseif ($recursive) {
  228. foreach ($this->_pages as $childPage) {
  229. if ($childPage->hasPage($page, true)) {
  230. return true;
  231. }
  232. }
  233. }
  234. return false;
  235. }
  236. /**
  237. * Returns true if container contains any pages
  238. *
  239. * @return bool whether container has any pages
  240. */
  241. public function hasPages()
  242. {
  243. return count($this->_index) > 0;
  244. }
  245. /**
  246. * Returns a child page matching $property == $value, or null if not found
  247. *
  248. * @param string $property name of property to match against
  249. * @param mixed $value value to match property against
  250. * @return Zend_Navigation_Page|null matching page or null
  251. */
  252. public function findOneBy($property, $value)
  253. {
  254. $iterator = new RecursiveIteratorIterator($this,
  255. RecursiveIteratorIterator::SELF_FIRST);
  256. foreach ($iterator as $page) {
  257. if ($page->get($property) == $value) {
  258. return $page;
  259. }
  260. }
  261. return null;
  262. }
  263. /**
  264. * Returns all child pages matching $property == $value, or an empty array
  265. * if no pages are found
  266. *
  267. * @param string $property name of property to match against
  268. * @param mixed $value value to match property against
  269. * @return array array containing only Zend_Navigation_Page
  270. * instances
  271. */
  272. public function findAllBy($property, $value)
  273. {
  274. $found = array();
  275. $iterator = new RecursiveIteratorIterator($this,
  276. RecursiveIteratorIterator::SELF_FIRST);
  277. foreach ($iterator as $page) {
  278. if ($page->get($property) == $value) {
  279. $found[] = $page;
  280. }
  281. }
  282. return $found;
  283. }
  284. /**
  285. * Returns page(s) matching $property == $value
  286. *
  287. * @param string $property name of property to match against
  288. * @param mixed $value value to match property against
  289. * @param bool $all [optional] whether an array of all matching
  290. * pages should be returned, or only the first.
  291. * If true, an array will be returned, even if not
  292. * matching pages are found. If false, null will
  293. * be returned if no matching page is found.
  294. * Default is false.
  295. * @return Zend_Navigation_Page|null matching page or null
  296. */
  297. public function findBy($property, $value, $all = false)
  298. {
  299. if ($all) {
  300. return $this->findAllBy($property, $value);
  301. } else {
  302. return $this->findOneBy($property, $value);
  303. }
  304. }
  305. /**
  306. * Magic overload: Proxy calls to finder methods
  307. *
  308. * Examples of finder calls:
  309. * <code>
  310. * // METHOD // SAME AS
  311. * $nav->findByLabel('foo'); // $nav->findOneBy('label', 'foo');
  312. * $nav->findOneByLabel('foo'); // $nav->findOneBy('label', 'foo');
  313. * $nav->findAllByClass('foo'); // $nav->findAllBy('class', 'foo');
  314. * </code>
  315. *
  316. * @param string $method method name
  317. * @param array $arguments method arguments
  318. * @throws Zend_Navigation_Exception if method does not exist
  319. */
  320. public function __call($method, $arguments)
  321. {
  322. if (@preg_match('/(find(?:One|All)?By)(.+)/', $method, $match)) {
  323. return $this->{$match[1]}($match[2], $arguments[0]);
  324. }
  325. require_once 'Zend/Navigation/Exception.php';
  326. throw new Zend_Navigation_Exception(sprintf(
  327. 'Bad method call: Unknown method %s::%s',
  328. get_class($this),
  329. $method));
  330. }
  331. /**
  332. * Returns an array representation of all pages in container
  333. *
  334. * @return array
  335. */
  336. public function toArray()
  337. {
  338. $pages = array();
  339. $this->_dirtyIndex = true;
  340. $this->_sort();
  341. $indexes = array_keys($this->_index);
  342. foreach ($indexes as $hash) {
  343. $pages[] = $this->_pages[$hash]->toArray();
  344. }
  345. return $pages;
  346. }
  347. // RecursiveIterator interface:
  348. /**
  349. * Returns current page
  350. *
  351. * Implements RecursiveIterator interface.
  352. *
  353. * @return Zend_Navigation_Page current page or null
  354. * @throws Zend_Navigation_Exception if the index is invalid
  355. */
  356. public function current()
  357. {
  358. $this->_sort();
  359. current($this->_index);
  360. $hash = key($this->_index);
  361. if (isset($this->_pages[$hash])) {
  362. return $this->_pages[$hash];
  363. } else {
  364. require_once 'Zend/Navigation/Exception.php';
  365. throw new Zend_Navigation_Exception(
  366. 'Corruption detected in container; ' .
  367. 'invalid key found in internal iterator');
  368. }
  369. }
  370. /**
  371. * Returns hash code of current page
  372. *
  373. * Implements RecursiveIterator interface.
  374. *
  375. * @return string hash code of current page
  376. */
  377. public function key()
  378. {
  379. $this->_sort();
  380. return key($this->_index);
  381. }
  382. /**
  383. * Moves index pointer to next page in the container
  384. *
  385. * Implements RecursiveIterator interface.
  386. *
  387. * @return void
  388. */
  389. public function next()
  390. {
  391. $this->_sort();
  392. next($this->_index);
  393. }
  394. /**
  395. * Sets index pointer to first page in the container
  396. *
  397. * Implements RecursiveIterator interface.
  398. *
  399. * @return void
  400. */
  401. public function rewind()
  402. {
  403. $this->_sort();
  404. reset($this->_index);
  405. }
  406. /**
  407. * Checks if container index is valid
  408. *
  409. * Implements RecursiveIterator interface.
  410. *
  411. * @return bool
  412. */
  413. public function valid()
  414. {
  415. $this->_sort();
  416. return current($this->_index) !== false;
  417. }
  418. /**
  419. * Proxy to hasPages()
  420. *
  421. * Implements RecursiveIterator interface.
  422. *
  423. * @return bool whether container has any pages
  424. */
  425. public function hasChildren()
  426. {
  427. return $this->hasPages();
  428. }
  429. /**
  430. * Returns the child container.
  431. *
  432. * Implements RecursiveIterator interface.
  433. *
  434. * @return Zend_Navigation_Page|null
  435. */
  436. public function getChildren()
  437. {
  438. $hash = key($this->_index);
  439. if (isset($this->_pages[$hash])) {
  440. return $this->_pages[$hash];
  441. }
  442. return null;
  443. }
  444. // Countable interface:
  445. /**
  446. * Returns number of pages in container
  447. *
  448. * Implements Countable interface.
  449. *
  450. * @return int number of pages in the container
  451. */
  452. public function count()
  453. {
  454. return count($this->_index);
  455. }
  456. }