-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathResultSet.php
More file actions
84 lines (69 loc) · 1.83 KB
/
Copy pathResultSet.php
File metadata and controls
84 lines (69 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<?php
namespace ADT\DoctrineComponents\QueryObject;
use ArrayIterator;
use Doctrine\ORM\NonUniqueResultException;
use Doctrine\ORM\NoResultException;
use Exception;
use IteratorAggregate;
use Nette\Utils\Paginator;
use Traversable;
class ResultSet implements IteratorAggregate
{
private QueryObject $qo;
private int $page;
private ?int $itemsPerPage;
private ?int $count = null;
private ?Traversable $results = null;
private ?Paginator $paginator = null;
public function __construct(QueryObject $qo, int $page, int $itemsPerPage)
{
$this->qo = $qo;
$this->page = $page;
$this->itemsPerPage = $itemsPerPage;
}
/**
* @throws NonUniqueResultException
* @throws NoResultException
* @throws PageIsOutOfRangeException
* @throws Exception
*/
public function getPaginator(): Paginator
{
if ($this->paginator) {
return $this->paginator;
}
$count = $this->count();
$paginator = new Paginator();
$paginator->setItemCount($count);
$paginator->setPage($this->page);
$paginator->setItemsPerPage($this->itemsPerPage);
if ($paginator->getPage() !== $this->page)
{
throw new PageIsOutOfRangeException('Page number is out of range. Page number '.$this->page . ' is not in the range ('.$paginator->getFirstPage().', '.$paginator->getLastPage().')');
}
return $this->paginator = $paginator;
}
/**
* @throws NonUniqueResultException
* @throws NoResultException
* @throws Exception
*/
public function count(): int
{
if ($this->count !== null) {
return $this->count;
}
return $this->count = $this->qo->count();
}
/**
* @throws Exception
*/
public function getIterator(): Traversable
{
if ($this->results !== null) {
return $this->results;
}
$this->results = new ArrayIterator($this->qo->fetch($this->itemsPerPage, $this->itemsPerPage * ($this->page - 1)));
return $this->results;
}
}