Completed
Pull Request — master (#2)
by Joao
05:22
created

SparQLIterator::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
3
namespace ByJG\AnyDataset\Dataset;
4
5
use ByJG\AnyDataset\Exception\IteratorException;
6
use SparQL\Result;
7
8
class SparQLIterator extends GenericIterator
9
{
10
11
    /**
12
     * @var Result
13
     */
14
    private $sparqlQuery;
15
16
    /**
17
     * Enter description here...
18
     *
19
     * @var int
20
     */
21
    private $current = 0;
22
23 2
    public function __construct(Result $sparqlQuery)
24
    {
25 2
        $this->sparqlQuery = $sparqlQuery;
26
27 2
        $this->current = 0;
28 2
    }
29
30 2
    public function count()
31
    {
32 2
        return ($this->sparqlQuery->numRows());
33
    }
34
35
    /**
36
     * @access public
37
     * @return bool
38
     */
39 2
    public function hasNext()
40
    {
41 2
        if ($this->current < $this->count()) {
42 2
            return true;
43
        }
44
45 1
        return false;
46
    }
47
48
    /**
49
     * @access public
50
     * @return Row
51
     * @throws IteratorException
52
     */
53 1
    public function moveNext()
54
    {
55 1
        if (!$this->hasNext()) {
56
            throw new IteratorException("No more records. Did you used hasNext() before moveNext()?");
57
        }
58
59 1
        if ($row = $this->sparqlQuery->fetchArray()) {
60 1
            $sr = new Row($row);
61 1
            $this->current++;
62 1
            return $sr;
63
        } else {
64
            throw new IteratorException("No more records. Unexpected behavior.");
65
        }
66
    }
67
68
    public function key()
69
    {
70
        return $this->current;
71
    }
72
}
73