Iterator   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 18
dl 0
loc 37
rs 10
c 0
b 0
f 0
wmc 7

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A current() 0 2 1
A next() 0 2 1
A valid() 0 2 1
A rewind() 0 2 1
A key() 0 10 2
1
<?php
2
3
namespace Maphper;
4
5
class Iterator implements \Iterator {
6
	private $array;
7
	private $pk;
8
	private $iterator = 0;
9
10
	public function __construct(array $array, $pk) {
11
		$this->array = $array;
12
		$this->pk = $pk;
13
	}
14
15
	public function current() {
16
		return $this->array[$this->iterator];
17
	}
18
19
	public function key() {
20
		if (count($this->pk) == 1) {
21
			$pk = end($this->pk);
22
			return $this->array[$this->iterator]->$pk;
23
		}
24
		else {
25
			$current = $this->current();
26
			return array_map(function($pkName) use ($current) {
27
				return $current->$pkName;
28
			}, $this->pk);
29
		}
30
	}
31
32
	public function next() {
33
		++$this->iterator;
34
	}
35
36
	public function valid() {
37
		return isset($this->array[$this->iterator]);
38
	}
39
40
	public function rewind() {
41
		$this->iterator = 0;
42
	}
43
}
44