aboutsummaryrefslogtreecommitdiff
path: root/includes/vendor/aura/sql/src/Iterator/AbstractIterator.php
blob: 895fd4e779405d67c5f791612b14f4dbed037526 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?php
/**
 *
 * This file is part of Aura for PHP.
 *
 * @license http://opensource.org/licenses/bsd-license.php BSD
 *
 */
namespace Aura\Sql\Iterator;

use Iterator;
use PDOStatement;

/**
 *
 * A base class for iterators.
 *
 * @package Aura.Sql
 *
 */
abstract class AbstractIterator implements Iterator
{
    /**
     *
     * PDO statement.
     *
     * @var PDOStatement
     *
     */
    protected $statement;

    /**
     *
     * Current row value.
     *
     * @var mixed
     *
     */
    protected $row;

    /**
     *
     * Current key.
     *
     * @var mixed
     *
     */
    protected $key = -1;

    /**
     *
     * Frees memory when object is destroyed.
     *
     */
    public function __destruct()
    {
        $this->statement->closeCursor();
        unset($this->statement);
    }

    /**
     *
     * Moves row set pointer to first element.
     *
     */
    public function rewind()
    {
        $this->key = -1;
        $this->next();
    }

    /**
     *
     * Returns value at current position.
     *
     * @return mixed
     *
     */
    public function current()
    {
        return $this->row;
    }

    /**
     *
     * Returns key at current position.
     *
     * @return mixed
     *
     */
    public function key()
    {
        return $this->key;
    }

    /**
     *
     * Fetches next row from statement.
     *
     */
    abstract public function next();

    /**
     *
     * Detects if iterator state is valid.
     *
     * @return bool
     *
     */
    public function valid()
    {
        return $this->row !== false;
    }
}