PageRenderTime 50ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/src/PHPagstract/Symbol/Symbols/Tokens/PagstractList.php

https://gitlab.com/php.bjoernbartels.earth/phpagstract
PHP | 280 lines | 130 code | 48 blank | 102 comment | 33 complexity | a3ce1c092b18a76baced3d155ecc8a99 MD5 | raw file
  1. <?php
  2. /**
  3. * PHPagstract list token symbol class
  4. */
  5. namespace PHPagstract\Symbol\Symbols\Tokens;
  6. use PHPagstract\Symbol\Symbols\AbstractTokenSymbol;
  7. use PHPagstract\Token\Tokens\Token;
  8. use PHPagstract\Symbol\Symbols\Properties\ListProperty;
  9. /**
  10. * PHPagstract list token symbol class
  11. *
  12. * @package PHPagstract
  13. * @author Björn Bartels <coding@bjoernbartels.earth>
  14. * @link https://gitlab.bjoernbartels.earth/groups/zf2
  15. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
  16. * @copyright copyright (c) 2016 Björn Bartels <coding@bjoernbartels.earth>
  17. */
  18. class PagstractList extends PagstractMarkup
  19. {
  20. /**
  21. * class constructor
  22. *
  23. * @param AbstractTokenSymbol $parent
  24. * @param string $throwOnError
  25. */
  26. public function __construct($parent = null, $throwOnError = false)
  27. {
  28. parent::__construct($parent, $throwOnError);
  29. }
  30. /**
  31. * convert symbol to string representation
  32. *
  33. * @return string
  34. */
  35. public function toString()
  36. {
  37. $attr = ($this->getAttributes());
  38. /**
  39. * @var AbstractPropertySymbol $property
  40. */
  41. $property = null;
  42. if (isset($attr['pma:name'])) {
  43. $property = $this->getPropertyResolver()->getPropertyByReference($attr['pma:name']);
  44. }
  45. if ($property === null) {
  46. return '';
  47. }
  48. if (!($property instanceof ListProperty) ) {
  49. /*if ( $this->config()->throwOnError() ) {
  50. echo '<pre>property: '.htmlentities(print_r( $property->getType(), true )).'</pre>'; flush();
  51. echo '<pre>property: '.htmlentities(print_r( $property->get('items'), true )).'</pre>'; flush();
  52. throw new RendererException("invalid property to use as list '".$attr['pma:name']."'");
  53. }*/
  54. return '';
  55. }
  56. $result = '';
  57. // detect elements: header, footer, ...
  58. $header = $this->getListHeader();
  59. $footer = $this->getListFooter();
  60. $first = $this->getListFirst();
  61. $last = $this->getListLast();
  62. $even = $this->getListEven();
  63. $odd = $this->getListOdd();
  64. $content = $this->getListContent();
  65. $noContent = $this->getListNoContent();
  66. $separator = $this->getListSeparator();
  67. // temporarily add list context to scope
  68. $this->getPropertyResolver()->addScope($property);
  69. // process header
  70. if ($header !== null) {
  71. $result .= $this->getRenderer()->render($header->getChildren());
  72. }
  73. // iterate the items
  74. $listItems = $property->get('items');
  75. $itemCount = count($listItems);
  76. if ($itemCount == 0) {
  77. // process no-content
  78. if ($noContent !== null) {
  79. $result .= $this->getRenderer()->render($noContent->getChildren());
  80. }
  81. } else {
  82. // process the item
  83. foreach ($listItems as $itemIndex => $listItem) {
  84. $itemScopeReference = $listItems[$itemIndex]; // $attr['pma:name'] . '['.$index.']';
  85. // temporarily add list item reference to scope
  86. $this->getPropertyResolver()->addScope($itemScopeReference);
  87. if (($itemIndex == 0) && ($first !== null) ) {
  88. // process first item
  89. $result .= $this->getRenderer()->render($first->getChildren());
  90. } else if (($itemIndex == $itemCount-1) && ($last !== null) ) {
  91. // process last item
  92. $result .= $this->getRenderer()->render($last->getChildren());
  93. } else if (( ($itemIndex+1) % 2 == 0 ) && ($even !== null) ) {
  94. // process even item
  95. $result .= $this->getRenderer()->render($even->getChildren());
  96. } else if (( ($itemIndex+1) % 2 == 1 ) && ($odd !== null) ) {
  97. // process odd item
  98. $result .= $this->getRenderer()->render($odd->getChildren());
  99. } else {
  100. if ($content === null) {
  101. // process list symbol children if there is no content item
  102. $result .= $this->getRenderer()->render($this->getChildren());
  103. } else {
  104. // process content item
  105. $result .= $this->getRenderer()->render($content->getChildren());
  106. }
  107. }
  108. if ($itemIndex < ($itemCount-1)) {
  109. // process separator
  110. if ($separator !== null) {
  111. $result .= $this->getRenderer()->render($separator->getChildren());
  112. }
  113. }
  114. // remove list item reference from scope
  115. $this->getPropertyResolver()->unsetLastScope();
  116. }
  117. }
  118. // process footer
  119. if ($footer !== null) {
  120. $result .= $this->getRenderer()->render($footer->getChildren());
  121. }
  122. // remove list context from scope
  123. $this->getPropertyResolver()->unsetLastScope();
  124. return $result;
  125. }
  126. /**
  127. * detect list element symbol by name
  128. *
  129. * @param string $tokenName
  130. * @return null|mixed
  131. */
  132. private function getChildTokenSymbol( $tokenSymbolName )
  133. {
  134. if ($this->hasChildren() ) {
  135. $children = $this->getChildren()->getIterator();
  136. $children->rewind();
  137. /**
  138. * @var AbstractTokenSymbol $child
  139. */
  140. $child = $children->current();
  141. while ($child !== null) {
  142. if ($child->getName() == $tokenSymbolName ) {
  143. return $child;
  144. }
  145. $children->next();
  146. $child = $children->current();
  147. }
  148. }
  149. return null;
  150. }
  151. /**
  152. * detect list-header element
  153. *
  154. * @return null|PagstractListHeader
  155. */
  156. private function getListHeader()
  157. {
  158. return $this->getChildTokenSymbol(Token::PAGSTRACTLISTHEADER);
  159. }
  160. /**
  161. * detect list-footer element
  162. *
  163. * @return null|PagstractListFooter
  164. */
  165. private function getListFooter()
  166. {
  167. return $this->getChildTokenSymbol(Token::PAGSTRACTLISTFOOTER);
  168. }
  169. /**
  170. * detect list-first element
  171. *
  172. * @return null|PagstractListFrist
  173. */
  174. private function getListFirst()
  175. {
  176. return $this->getChildTokenSymbol(Token::PAGSTRACTLISTFIRST);
  177. }
  178. /**
  179. * detect list-last element
  180. *
  181. * @return null|PagstractListLast
  182. */
  183. private function getListLast()
  184. {
  185. return $this->getChildTokenSymbol(Token::PAGSTRACTLISTLAST);
  186. }
  187. /**
  188. * detect list-even element
  189. *
  190. * @return null|PagstractListEven
  191. */
  192. private function getListEven()
  193. {
  194. return $this->getChildTokenSymbol(Token::PAGSTRACTLISTEVEN);
  195. }
  196. /**
  197. * detect list-odd element
  198. *
  199. * @return null|PagstractListOdd
  200. */
  201. private function getListOdd()
  202. {
  203. return $this->getChildTokenSymbol(Token::PAGSTRACTLISTODD);
  204. }
  205. /**
  206. * detect list-content element
  207. *
  208. * @return null|PagstractListContent
  209. */
  210. private function getListContent()
  211. {
  212. return $this->getChildTokenSymbol(Token::PAGSTRACTLISTCONTENT);
  213. }
  214. /**
  215. * detect list-no-content element
  216. *
  217. * @return null|PagstractListNoContent
  218. */
  219. private function getListNoContent()
  220. {
  221. return $this->getChildTokenSymbol(Token::PAGSTRACTLISTNOCONTENT);
  222. }
  223. /**
  224. * detect list-separator element
  225. *
  226. * @return null|PagstractListSeparator
  227. */
  228. private function getListSeparator()
  229. {
  230. return $this->getChildTokenSymbol(Token::PAGSTRACTLISTSEPARATOR);
  231. }
  232. }