1<?php
2
3/**
4 * Swift Mailer MySQL Resultset Iterator
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 Mailer MySQL Resultset Iterator.
16 * Iterates over MySQL Resultset from mysql_query().
17 * @package Swift
18 * @author Chris Corbyn <chris@w3style.co.uk>
19 */
20class Swift_Iterator_MySQLResult implements Swift_Iterator
21{
22  /**
23   * The MySQL resource.
24   * @var resource
25   */
26  protected $resultSet;
27  /**
28   * The current row (array) in the resultset.
29   * @var array
30   */
31  protected $currentRow = array(null, null);
32  /**
33   * The current array position.
34   * @var int
35   */
36  protected $pos = -1;
37  /**
38   * The total number of rows in the resultset.
39   * @var int
40   */
41  protected $numRows = 0;
42
43  /**
44   * Ctor.
45   * @param resource The resultset iterate over.
46   */
47  public function __construct($rs)
48  {
49    $this->resultSet = $rs;
50    $this->numRows = mysql_num_rows($rs);
51  }
52  /**
53   * Get the resultset.
54   * @return resource
55   */
56  public function getResultSet()
57  {
58    return $this->resultSet;
59  }
60  /**
61   * Returns true if there is a value after the current one.
62   * @return boolean
63   */
64  public function hasNext()
65  {
66    return (($this->pos + 1) < $this->numRows);
67  }
68  /**
69   * Moves to the next array element if possible.
70   * @return boolean
71   */
72  public function next()
73  {
74    if ($this->hasNext())
75    {
76      $this->currentRow = mysql_fetch_array($this->resultSet);
77      $this->pos++;
78      return true;
79    }
80
81    return false;
82  }
83  /**
84   * Goes directly to the given element in the array if possible.
85   * @param int Numeric position
86   * @return boolean
87   */
88  public function seekTo($pos)
89  {
90    if ($pos >= 0 && $pos < $this->numRows)
91    {
92      mysql_data_seek($this->resultSet, $pos);
93      $this->currentRow = mysql_fetch_array($this->resultSet);
94      mysql_data_seek($this->resultSet, $pos);
95      $this->pos = $pos;
96      return true;
97    }
98
99    return false;
100  }
101  /**
102   * Returns the value at the current position, or NULL otherwise.
103   * @return mixed.
104   */
105  public function getValue()
106  {
107    $row = $this->currentRow;
108    if ($row[0] !== null)
109      return new Swift_Address($row[0], isset($row[1]) ? $row[1] : null);
110    else
111      return null;
112  }
113  /**
114   * Gets the current numeric position within the array.
115   * @return int
116   */
117  public function getPosition()
118  {
119    return $this->pos;
120  }
121}
122