ArrayToObjectIterator::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 2
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
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
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
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