PageRenderTime 40ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/eztemplate/classes/eztemplatesectioniterator.php

http://github.com/ezsystems/ezpublish
PHP | 151 lines | 96 code | 9 blank | 46 comment | 11 complexity | 38a27ac76c16f051595acec19013fdbe MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * File containing the eZTemplateSectionIterator class.
  4. *
  5. * @copyright Copyright (C) eZ Systems AS. All rights reserved.
  6. * @license For full copyright and license information view LICENSE file distributed with this source code.
  7. * @version //autogentag//
  8. * @package lib
  9. */
  10. /*!
  11. \class eZTemplateSectionIterator eztemplatesectioniterator.php
  12. \ingroup eZTemplateFunctions
  13. \brief The iterator item in a section loop which works as a proxy.
  14. The iterator provides transparent access to iterator items. It will
  15. redirect all attribute calls to the iterator item with the exception
  16. of a few internal values. The internal values are
  17. - item - The actual item, provides backwards compatibility
  18. - key - The current key
  19. - index - The current index value (starts at 0 and increases with 1 for each element)
  20. - number - The current index value + 1 (starts at 1 and increases with 1 for each element)
  21. - sequence - The current sequence value
  22. - last - The last iterated element item
  23. */
  24. class eZTemplateSectionIterator
  25. {
  26. /**
  27. * Initializes the iterator with empty values.
  28. */
  29. public function __construct()
  30. {
  31. $this->InternalAttributes = array( 'item' => false,
  32. 'key' => false,
  33. 'index' => false,
  34. 'number' => false,
  35. 'sequence' => false,
  36. 'last' => false );
  37. }
  38. /*!
  39. \return the value of the current item for the template system to use.
  40. */
  41. function templateValue()
  42. {
  43. $value = $this->InternalAttributes['item'];
  44. return $value;
  45. }
  46. /*!
  47. \return a merged list of attributes from both the internal attributes and the items attributes.
  48. */
  49. function attributes()
  50. {
  51. $attributes = array();
  52. $item = $this->InternalAttributes['item'];
  53. if ( is_array( $item ) )
  54. {
  55. $attributes = array_keys( $item );
  56. }
  57. else if ( is_object( $item ) and
  58. method_exists( $item, 'attributes' ) )
  59. {
  60. $attributes = $item->attributes();
  61. }
  62. $attributes = array_merge( $this->InternalAttributes, $attributes );
  63. $attributes = array_unique( $attributes );
  64. return $attributes;
  65. }
  66. /*!
  67. \return \c true if the attribute \a $name exists either in
  68. the internal attributes or in the item value.
  69. */
  70. function hasAttribute( $name )
  71. {
  72. switch ( $name )
  73. {
  74. case "item":
  75. case "key":
  76. case "index":
  77. case "number":
  78. case "sequence":
  79. case "last":
  80. return true;
  81. }
  82. $item = $this->InternalAttributes['item'];
  83. if ( is_array( $item ) )
  84. {
  85. return array_key_exists( $name, $item );
  86. }
  87. if ( is_object( $item ) && method_exists( $item, 'hasAttribute' ) )
  88. {
  89. return $item->hasAttribute( $name );
  90. }
  91. return false;
  92. }
  93. /*!
  94. \return the attribute value of either the internal attributes or
  95. from the item value if the attribute exists for it.
  96. */
  97. function attribute( $name )
  98. {
  99. switch ( $name )
  100. {
  101. case "item":
  102. case "key":
  103. case "index":
  104. case "number":
  105. case "sequence":
  106. case "last":
  107. return $this->InternalAttributes[$name];
  108. }
  109. $item = $this->InternalAttributes['item'];
  110. if ( is_array( $item ) )
  111. {
  112. return $item[$name];
  113. }
  114. if ( is_object( $item ) && method_exists( $item, 'attribute' ) )
  115. {
  116. return $item->attribute( $name );
  117. }
  118. eZDebug::writeError( "Attribute '$name' does not exist", __METHOD__ );
  119. return null;
  120. }
  121. /*!
  122. Updates the iterator with the current iteration values.
  123. */
  124. function setIteratorValues( $item, $key, $index, $number, $sequence, &$last )
  125. {
  126. $this->InternalAttributes['item'] = $item;
  127. $this->InternalAttributes['key'] = $key;
  128. $this->InternalAttributes['index'] = $index;
  129. $this->InternalAttributes['number'] = $number;
  130. $this->InternalAttributes['sequence'] = $sequence;
  131. $this->InternalAttributes['last'] = $last;
  132. }
  133. /*!
  134. Updates the current sequence value to \a $sequence.
  135. */
  136. function setSequence( $sequence )
  137. {
  138. $this->InternalAttributes['sequence'] = $sequence;
  139. }
  140. }
  141. ?>