Completed
Pull Request — master (#76)
by
unknown
02:33
created

ResultIterator::key()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Cassandra\Response;
4
5
/**
6
 * @author Dennis Birkholz <[email protected]>
7
 */
8
class ResultIterator implements \Iterator {
9
    /**
10
     * Stream containing the raw result data
11
     * 
12
     * @var StreamReader
13
     */
14
    private $stream;
15
    
16
    /**
17
     * Offset to start reading data in this stream
18
     * 
19
     * @var int
20
     */
21
    private $offset;
22
    
23
    /**
24
     * Metadata generated by the creating Result
25
     * 
26
     * @var array
27
     */
28
    private $metadata;
29
    
30
    /**
31
     * Class to use for each row of data
32
     * 
33
     * @var string
34
     */
35
    private $rowClass;
36
    
37
    /**
38
     * Number of available rows in the resultset
39
     * 
40
     * @var int
41
     */
42
    private $count;
43
    
44
    /**
45
     * Current row
46
     * 
47
     * @var int
48
     */
49
    private $row = 0;
50
    
51
    
52
    public function __construct(StreamReader $stream, array $metadata, $rowClass) {
0 ignored issues
show
Unused Code introduced by
The parameter $rowClass is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
53
        $this->stream = clone $stream;
54
        $this->metadata = $metadata;
55
        $this->count = $this->stream->readInt();
56
        $this->offset = $this->stream->pos();
57
        $this->rewind();
58
    }
59
60
    public function current() {
61
        $data = [];
62
        
63
        foreach ($this->metadata['columns'] as $column) {
64
            $data[$column['name']] = $this->stream->readValue($column['type']);
65
        }
66
        
67
        $rowClass = $this->rowClass;
68
        return ($rowClass === null ? $data : new $rowClass($data));
69
    }
70
71
    /**
72
     * The current position in this result set
73
     * 
74
     * @return int
75
     */
76
    public function key() {
77
        return $this->row;
78
    }
79
80
    /**
81
     * Move forward to next element
82
     * 
83
     * @return void
84
     */
85
    public function next() {
86
        $this->row++;
87
    }
88
89
    /**
90
     * Reset the result set
91
     * 
92
     * @return void
93
     */
94
    public function rewind() {
95
        $this->row = 0;
96
        $this->stream->offset($this->offset);
97
    }
98
99
    /**
100
     * Checks if current position is valid
101
     * 
102
     * @return boolean
103
     */
104
    public function valid() {
105
        return (($this->row >= 0) && ($this->row < $this->count));
106
    }
107
}
108