1<?php 2 3/** 4 * Swift Mailer Array Iterator Interface 5 * Please read the LICENSE file 6 * @author Chris Corbyn <chris@w3style.co.uk> 7 * @package Swift 8 * @license GNU Lesser General Public License 9 */ 10 11require_once dirname(__FILE__) . "/../ClassLoader.php"; 12Swift_ClassLoader::load("Swift_Iterator"); 13 14/** 15 * Swift Array Iterator Interface 16 * Iterates over a standard PHP array. 17 * @package Swift 18 * @author Chris Corbyn <chris@w3style.co.uk> 19 */ 20class Swift_Iterator_Array implements Swift_Iterator 21{ 22 /** 23 * All keys in this array. 24 * @var array 25 */ 26 protected $keys; 27 /** 28 * All values in this array. 29 * @var array 30 */ 31 protected $values; 32 /** 33 * The current array position. 34 * @var int 35 */ 36 protected $pos = -1; 37 38 /** 39 * Ctor. 40 * @param array The array to iterate over. 41 */ 42 public function __construct($input) 43 { 44 $input = (array) $input; 45 $this->keys = array_keys($input); 46 $this->values = array_values($input); 47 } 48 /** 49 * Returns the original array. 50 * @return array 51 */ 52 public function getArray() 53 { 54 return array_combine($this->keys, $this->values); 55 } 56 /** 57 * Returns true if there is a value after the current one. 58 * @return boolean 59 */ 60 public function hasNext() 61 { 62 return array_key_exists($this->pos + 1, $this->keys); 63 } 64 /** 65 * Moves to the next array element if possible. 66 * @return boolean 67 */ 68 public function next() 69 { 70 if ($this->hasNext()) 71 { 72 ++$this->pos; 73 return true; 74 } 75 76 return false; 77 } 78 /** 79 * Goes directly to the given element in the array if possible. 80 * @param int Numeric position 81 * @return boolean 82 */ 83 public function seekTo($pos) 84 { 85 if (array_key_exists($pos, $this->keys)) 86 { 87 $this->pos = $pos; 88 return true; 89 } 90 91 return false; 92 } 93 /** 94 * Returns the value at the current position, or NULL otherwise. 95 * @return mixed. 96 */ 97 public function getValue() 98 { 99 if (array_key_exists($this->pos, $this->values)) 100 return $this->values[$this->pos]; 101 else return null; 102 } 103 /** 104 * Gets the current numeric position within the array. 105 * @return int 106 */ 107 public function getPosition() 108 { 109 return $this->pos; 110 } 111} 112