Source for file HarmoniIterator.class.php

Documentation is available at HarmoniIterator.class.php

  1. <?php
  2.  
  3. require_once(HARMONI."Primitives/Objects/SObject.class.php");
  4.  
  5. /**
  6. * A class for passing an arbitrary input array as an iterator.
  7. *
  8. * @package harmoni.osid_v2.shared
  9. *
  10. * @copyright Copyright &copy; 2005, Middlebury College
  11. * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License (GPL)
  12. *
  13. * @version $Id: HarmoniIterator.class.php,v 1.12 2007/09/04 20:25:48 adamfranco Exp $
  14. */
  15. class HarmoniIterator
  16. extends SObject
  17. {
  18.  
  19. /**
  20. * @var array $_elements The stored elements.
  21. * @access private
  22. */
  23. var $_elements = array();
  24. /**
  25. * @var int $_i The current posititon.
  26. * @access private
  27. */
  28. var $_i = -1;
  29. /**
  30. * Constructor
  31. */
  32. function HarmoniIterator ($elementArray) {
  33. if($elementArray===NULL){
  34. $elementArray=array();
  35. }
  36. // load the elements into our private array
  37. foreach (array_keys($elementArray) as $i => $key) {
  38. $this->_elements[] =$elementArray[$key];
  39. }
  40. }
  41. /**
  42. * Add an item to the iterator.
  43. *
  44. * WARNING: NOT IN OSID
  45. *
  46. * @param ref mixed $element
  47. * @return void
  48. * @access public
  49. * @since 7/3/07
  50. */
  51. function add ( $element ) {
  52. $this->_elements[] =$element;
  53. }
  54.  
  55. // public boolean hasNext();
  56. function hasNext() {
  57. return ($this->_i < count($this->_elements)-1);
  58. }
  59.  
  60. // public Type & next();
  61. function next() {
  62. if ($this->hasNext()) {
  63. $this->_i++;
  64. return $this->_elements[$this->_i];
  65. } else {
  66. throwError(new Error(SharedException::NO_MORE_ITERATOR_ELEMENTS(), get_class($this), 1));
  67. }
  68. }
  69.  
  70. /**
  71. * Gives the number of items in the iterator
  72. *
  73. * @return int
  74. * @public
  75. */
  76. function count () {
  77. return count($this->_elements);
  78. }
  79. /**
  80. * Skips the next item in the iterator
  81. *
  82. * @return void
  83. * @public
  84. */
  85. function skipNext() {
  86. if ($this->hasNext())
  87. $this->_i++;
  88. else
  89. throwError(new Error(SharedException::NO_MORE_ITERATOR_ELEMENTS(),
  90. get_class($this), true));
  91. }
  92. }
  93.  
  94. ?>

Documentation generated on Wed, 19 Sep 2007 10:24:05 -0400 by phpDocumentor 1.3.0RC3