1: <?php
2:
3: namespace PHPixie\ORM\Loaders\Loader\Repository;
4:
5: class DataIterator extends \PHPixie\ORM\Loaders\Loader\Repository
6: {
7: protected $dataIterator;
8: protected $currentOffset = null;
9: protected $currentEntity = null;
10: protected $reachedEnd = false;
11:
12: public function __construct($loaders, $repository, $dataIterator)
13: {
14: parent::__construct($loaders, $repository);
15: $this->dataIterator = $dataIterator;
16: }
17:
18: public function offsetExists($offset)
19: {
20: if($this->currentOffset === $offset)
21: return true;
22:
23: $isFirst = $this->currentOffset === null;
24: if (!($this->currentOffset + 1 === $offset || $isFirst && $offset === 0))
25: throw new \PHPixie\ORM\Exception\Loader("Entities can only be accessed in sequential order when using this loader.");
26:
27: if(!$isFirst)
28: $this->dataIterator->next();
29:
30: $this->currentEntity = null;
31: if (!$this->dataIterator->valid()) {
32: $this->reachedEnd = true;
33: return false;
34: }
35:
36: if($isFirst) {
37: $this->currentOffset = 0;
38: }else{
39: $this->currentOffset++;
40: }
41:
42: return true;
43: }
44:
45: public function getByOffset($offset)
46: {
47: if(!$this->offsetExists($offset))
48: throw new \PHPixie\ORM\Exception\Loader("Offset $offset does not exist.");
49:
50: if($this->currentEntity === null) {
51: $data = $this->dataIterator->current();
52: $this->currentEntity = $this->loadEntity($data);
53: }
54:
55: return $this->currentEntity;
56: }
57:
58: public function dataIterator()
59: {
60: return $this->dataIterator;
61: }
62: }
63: