IteratorTrait   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 50
rs 10
c 0
b 0
f 0
wmc 5

5 Methods

Rating   Name   Duplication   Size   Complexity  
A valid() 0 2 1
A next() 0 2 1
A key() 0 2 1
A current() 0 2 1
A rewind() 0 2 1
1
<?php
2
/**
3
 * Trait IteratorTrait
4
 *
5
 * @filesource   IteratorTrait.php
6
 * @created      03.12.2017
7
 * @package      chillerlan\Traits\Interfaces
8
 * @author       Smiley <[email protected]>
9
 * @copyright    2017 Smiley
10
 * @license      MIT
11
 */
12
13
namespace chillerlan\Traits\Interfaces;
14
15
/**
16
 * @extends \Traversable
17
 * @implements \Iterator
18
 *
19
 * @link http://php.net/manual/class.traversable.php
20
 */
21
trait IteratorTrait{
22
23
	/**
24
	 * @var array
25
	 */
26
	protected $array = [];
27
28
	/**
29
	 * @var int
30
	 */
31
	protected $offset = 0;
32
33
	/**
34
	 * @link  http://php.net/manual/iterator.current.php
35
	 * @inheritdoc
36
	 */
37
	public function current(){
38
		return $this->array[$this->offset] ?? null;
39
	}
40
41
	/**
42
	 * @link  http://php.net/manual/iterator.next.php
43
	 * @inheritdoc
44
	 */
45
	public function next():void{
46
		$this->offset++;
47
	}
48
49
	/**
50
	 * @link  http://php.net/manual/iterator.key.php
51
	 * @inheritdoc
52
	 */
53
	public function key(){
54
		return $this->offset;
55
	}
56
57
	/**
58
	 * @link  http://php.net/manual/iterator.valid.php
59
	 * @inheritdoc
60
	 */
61
	public function valid():bool{
62
		return \array_key_exists($this->offset, $this->array);
63
	}
64
65
	/**
66
	 * @link  http://php.net/manual/iterator.rewind.php
67
	 * @inheritdoc
68
	 */
69
	public function rewind():void{
70
		$this->offset = 0;
71
	}
72
73
}
74