Completed
Push — master ( d96bec...98160d )
by Joao
03:31
created

src/Model/BaseModelCollection.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace ByJG\AnyDataset\Model;
4
5
use ByJG\AnyDataset\Exception\IteratorException;
6
use ByJG\AnyDataset\Repository\GenericIterator;
7
use ByJG\AnyDataset\Repository\IteratorInterface;
8
use ByJG\AnyDataset\Repository\SingleRow;
9
use InvalidArgumentException;
10
11
abstract class BaseModelCollection extends GenericIterator
12
{
13
14
    protected $_items = array();
15
    private $_curRow = 0; //int
16
    protected $_type = null;
17
18
    // <editor-fold desc="Implementation of IteratorInterface interface based on GenericIterator">
19
20
    public function hasNext()
21
    {
22
        return ($this->_curRow < $this->count());
23
    }
24
25
    public function moveNext()
26
    {
27
        if ($this->hasNext()) {
28
            $singleRow = new SingleRow($this->_items[$this->_curRow++]);
29
            return $singleRow;
30
        } else {
31
            throw new IteratorException("Cannot move to next item");
32
        }
33
    }
34
35
    public function count()
36
    {
37
        return count($this->_items);
38
    }
39
40
    function key()
0 ignored issues
show
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
41
    {
42
        return $this->_curRow;
43
    }
44
45
    // </editor-fold>
46
47
    public function __construct($it = null, $type = 'BaseModel')
48
    {
49
        if (!is_null($it)) {
50
            if (!($it instanceof IteratorInterface)) {
51
                throw new InvalidArgumentException("You have to pass an IteratorInterface class");
52
            }
53
54
            // Check if the object is an instance of BaseModel
55
            $tObj = new $type();
56
            if (!($tObj instanceof BaseModel)) {
57
                throw new InvalidArgumentException("You have to pass a BaseModel descendant as type");
58
            }
59
60
            // Save the type to be used in the future
61
            $this->_type = $type;
62
63
            foreach ($it as $singleRow) {
0 ignored issues
show
The expression $it of type object<ByJG\AnyDataset\R...tory\IteratorInterface> is not traversable.
Loading history...
64
                $item = new $type($singleRow);
65
                $this->addItem($item);
66
            }
67
        }
68
    }
69
70
    // <editor-fold desc="Implementation of BaseModelCollection methods">
71
    public function getItems()
72
    {
73
        return $this->_items;
74
    }
75
76
    public function addItem($item)
77
    {
78
        $this->_items[] = $item;
79
    }
80
81
    public function getItem($index)
82
    {
83
        if (array_key_exists($index, $this->_items)) {
84
            return $this->_items[$index];
85
        } else {
86
            throw new IteratorException('The index is out of range.');
87
        }
88
    }
89
    // </editor-fold>
90
}
91