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

SparQLIterator   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 80%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 4
dl 0
loc 65
ccs 16
cts 20
cp 0.8
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A count() 0 4 1
A hasNext() 0 8 2
A moveNext() 0 14 3
A key() 0 4 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