Passed
Branch master (d51fdb)
by Joao
05:45 queued 02:33
created

ArrayDatasetIterator::moveNext()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 21
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 3.0026

Importance

Changes 0
Metric Value
dl 0
loc 21
ccs 14
cts 15
cp 0.9333
rs 9.3142
c 0
b 0
f 0
cc 3
eloc 15
nc 3
nop 0
crap 3.0026
1
<?php
2
3
namespace ByJG\AnyDataset\Dataset;
4
5
use InvalidArgumentException;
6
7
class ArrayDatasetIterator extends GenericIterator
8
{
9
10
    /**
11
     * @var array
12
     */
13
    protected $rows;
14
15
    /**
16
     * Enter description here...
17
     *
18
     * @var array
19
     */
20
    protected $keys;
21
22
    /**
23
      /* @var int
24
     */
25
    protected $currentRow;
26
27
    /**
28
     * @param $rows
29
     */
30 10
    public function __construct($rows)
31
    {
32 10
        if (!is_array($rows)) {
33
            throw new InvalidArgumentException("ArrayDatasetIterator must receive an array");
34
        }
35 10
        $this->currentRow = 0;
36 10
        $this->rows = $rows;
37 10
        $this->keys = array_keys($rows);
38 10
    }
39
40
    /**
41
     * @return int
42
     */
43 10
    public function count()
44
    {
45 10
        return count($this->rows);
46
    }
47
48
    /**
49
     * @return bool
50
     */
51 10
    public function hasNext()
52
    {
53 10
        return ($this->currentRow < $this->count());
54
    }
55
56
    /**
57
     * @return Row
58
     * @throws \ByJG\Serializer\Exception\InvalidArgumentException
59
     * @throws \ByJG\Util\Exception\XmlUtilException
60
     */
61 5
    public function moveNext()
62
    {
63 5
        if (!$this->hasNext()) {
64
            return null;
65
        }
66
67 5
        $key = $this->keys[$this->currentRow];
68 5
        $cols = $this->rows[$key];
69
70 5
        $any = new AnyDataset();
71 5
        $any->appendRow();
72 5
        $any->addField("__id", $this->currentRow);
73 5
        $any->addField("__key", $key);
74 5
        foreach ($cols as $key => $value) {
75 5
            $any->addField(strtolower($key), $value);
76
        }
77 5
        $iterator = $any->getIterator(null);
78 5
        $singleRow = $iterator->moveNext();
79 5
        $this->currentRow++;
80 5
        return $singleRow;
81
    }
82
83
    public function key()
84
    {
85
        return $this->currentRow;
86
    }
87
}
88