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

/library/Zend/View/Helper/PartialLoop.php

http://github.com/zendframework/zf2
PHP | 163 lines | 72 code | 24 blank | 67 comment | 9 complexity | bf220b4680ec07547d6e247f66b41a71 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * Zend Framework (http://framework.zend.com/)
  4. *
  5. * @link http://github.com/zendframework/zf2 for the canonical source repository
  6. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  7. * @license http://framework.zend.com/license/new-bsd New BSD License
  8. */
  9. namespace Zend\View\Helper;
  10. use Traversable;
  11. use Zend\Stdlib\ArrayUtils;
  12. use Zend\View\Exception;
  13. /**
  14. * Helper for rendering a template fragment in its own variable scope; iterates
  15. * over data provided and renders for each iteration.
  16. */
  17. class PartialLoop extends Partial
  18. {
  19. /**
  20. * Marker to where the pointer is at in the loop
  21. *
  22. * @var int
  23. */
  24. protected $partialCounter = 0;
  25. /**
  26. * The current nesting level
  27. *
  28. * @var int
  29. */
  30. private $nestingLevel = 0;
  31. /**
  32. * Stack with object keys for each nested level
  33. *
  34. * @var array indexed by nesting level
  35. */
  36. private $objectKeyStack = array(
  37. 0 => null,
  38. );
  39. /**
  40. * Renders a template fragment within a variable scope distinct from the
  41. * calling View object.
  42. *
  43. * If no arguments are provided, returns object instance.
  44. *
  45. * @param string $name Name of view script
  46. * @param array $values Variables to populate in the view
  47. * @throws Exception\InvalidArgumentException
  48. * @return string
  49. */
  50. public function __invoke($name = null, $values = null)
  51. {
  52. if (0 == func_num_args()) {
  53. return $this;
  54. }
  55. // reset the counter if it's called again
  56. $this->partialCounter = 0;
  57. $content = '';
  58. foreach ($this->extractViewVariables($values) as $item) {
  59. $this->nestObjectKey();
  60. $this->partialCounter++;
  61. $content .= parent::__invoke($name, $item);
  62. $this->unNestObjectKey();
  63. }
  64. return $content;
  65. }
  66. /**
  67. * Get the partial counter
  68. *
  69. * @return int
  70. */
  71. public function getPartialCounter()
  72. {
  73. return $this->partialCounter;
  74. }
  75. /**
  76. * Set object key in this loop and any child loop
  77. *
  78. * {@inheritDoc}
  79. *
  80. * @param string|null $key
  81. *
  82. * @return self
  83. */
  84. public function setObjectKey($key)
  85. {
  86. if (null === $key) {
  87. unset($this->objectKeyStack[$this->nestingLevel]);
  88. } else {
  89. $this->objectKeyStack[$this->nestingLevel] = (string) $key;
  90. }
  91. return parent::setObjectKey($key);
  92. }
  93. /**
  94. * Increment nestedLevel and default objectKey to parent's value
  95. *
  96. * @return self
  97. */
  98. private function nestObjectKey()
  99. {
  100. $this->nestingLevel += 1;
  101. $this->setObjectKey($this->getObjectKey());
  102. return $this;
  103. }
  104. /**
  105. * Decrement nestedLevel and restore objectKey to parent's value
  106. *
  107. * @return self
  108. */
  109. private function unNestObjectKey()
  110. {
  111. $this->setObjectKey(null);
  112. $this->nestingLevel -= 1;
  113. if (isset($this->objectKeyStack[$this->nestingLevel])) {
  114. $this->objectKey = $this->objectKeyStack[$this->nestingLevel];
  115. }
  116. return $this;
  117. }
  118. /**
  119. * @param mixed $values
  120. *
  121. * @return array Variables to populate in the view
  122. */
  123. private function extractViewVariables($values)
  124. {
  125. if ($values instanceof Traversable) {
  126. return ArrayUtils::iteratorToArray($values, false);
  127. }
  128. if (is_array($values)) {
  129. return $values;
  130. }
  131. if (is_object($values) && method_exists($values, 'toArray')) {
  132. return $values->toArray();
  133. }
  134. throw new Exception\InvalidArgumentException(sprintf(
  135. 'PartialLoop helper requires iterable data, %s given',
  136. is_object($values) ? get_class($values) : gettype($values)
  137. ));
  138. }
  139. }