Rows   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 135
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 1
dl 0
loc 135
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 16 4
A current() 0 16 4
A next() 0 3 1
A key() 0 3 1
A valid() 0 3 1
A rewind() 0 3 1
A count() 0 3 1
A asArray() 0 8 2
1
<?php
2
namespace evseevnn\Cassandra\Protocol\Response;
3
4
class Rows implements \Iterator, \Countable {
5
6
	/**
7
	 * @var array
8
	 */
9
	private $columns = [];
10
11
	/**
12
	 * @var int
13
	 */
14
	private $columnCount;
15
16
	/**
17
	 * @var int
18
	 */
19
	private $rowCount;
20
21
	/**
22
	 * @var int
23
	 */
24
	private $current = 0;
25
26
	/**
27
	 * @var array
28
	 */
29
	private $rows = [];
30
31
	public function __construct(DataStream $stream, array $metadata) {
32
		$this->columns = $metadata['columns'];
33
		$this->columnCount = $metadata['columnCount'];
34
		$this->rowCount = $stream->readInt();
35
		for ($i = 0; $i < $this->rowCount; ++$i) {
36
			$row = array();
37
			for ($j = 0; $j < $this->columnCount; ++$j) {
38
				try {
39
					$row[$this->columns[$j]['name']] = $stream->readBytes();
40
				} catch (\Exception $e) {
41
					$row[$this->columns[$j]['name']] = null;
42
				}
43
			}
44
			$this->rows[] = $row;
45
		}
46
	}
47
48
	/**
49
	 * (PHP 5 &gt;= 5.0.0)<br/>
50
	 * Return the current element
51
	 * @link http://php.net/manual/en/iterator.current.php
52
	 * @throws \OutOfRangeException
53
	 * @return mixed Can return any type.
54
	 */
55
	public function current() {
56
		if (!isset($this->rows[$this->current])) {
57
			throw new \OutOfRangeException('Invalid index');
58
		}
59
		$row = $this->rows[$this->current];
60
		for ($i = 0; $i < $this->columnCount; ++$i) {
61
			try {
62
				$data = new DataStream($this->rows[$this->current][$this->columns[$i]['name']]);
63
				$row[$this->columns[$i]['name']] = $data->readByType($this->columns[$i]['type']);
64
			} catch (\Exception $e) {
65
				trigger_error($e->getMessage());
66
				$row[$this->columns[$i]['name']] = null;
67
			}
68
		}
69
		return $row;
70
	}
71
72
	/**
73
	 * (PHP 5 &gt;= 5.0.0)<br/>
74
	 * Move forward to next element
75
	 * @link http://php.net/manual/en/iterator.next.php
76
	 * @return void Any returned value is ignored.
77
	 */
78
	public function next() {
79
		++$this->current;
80
	}
81
82
	/**
83
	 * (PHP 5 &gt;= 5.0.0)<br/>
84
	 * Return the key of the current element
85
	 * @link http://php.net/manual/en/iterator.key.php
86
	 * @return mixed scalar on success, or null on failure.
87
	 */
88
	public function key() {
89
		return $this->current;
90
	}
91
92
	/**
93
	 * (PHP 5 &gt;= 5.0.0)<br/>
94
	 * Checks if current position is valid
95
	 * @link http://php.net/manual/en/iterator.valid.php
96
	 * @return boolean The return value will be casted to boolean and then evaluated.
97
	 * Returns true on success or false on failure.
98
	 */
99
	public function valid() {
100
		return $this->current < $this->rowCount;
101
	}
102
103
	/**
104
	 * (PHP 5 &gt;= 5.0.0)<br/>
105
	 * Rewind the Iterator to the first element
106
	 * @link http://php.net/manual/en/iterator.rewind.php
107
	 * @return void Any returned value is ignored.
108
	 */
109
	public function rewind() {
110
		$this->current = 0;
111
	}
112
113
	/**
114
	 * (PHP 5 &gt;= 5.1.0)<br/>
115
	 * Count elements of an object
116
	 * @link http://php.net/manual/en/countable.count.php
117
	 * @return int The custom count as an integer.
118
	 * </p>
119
	 * <p>
120
	 * The return value is cast to an integer.
121
	 */
122
	public function count() {
123
		return $this->rowCount;
124
	}
125
126
	/**
127
	 * Return rows as array
128
	 * @return array
129
	 */
130
	public function asArray() {
131
		$items = [];
132
		foreach($this as $item) {
133
			$items[] = $item;
134
		}
135
136
		return $items;
137
	}
138
}