Container.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613
  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
  247. * preg_match($value, $property), or null if not found
  248. *
  249. * @param string $property name of property to match against
  250. * @param mixed $value value to match property against
  251. * @param bool $useRegex [optional] if true PHP's preg_match
  252. * is used. Default is false.
  253. * @return Zend_Navigation_Page|null matching page or null
  254. */
  255. public function findOneBy($property, $value, $useRegex = false)
  256. {
  257. $iterator = new RecursiveIteratorIterator(
  258. $this,
  259. RecursiveIteratorIterator::SELF_FIRST
  260. );
  261. foreach ($iterator as $page) {
  262. $pageProperty = $page->get($property);
  263. // Rel and rev
  264. if (is_array($pageProperty)) {
  265. foreach ($pageProperty as $item) {
  266. if (is_array($item)) {
  267. // Use regex?
  268. if (true === $useRegex) {
  269. foreach ($item as $item2) {
  270. if (0 !== preg_match($value, $item2)) {
  271. return $page;
  272. }
  273. }
  274. } else {
  275. if (in_array($value, $item)) {
  276. return $page;
  277. }
  278. }
  279. } else {
  280. // Use regex?
  281. if (true === $useRegex) {
  282. if (0 !== preg_match($value, $item)) {
  283. return $page;
  284. }
  285. } else {
  286. if ($item == $value) {
  287. return $page;
  288. }
  289. }
  290. }
  291. }
  292. continue;
  293. }
  294. // Use regex?
  295. if (true === $useRegex) {
  296. if (preg_match($value, $pageProperty)) {
  297. return $page;
  298. }
  299. } else {
  300. if ($pageProperty == $value) {
  301. return $page;
  302. }
  303. }
  304. }
  305. return null;
  306. }
  307. /**
  308. * Returns all child pages matching $property == $value or
  309. * preg_match($value, $property), or an empty array if no pages are found
  310. *
  311. * @param string $property name of property to match against
  312. * @param mixed $value value to match property against
  313. * @param bool $useRegex [optional] if true PHP's preg_match is used.
  314. * Default is false.
  315. * @return array array containing only Zend_Navigation_Page
  316. * instances
  317. */
  318. public function findAllBy($property, $value, $useRegex = false)
  319. {
  320. $found = array();
  321. $iterator = new RecursiveIteratorIterator(
  322. $this,
  323. RecursiveIteratorIterator::SELF_FIRST
  324. );
  325. foreach ($iterator as $page) {
  326. $pageProperty = $page->get($property);
  327. // Rel and rev
  328. if (is_array($pageProperty)) {
  329. foreach ($pageProperty as $item) {
  330. if (is_array($item)) {
  331. // Use regex?
  332. if (true === $useRegex) {
  333. foreach ($item as $item2) {
  334. if (0 !== preg_match($value, $item2)) {
  335. $found[] = $page;
  336. }
  337. }
  338. } else {
  339. if (in_array($value, $item)) {
  340. $found[] = $page;
  341. }
  342. }
  343. } else {
  344. // Use regex?
  345. if (true === $useRegex) {
  346. if (0 !== preg_match($value, $item)) {
  347. $found[] = $page;
  348. }
  349. } else {
  350. if ($item == $value) {
  351. $found[] = $page;
  352. }
  353. }
  354. }
  355. }
  356. continue;
  357. }
  358. // Use regex?
  359. if (true === $useRegex) {
  360. if (0 !== preg_match($value, $pageProperty)) {
  361. $found[] = $page;
  362. }
  363. } else {
  364. if ($pageProperty == $value) {
  365. $found[] = $page;
  366. }
  367. }
  368. }
  369. return $found;
  370. }
  371. /**
  372. * Returns page(s) matching $property == $value or
  373. * preg_match($value, $property)
  374. *
  375. * @param string $property name of property to match against
  376. * @param mixed $value value to match property against
  377. * @param bool $all [optional] whether an array of all matching
  378. * pages should be returned, or only the first.
  379. * If true, an array will be returned, even if not
  380. * matching pages are found. If false, null will
  381. * be returned if no matching page is found.
  382. * Default is false.
  383. * @param bool $useRegex [optional] if true PHP's preg_match is used.
  384. * Default is false.
  385. * @return Zend_Navigation_Page|null matching page or null
  386. */
  387. public function findBy($property, $value, $all = false, $useRegex = false)
  388. {
  389. if ($all) {
  390. return $this->findAllBy($property, $value, $useRegex);
  391. } else {
  392. return $this->findOneBy($property, $value, $useRegex);
  393. }
  394. }
  395. /**
  396. * Magic overload: Proxy calls to finder methods
  397. *
  398. * Examples of finder calls:
  399. * <code>
  400. * // METHOD // SAME AS
  401. * $nav->findByLabel('foo'); // $nav->findOneBy('label', 'foo');
  402. * $nav->findByLabel('/foo/', true); // $nav->findBy('label', '/foo/', true);
  403. * $nav->findOneByLabel('foo'); // $nav->findOneBy('label', 'foo');
  404. * $nav->findAllByClass('foo'); // $nav->findAllBy('class', 'foo');
  405. * </code>
  406. *
  407. * @param string $method method name
  408. * @param array $arguments method arguments
  409. * @return mixed Zend_Navigation|array|null matching page, array of pages
  410. * or null
  411. * @throws Zend_Navigation_Exception if method does not exist
  412. */
  413. public function __call($method, $arguments)
  414. {
  415. if (@preg_match('/(find(?:One|All)?By)(.+)/', $method, $match)) {
  416. return $this->{$match[1]}($match[2], $arguments[0], !empty($arguments[1]));
  417. }
  418. require_once 'Zend/Navigation/Exception.php';
  419. throw new Zend_Navigation_Exception(
  420. sprintf(
  421. 'Bad method call: Unknown method %s::%s',
  422. get_class($this),
  423. $method
  424. )
  425. );
  426. }
  427. /**
  428. * Returns an array representation of all pages in container
  429. *
  430. * @return array
  431. */
  432. public function toArray()
  433. {
  434. $pages = array();
  435. $this->_dirtyIndex = true;
  436. $this->_sort();
  437. $indexes = array_keys($this->_index);
  438. foreach ($indexes as $hash) {
  439. $pages[] = $this->_pages[$hash]->toArray();
  440. }
  441. return $pages;
  442. }
  443. // RecursiveIterator interface:
  444. /**
  445. * Returns current page
  446. *
  447. * Implements RecursiveIterator interface.
  448. *
  449. * @return Zend_Navigation_Page current page or null
  450. * @throws Zend_Navigation_Exception if the index is invalid
  451. */
  452. public function current()
  453. {
  454. $this->_sort();
  455. current($this->_index);
  456. $hash = key($this->_index);
  457. if (isset($this->_pages[$hash])) {
  458. return $this->_pages[$hash];
  459. } else {
  460. require_once 'Zend/Navigation/Exception.php';
  461. throw new Zend_Navigation_Exception(
  462. 'Corruption detected in container; ' .
  463. 'invalid key found in internal iterator');
  464. }
  465. }
  466. /**
  467. * Returns hash code of current page
  468. *
  469. * Implements RecursiveIterator interface.
  470. *
  471. * @return string hash code of current page
  472. */
  473. public function key()
  474. {
  475. $this->_sort();
  476. return key($this->_index);
  477. }
  478. /**
  479. * Moves index pointer to next page in the container
  480. *
  481. * Implements RecursiveIterator interface.
  482. *
  483. * @return void
  484. */
  485. public function next()
  486. {
  487. $this->_sort();
  488. next($this->_index);
  489. }
  490. /**
  491. * Sets index pointer to first page in the container
  492. *
  493. * Implements RecursiveIterator interface.
  494. *
  495. * @return void
  496. */
  497. public function rewind()
  498. {
  499. $this->_sort();
  500. reset($this->_index);
  501. }
  502. /**
  503. * Checks if container index is valid
  504. *
  505. * Implements RecursiveIterator interface.
  506. *
  507. * @return bool
  508. */
  509. public function valid()
  510. {
  511. $this->_sort();
  512. return current($this->_index) !== false;
  513. }
  514. /**
  515. * Proxy to hasPages()
  516. *
  517. * Implements RecursiveIterator interface.
  518. *
  519. * @return bool whether container has any pages
  520. */
  521. public function hasChildren()
  522. {
  523. return $this->hasPages();
  524. }
  525. /**
  526. * Returns the child container.
  527. *
  528. * Implements RecursiveIterator interface.
  529. *
  530. * @return Zend_Navigation_Page|null
  531. */
  532. public function getChildren()
  533. {
  534. $hash = key($this->_index);
  535. if (isset($this->_pages[$hash])) {
  536. return $this->_pages[$hash];
  537. }
  538. return null;
  539. }
  540. // Countable interface:
  541. /**
  542. * Returns number of pages in container
  543. *
  544. * Implements Countable interface.
  545. *
  546. * @return int number of pages in the container
  547. */
  548. public function count()
  549. {
  550. return count($this->_index);
  551. }
  552. }