PageRenderTime 54ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/eztemplate/classes/eztemplatesectioniterator.php

https://github.com/StephanBoganskyXrow/ezpublish
PHP | 163 lines | 85 code | 10 blank | 68 comment | 11 complexity | 84a33f2f07349fe25464c99f7f92ec15 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. //
  3. // Definition of eZTemplateSectionIterator class
  4. //
  5. // Created on: <26-Feb-2004 11:33:05 >
  6. //
  7. // ## BEGIN COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
  8. // SOFTWARE NAME: eZ Publish
  9. // SOFTWARE RELEASE: 4.1.x
  10. // COPYRIGHT NOTICE: Copyright (C) 1999-2011 eZ Systems AS
  11. // SOFTWARE LICENSE: GNU General Public License v2.0
  12. // NOTICE: >
  13. // This program is free software; you can redistribute it and/or
  14. // modify it under the terms of version 2.0 of the GNU General
  15. // Public License as published by the Free Software Foundation.
  16. //
  17. // This program is distributed in the hope that it will be useful,
  18. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. // GNU General Public License for more details.
  21. //
  22. // You should have received a copy of version 2.0 of the GNU General
  23. // Public License along with this program; if not, write to the Free
  24. // Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  25. // MA 02110-1301, USA.
  26. //
  27. //
  28. // ## END COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
  29. //
  30. /*! \file
  31. */
  32. /*!
  33. \class eZTemplateSectionIterator eztemplatesectioniterator.php
  34. \ingroup eZTemplateFunctions
  35. \brief The iterator item in a section loop which works as a proxy.
  36. The iterator provides transparent access to iterator items. It will
  37. redirect all attribute calls to the iterator item with the exception
  38. of a few internal values. The internal values are
  39. - item - The actual item, provides backwards compatibility
  40. - key - The current key
  41. - index - The current index value (starts at 0 and increases with 1 for each element)
  42. - number - The current index value + 1 (starts at 1 and increases with 1 for each element)
  43. - sequence - The current sequence value
  44. - last - The last iterated element item
  45. */
  46. class eZTemplateSectionIterator
  47. {
  48. /*!
  49. Initializes the iterator with empty values.
  50. */
  51. function eZTemplateSectionIterator()
  52. {
  53. $this->InternalAttributes = array( 'item' => false,
  54. 'key' => false,
  55. 'index' => false,
  56. 'number' => false,
  57. 'sequence' => false,
  58. 'last' => false );
  59. $this->InternalAttributeNames = array_keys( $this->InternalAttributes );
  60. }
  61. /*!
  62. \return the value of the current item for the template system to use.
  63. */
  64. function templateValue()
  65. {
  66. $value = $this->InternalAttributes['item'];
  67. return $value;
  68. }
  69. /*!
  70. \return a merged list of attributes from both the internal attributes and the items attributes.
  71. */
  72. function attributes()
  73. {
  74. $attributes = array();
  75. $item = $this->InternalAttributes['item'];
  76. if ( is_array( $item ) )
  77. {
  78. $attributes = array_keys( $item );
  79. }
  80. else if ( is_object( $item ) and
  81. method_exists( $item, 'attributes' ) )
  82. {
  83. $attributes = $item->attributes();
  84. }
  85. $attributes = array_merge( $this->InternalAttributes, $attributes );
  86. $attributes = array_unique( $attributes );
  87. return $attributes;
  88. }
  89. /*!
  90. \return \c true if the attribute \a $name exists either in
  91. the internal attributes or in the item value.
  92. */
  93. function hasAttribute( $name )
  94. {
  95. if ( in_array( $name, $this->InternalAttributeNames ) )
  96. return true;
  97. $item = $this->InternalAttributes['item'];
  98. if ( is_array( $item ) )
  99. {
  100. return in_array( $name, array_keys( $item ) );
  101. }
  102. else if ( is_object( $item ) and
  103. method_exists( $item, 'hasAttribute' ) )
  104. {
  105. return $item->hasAttribute( $name );
  106. }
  107. return false;
  108. }
  109. /*!
  110. \return the attribute value of either the internal attributes or
  111. from the item value if the attribute exists for it.
  112. */
  113. function attribute( $name )
  114. {
  115. if ( in_array( $name, $this->InternalAttributeNames ) )
  116. {
  117. return $this->InternalAttributes[$name];
  118. }
  119. $item = $this->InternalAttributes['item'];
  120. if ( is_array( $item ) )
  121. {
  122. return $item[$name];
  123. }
  124. else if ( is_object( $item ) and
  125. method_exists( $item, 'attribute' ) )
  126. {
  127. return $item->attribute( $name );
  128. }
  129. eZDebug::writeError( "Attribute '$name' does not exist", __METHOD__ );
  130. return null;
  131. }
  132. /*!
  133. Updates the iterator with the current iteration values.
  134. */
  135. function setIteratorValues( $item, $key, $index, $number, $sequence, &$last )
  136. {
  137. $this->InternalAttributes['item'] = $item;
  138. $this->InternalAttributes['key'] = $key;
  139. $this->InternalAttributes['index'] = $index;
  140. $this->InternalAttributes['number'] = $number;
  141. $this->InternalAttributes['sequence'] = $sequence;
  142. $this->InternalAttributes['last'] = $last;
  143. }
  144. /*!
  145. Updates the current sequence value to \a $sequence.
  146. */
  147. function setSequence( $sequence )
  148. {
  149. $this->InternalAttributes['sequence'] = $sequence;
  150. }
  151. }
  152. ?>