1: <?php
2:
3: namespace Deimos\Paginate;
4:
5: use Deimos\ORM\Queries\Query;
6:
7: class Paginate extends Pager
8: {
9:
10: /**
11: * @var Query
12: */
13: protected $query;
14:
15: /**
16: * @param Query $query
17: */
18: protected function setQuery($query)
19: {
20: $this->query = $query;
21: }
22:
23: /**
24: * @return Query
25: */
26: protected function query()
27: {
28: return clone $this->query;
29: }
30:
31: /**
32: * reset pager
33: */
34: protected function reset()
35: {
36: parent::reset();
37: $this->setQuery(null);
38: }
39:
40: /**
41: * @param bool $asObject
42: *
43: * @return array
44: * @throws \InvalidArgumentException
45: */
46: public function currentItems($asObject = true)
47: {
48: if (!$this->isLoaded())
49: {
50: throw new \InvalidArgumentException('Data is not load');
51: }
52:
53: if ($this->query === null)
54: {
55: return $this->slice();
56: }
57:
58: return $this->query()
59: ->limit($this->limit)
60: ->offset($this->offset())
61: ->find($asObject);
62: }
63:
64: /**
65: * @param array $storage
66: *
67: * @return $this
68: */
69: public function arrayPager(array $storage)
70: {
71: $this->reset();
72: $this->loaded = true;
73: $this->storage = $storage;
74:
75: return $this;
76: }
77:
78: /**
79: * @param Query $query
80: *
81: * @return $this
82: */
83: public function queryPager(Query $query)
84: {
85: $this->reset();
86: $this->setQuery($query);
87:
88: $this->loaded = true;
89:
90: return $this;
91: }
92:
93: /**
94: * @param Query $query
95: *
96: * @return $this
97: */
98: protected function queryClone(Query $query)
99: {
100: return $this->queryPager(clone $query);
101: }
102:
103: /**
104: * @return int
105: */
106: protected function count()
107: {
108: if ($this->query)
109: {
110: return $this->query()->count();
111: }
112:
113: return count($this->storage);
114: }
115:
116: /**
117: * @return int
118: */
119: public function itemCount()
120: {
121: if (!$this->itemCount)
122: {
123: $this->itemCount = $this->count();
124: }
125:
126: return $this->itemCount;
127: }
128:
129: }