AbstractContainer.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. <?php
  2. /**
  3. *
  4. * Cube Framework $Id$ V5rykL5rjDY3kCKBkcdZMWXITYhCgaihxJ7xfPl7ZbY=
  5. *
  6. * @link http://codecu.be/framework
  7. * @copyright Copyright (c) 2015 CodeCube SRL
  8. * @license http://codecu.be/framework/license Commercial License
  9. *
  10. * @version 1.4
  11. */
  12. namespace Cube\Navigation;
  13. use Cube\Config\AbstractConfig;
  14. abstract class AbstractContainer implements \RecursiveIterator
  15. {
  16. /**
  17. *
  18. * will hold the navigation array
  19. *
  20. * @var array
  21. */
  22. protected $_pages = array();
  23. /**
  24. *
  25. * get the pages container
  26. *
  27. * @return array
  28. */
  29. public function getPages()
  30. {
  31. return $this->_pages;
  32. }
  33. /**
  34. *
  35. * add new pages to the pages array, accepts an array or an object of type Config
  36. *
  37. * @param \Cube\Config\AbstractConfig|array $pages
  38. *
  39. * @return \Cube\Navigation\AbstractContainer
  40. * @throws \InvalidArgumentException
  41. */
  42. public function addPages($pages)
  43. {
  44. if (!is_array($pages) && !($pages instanceof AbstractConfig)) {
  45. throw new \InvalidArgumentException("The navigation object requires an array or an object of type \Cube\Config in order to be created.");
  46. }
  47. else {
  48. // need to test as i'm not sure it works as intended
  49. if ($pages instanceof AbstractConfig) {
  50. $pages = $pages->getData();
  51. }
  52. foreach ($pages as $page) {
  53. $this->addPage($page);
  54. }
  55. }
  56. return $this;
  57. }
  58. /**
  59. *
  60. * set pages array
  61. *
  62. * @param \Cube\Config\AbstractConfig|array $pages
  63. *
  64. * @return \Cube\Navigation\AbstractContainer
  65. */
  66. public function setPages($pages)
  67. {
  68. $this->removePages();
  69. $this->addPages($pages);
  70. return $this;
  71. }
  72. /**
  73. *
  74. * reset pages array
  75. *
  76. * @return \Cube\Navigation\AbstractContainer
  77. */
  78. public function removePages()
  79. {
  80. $this->_pages = array();
  81. return $this;
  82. }
  83. /**
  84. * Adds a page to the container
  85. *
  86. * This method will inject the container as the given page's parent by
  87. * calling {@link Page\AbstractPage::setParent()}.
  88. *
  89. * @param Page\AbstractPage|array|\Traversable $page page to add
  90. *
  91. * @return AbstractContainer fluent interface, returns self
  92. * @throws \InvalidArgumentException if page is invalid
  93. */
  94. public function addPage($page)
  95. {
  96. if ($page === $this) {
  97. throw new \InvalidArgumentException('A page cannot have itself as a parent');
  98. }
  99. if (!$page instanceof Page\AbstractPage) {
  100. if (!is_array($page) && !$page instanceof \Traversable) {
  101. throw new \InvalidArgumentException(sprintf(
  102. "'%s' must be an instance of Cube\Navigation\Page\AbstractPage, or an array", $page));
  103. }
  104. $page = Page\AbstractPage::factory($page);
  105. }
  106. if ($page) {
  107. $this->_pages[] = $page;
  108. $page->setParent($this);
  109. }
  110. return $this;
  111. }
  112. /**
  113. * get the first page matching a property
  114. *
  115. * @param string $property property name to search against
  116. * @param mixed $value property value
  117. * @param bool $exact whether to search for exact values only
  118. *
  119. * @return \Cube\Navigation\Page\AbstractPage|null
  120. */
  121. public function findOneBy($property, $value, $exact = true)
  122. {
  123. $iterator = new \RecursiveIteratorIterator($this, \RecursiveIteratorIterator::SELF_FIRST);
  124. /** @var \Cube\Navigation\Page\AbstractPage $page */
  125. foreach ($iterator as $page) {
  126. $pageName = $page->get($property);
  127. if ($exact === true) {
  128. if ($pageName == $value) {
  129. return $page;
  130. }
  131. }
  132. else {
  133. if (stristr($pageName, $value)) {
  134. return $page;
  135. }
  136. }
  137. }
  138. return null;
  139. }
  140. /**
  141. *
  142. * get all pages matching a set property
  143. *
  144. * @param string $property property name to search against
  145. * @param mixed $value property value
  146. * @param bool $exact whether to search for exact values only
  147. *
  148. * @return array
  149. */
  150. public function findAllBy($property, $value, $exact = true)
  151. {
  152. $result = array();
  153. $iterator = new \RecursiveIteratorIterator($this, \RecursiveIteratorIterator::SELF_FIRST);
  154. /** @var \Cube\Navigation\Page\AbstractPage $page */
  155. foreach ($iterator as $page) {
  156. $pageName = $page->get($property);
  157. if ($exact === true) {
  158. if ($pageName == $value) {
  159. $result[] = $page;
  160. }
  161. }
  162. else {
  163. if (stristr($pageName, $value)) {
  164. $result[] = $page;
  165. }
  166. }
  167. }
  168. return $result;
  169. }
  170. /**
  171. *
  172. * checks if the current position is valid
  173. *
  174. * @return bool
  175. */
  176. public function valid()
  177. {
  178. return current($this->_pages) !== false;
  179. }
  180. /**
  181. *
  182. * checks if the current container has children
  183. *
  184. * @return bool
  185. */
  186. public function hasChildren()
  187. {
  188. return (count($this->_pages) > 0) ? true : false;
  189. }
  190. public function next()
  191. {
  192. next($this->_pages);
  193. }
  194. /**
  195. *
  196. * return the current container
  197. *
  198. * @return mixed
  199. */
  200. public function current()
  201. {
  202. return current($this->_pages);
  203. }
  204. /**
  205. *
  206. * returns an iterator for the current element
  207. *
  208. * @return mixed
  209. */
  210. public function getChildren()
  211. {
  212. $key = key($this->_pages);
  213. if (isset($this->_pages[$key])) {
  214. return $this->_pages[$key];
  215. }
  216. return null;
  217. }
  218. public function rewind()
  219. {
  220. reset($this->_pages);
  221. }
  222. /**
  223. *
  224. * return the key of the current element
  225. *
  226. * @return mixed
  227. */
  228. public function key()
  229. {
  230. return key($this->_pages);
  231. }
  232. }