Container.php 19 KB

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