|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Transforme un tableau de valeur en objet |
|
5
|
|
|
* |
|
6
|
|
|
* @author [email protected] |
|
7
|
|
|
* @author [email protected] |
|
8
|
|
|
*/ |
|
9
|
|
|
class ArrayToObjectIterator extends \IteratorIterator implements \Countable, \ArrayAccess, \SeekableIterator |
|
|
|
|
|
|
10
|
|
|
{ |
|
11
|
|
|
private $className; |
|
12
|
|
|
private $current; |
|
13
|
|
|
private $row = 0; |
|
14
|
|
|
|
|
15
|
|
|
public function __construct(\Iterator $fIterator, $className) |
|
16
|
|
|
{ |
|
17
|
|
|
parent::__construct($fIterator); |
|
18
|
|
|
$this->className = $className; |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
private function defineCurrentToObject() |
|
22
|
|
|
{ |
|
23
|
|
|
$className = $this->className; |
|
24
|
|
|
$this->current = $this->getInnerIterator()->valid() |
|
25
|
|
|
? $className::objectsFromArray($this->getInnerIterator()->current(), $this->className) |
|
26
|
|
|
: null; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
public function rewind() |
|
30
|
|
|
{ |
|
31
|
|
|
$this->row = 0; |
|
32
|
|
|
$this->getInnerIterator()->rewind(); |
|
33
|
|
|
$this->defineCurrentToObject(); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
public function current() |
|
37
|
|
|
{ |
|
38
|
|
|
return $this->current; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public function valid() |
|
42
|
|
|
{ |
|
43
|
|
|
return $this->getInnerIterator()->valid(); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function next() |
|
47
|
|
|
{ |
|
48
|
|
|
$this->getInnerIterator()->next(); |
|
49
|
|
|
if ($this->getInnerIterator()->valid()) { |
|
50
|
|
|
$this->defineCurrentToObject(); |
|
51
|
|
|
} else { |
|
52
|
|
|
$this->current = null; |
|
53
|
|
|
} |
|
54
|
|
|
$this->row++; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public function count() |
|
58
|
|
|
{ |
|
59
|
|
|
return iterator_count($this->getInnerIterator()); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @return int |
|
64
|
|
|
*/ |
|
65
|
|
|
public function key() |
|
66
|
|
|
{ |
|
67
|
|
|
return (int)$this->row; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @param mixed $offset |
|
72
|
|
|
* @return bool |
|
73
|
|
|
*/ |
|
74
|
|
|
public function offsetExists($offset) |
|
75
|
|
|
{ |
|
76
|
|
|
return isset($this->getInnerIterator()[$offset]); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @param mixed $offset |
|
81
|
|
|
* @return array|object |
|
82
|
|
|
*/ |
|
83
|
|
|
public function offsetGet($offset) |
|
84
|
|
|
{ |
|
85
|
|
|
return $this->offsetExists($offset) |
|
86
|
|
|
? \Model::objectsFromArray($this->getInnerIterator()[$offset], $this->className) |
|
87
|
|
|
: null; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @param mixed $offset |
|
92
|
|
|
* @param mixed $value |
|
93
|
|
|
* @throws Exception |
|
94
|
|
|
*/ |
|
95
|
|
|
public function offsetSet($offset, $value) |
|
96
|
|
|
{ |
|
97
|
|
|
throw new Exception("Unable to set index on iterator : offset $offset / value $value"); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @param mixed $offset |
|
102
|
|
|
* @throws Exception |
|
103
|
|
|
*/ |
|
104
|
|
|
public function offsetUnset($offset) |
|
105
|
|
|
{ |
|
106
|
|
|
throw new Exception("Unable to unset index on iterator : offset $offset"); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* @param int $position |
|
111
|
|
|
*/ |
|
112
|
|
|
public function seek($position) |
|
113
|
|
|
{ |
|
114
|
|
|
$this->row = (int)$position; |
|
115
|
|
|
$iterator = $this->getInnerIterator(); |
|
116
|
|
|
if ($iterator instanceof \SeekableIterator) { |
|
117
|
|
|
$iterator->seek($position); |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.