1 | <?php |
||
13 | abstract class DataComponent { |
||
14 | |||
15 | /** @var Builder */ |
||
16 | protected $_queryBuilder; |
||
17 | |||
18 | /** @var Request */ |
||
19 | protected $_request; |
||
20 | |||
21 | /** @var bool */ |
||
22 | protected $_rememberState = false; |
||
23 | |||
24 | /** @var string */ |
||
25 | protected $_rememberKey = 'global'; |
||
26 | |||
27 | /** |
||
28 | * Everything that needs to be read when the state is remembered. |
||
29 | * Is called before _afterInit(), so that session values can be overriden. |
||
30 | */ |
||
31 | protected function _readFromSession() |
||
32 | { |
||
33 | |||
34 | } |
||
35 | |||
36 | /** |
||
37 | * Use this function to save your state in the session. |
||
38 | * This is called just before rendering, so all dynamically added stuff etc. is considered. |
||
39 | */ |
||
40 | protected function _storeInSession() |
||
41 | { |
||
42 | |||
43 | } |
||
44 | |||
45 | /** |
||
46 | * Initalize fields after the query builder instance and the request is set. |
||
47 | * |
||
48 | * TODO: Refactor.. |
||
49 | */ |
||
50 | protected function _afterInit() |
||
51 | { |
||
52 | |||
53 | } |
||
54 | |||
55 | /** |
||
56 | * You cannot count the data when ordering. |
||
57 | * Disables ordering temporary. |
||
58 | * |
||
59 | * @return int |
||
60 | */ |
||
61 | 2 | public function getQueryCount(): int |
|
62 | { |
||
63 | /** @var \Illuminate\Database\Query\Builder $query */ |
||
64 | 2 | $query = $this->_queryBuilder->getQuery(); |
|
65 | |||
66 | 2 | $oldOrders = $query->orders; |
|
67 | 2 | $oldLimit = $query->limit; |
|
68 | 2 | $oldOffset = $query->offset; |
|
69 | |||
70 | 2 | $query->orders = null; |
|
71 | 2 | $query->limit = null; |
|
72 | 2 | $query->offset = null; |
|
73 | |||
74 | 2 | $dataCount = $query->count(); |
|
75 | |||
76 | 2 | $query->orders = $oldOrders; |
|
77 | 2 | $query->limit = $oldLimit; |
|
78 | 2 | $query->offset = $oldOffset; |
|
79 | |||
80 | 2 | return $dataCount; |
|
81 | } |
||
82 | |||
83 | /** |
||
84 | * @param Builder $queryBuilder |
||
85 | * @param Request $request |
||
86 | */ |
||
87 | 4 | public function init(Builder $queryBuilder, Request $request) |
|
1 ignored issue
–
show
|
|||
88 | { |
||
89 | 4 | $this->_request = $request; |
|
90 | 4 | $this->_queryBuilder = $queryBuilder; |
|
91 | |||
92 | 4 | if ($this->_rememberState) |
|
93 | { |
||
94 | $this->_readFromSession(); |
||
95 | } |
||
96 | 4 | $this->_afterInit(); |
|
97 | 4 | } |
|
98 | |||
99 | /** |
||
100 | * Remember the state of the data component. |
||
101 | * |
||
102 | * @return $this |
||
103 | */ |
||
104 | public function remember() |
||
105 | { |
||
106 | $this->_rememberState = true; |
||
107 | |||
108 | return $this; |
||
109 | } |
||
110 | |||
111 | /** |
||
112 | * Forget the state of the data component. |
||
113 | * |
||
114 | * @return $this |
||
115 | */ |
||
116 | public function forget() |
||
117 | { |
||
118 | $this->_rememberState = false; |
||
119 | if ($this->_request !== null) |
||
120 | { |
||
121 | SessionHelper::removeState($this->_request, $this->_rememberKey); |
||
122 | } |
||
123 | |||
124 | return $this; |
||
125 | } |
||
126 | |||
127 | 3 | public function transformData() |
|
128 | { |
||
129 | 3 | if ($this->_rememberState) |
|
130 | { |
||
131 | $this->_storeInSession(); |
||
132 | } |
||
133 | 3 | $this->shapeData(); |
|
134 | 3 | } |
|
135 | |||
136 | /** |
||
137 | * @return Builder |
||
138 | */ |
||
139 | abstract public function shapeData(): Builder; |
||
140 | |||
141 | /** |
||
142 | * @return string |
||
143 | */ |
||
144 | abstract public function render(): string; |
||
145 | } |