Completed
Push — master ( 679ef8...32b48d )
by Peter
09:25
created

ResultSet::rewind()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Predictator\AssociationRule;
4
5
6
class ResultSet implements \Countable, \Iterator, \ArrayAccess
7
{
8
	/**
9
	 * @var Result[]
10
	 */
11
	private $results = array();
12
13
	/**
14
	 * @param Result $result
15
	 */
16
	public function addResult(Result $result)
17
	{
18
		$this->results[] = $result;
19
	}
20
21
	/**
22
	 * @var int
23
	 */
24
	protected $currentIndex = 0;
25
26
	/**
27
	 * Return the current element
28
	 * @link http://php.net/manual/en/iterator.current.php
29
	 * @return mixed Can return any type.
30
	 * @since 5.0.0
31
	 */
32
	public function current()
33
	{
34
		return array_values($this->results)[$this->currentIndex];
35
	}
36
	/**
37
	 * Move forward to next element
38
	 * @link http://php.net/manual/en/iterator.next.php
39
	 * @return void Any returned value is ignored.
40
	 * @since 5.0.0
41
	 */
42
	public function next()
43
	{
44
		$this->currentIndex++;
45
	}
46
	/**
47
	 * Return the key of the current element
48
	 * @link http://php.net/manual/en/iterator.key.php
49
	 * @return mixed scalar on success, or null on failure.
50
	 * @since 5.0.0
51
	 */
52
	public function key()
53
	{
54
		return array_keys($this->results)[$this->currentIndex];
55
	}
56
	/**
57
	 * Checks if current position is valid
58
	 * @link http://php.net/manual/en/iterator.valid.php
59
	 * @return boolean The return value will be casted to boolean and then evaluated.
60
	 * Returns true on success or false on failure.
61
	 * @since 5.0.0
62
	 */
63
	public function valid()
64
	{
65
		$features = array_values($this->results);
66
		return isset($features[$this->currentIndex]);
67
	}
68
	/**
69
	 * Rewind the Iterator to the first element
70
	 * @link http://php.net/manual/en/iterator.rewind.php
71
	 * @return void Any returned value is ignored.
72
	 * @since 5.0.0
73
	 */
74
	public function rewind()
75
	{
76
		$this->currentIndex = 0;
77
	}
78
	/**
79
	 * Whether a offset exists
80
	 * @link http://php.net/manual/en/arrayaccess.offsetexists.php
81
	 * @param mixed $offset <p>
82
	 * An offset to check for.
83
	 * </p>
84
	 * @return boolean true on success or false on failure.
85
	 * </p>
86
	 * <p>
87
	 * The return value will be casted to boolean if non-boolean was returned.
88
	 * @since 5.0.0
89
	 */
90
	public function offsetExists($offset)
91
	{
92
		return isset($this->results[$offset]);
93
	}
94
	/**
95
	 * Offset to retrieve
96
	 * @link http://php.net/manual/en/arrayaccess.offsetget.php
97
	 * @param mixed $offset <p>
98
	 * The offset to retrieve.
99
	 * </p>
100
	 * @return Result
101
	 * @since 5.0.0
102
	 */
103
	public function offsetGet($offset)
104
	{
105
		return $this->results[$offset];
106
	}
107
	/**
108
	 * Offset to set
109
	 * @link http://php.net/manual/en/arrayaccess.offsetset.php
110
	 * @param mixed $offset <p>
111
	 * The offset to assign the value to.
112
	 * </p>
113
	 * @param Result $value <p>
114
	 * The value to set.
115
	 * </p>
116
	 * @throws \Exception
117
	 * @since 5.0.0
118
	 */
119
	public function offsetSet($offset, $value)
120
	{
121
		if (!$value instanceof Result) {
122
			throw new \Exception('The value must be Result');
123
		}
124
		if (!is_null($offset)) {
125
			$this->results[$offset] = $value;
126
			return;
127
		}
128
		$this->results[] = $value;
129
	}
130
	/**
131
	 * Offset to unset
132
	 * @link http://php.net/manual/en/arrayaccess.offsetunset.php
133
	 * @param mixed $offset <p>
134
	 * The offset to unset.
135
	 * </p>
136
	 * @return void
137
	 * @since 5.0.0
138
	 */
139
	public function offsetUnset($offset)
140
	{
141
		if (isset($this->results[$offset])) {
142
			unset($this->results[$offset]);
143
		}
144
	}
145
146
	/**
147
	 * Count elements of an object
148
	 * @link http://php.net/manual/en/countable.count.php
149
	 * @return int The custom count as an integer.
150
	 * </p>
151
	 * <p>
152
	 * The return value is cast to an integer.
153
	 * @since 5.1.0
154
	 */
155
	public function count()
156
	{
157
		return count($this->results);
158
	}
159
}