1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* ZfTable ( Module for Zend Framework 2) |
4
|
|
|
* |
5
|
|
|
* @copyright Copyright (c) 2013 Piotr Duda [email protected] |
6
|
|
|
* @license MIT License |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace ZfTable\Params; |
10
|
|
|
|
11
|
|
|
use ZfTable\Params\AbstractAdapter; |
12
|
|
|
use ZfTable\Params\AdapterInterface; |
13
|
|
|
use ZfTable\Table\Exception; |
14
|
|
|
|
15
|
|
|
class AdapterNewDatatables extends AbstractAdapter implements |
16
|
|
|
AdapterInterface, |
17
|
|
|
\Zend\Stdlib\InitializableInterface |
18
|
|
|
{ |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* |
22
|
|
|
* @var \ArrayObject | \Zend\Stdlib\ArrayObject |
23
|
|
|
*/ |
24
|
|
|
protected $object; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* |
28
|
|
|
* @var int |
29
|
|
|
*/ |
30
|
|
|
protected $page; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
protected $order; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* |
40
|
|
|
* @var string |
41
|
|
|
*/ |
42
|
|
|
protected $column; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* |
46
|
|
|
* @var int |
47
|
|
|
*/ |
48
|
|
|
protected $itemCountPerPage; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Quick search |
52
|
|
|
* @var string |
53
|
|
|
*/ |
54
|
|
|
protected $quickSearch; |
55
|
|
|
|
56
|
|
|
const DEFAULT_PAGE = 1; |
57
|
|
|
const DEFAULT_ORDER = 'asc'; |
58
|
|
|
const DEFAULT_ITEM_COUNT_PER_PAGE = 2; |
59
|
|
|
|
60
|
|
View Code Duplication |
public function __construct($object) |
|
|
|
|
61
|
|
|
{ |
62
|
|
|
if ($object instanceof \ArrayObject) { |
63
|
|
|
$this->object = $object; |
64
|
|
|
} elseif ($object instanceof \Zend\Stdlib\ArrayObject) { |
|
|
|
|
65
|
|
|
$this->object = $object; |
|
|
|
|
66
|
|
|
} else { |
67
|
|
|
throw new Exception\InvalidArgumentException('parameter must be instance of ArrayObject'); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Init method |
73
|
|
|
*/ |
74
|
|
|
public function init() |
75
|
|
|
{ |
76
|
|
|
$array = $this->object->toArray(); |
77
|
|
|
|
78
|
|
|
$this->page = (isset($array['start'])) ? ($array['start'] / $array['length'] + 1) : self::DEFAULT_PAGE; |
|
|
|
|
79
|
|
|
|
80
|
|
|
|
81
|
|
|
if (isset($array['order'][0]['column'])) { |
82
|
|
|
$headers = $this->getTable()->getHeaders(); |
83
|
|
|
$slice = array_slice($headers, $array['order'][0]['column'], 1); |
84
|
|
|
$this->column = key($slice); |
85
|
|
|
} |
86
|
|
|
$this->order = (isset($array['order'][0]['dir'])) ? $array['order'][0]['dir'] : self::DEFAULT_ORDER; |
87
|
|
|
$this->itemCountPerPage = (isset($array['start'])) ? $array['length'] : 20; |
88
|
|
|
$this->quickSearch = (isset($array['columns']['search']['value'])) ? $array['columns']['search']['value'] : ''; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Get page |
93
|
|
|
* |
94
|
|
|
* @return int |
95
|
|
|
*/ |
96
|
|
|
public function getPage() |
97
|
|
|
{ |
98
|
|
|
return $this->page; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Set page |
103
|
|
|
* |
104
|
|
|
* @param int $page |
105
|
|
|
* @return $this |
106
|
|
|
*/ |
107
|
|
|
public function setPage($page) |
108
|
|
|
{ |
109
|
|
|
$this->page = $page; |
110
|
|
|
return $this; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Get order |
115
|
|
|
* |
116
|
|
|
* @return string |
117
|
|
|
*/ |
118
|
|
|
public function getOrder() |
119
|
|
|
{ |
120
|
|
|
return $this->order; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Set asc or desc ordering |
125
|
|
|
* |
126
|
|
|
* @param string $order |
127
|
|
|
*/ |
128
|
|
|
public function setOrder($order) |
129
|
|
|
{ |
130
|
|
|
$this->order = $order; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Get column |
135
|
|
|
* |
136
|
|
|
* @return string |
137
|
|
|
*/ |
138
|
|
|
public function getColumn() |
139
|
|
|
{ |
140
|
|
|
return ($this->column == '') ? null : $this->column; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* |
145
|
|
|
* @param string $column |
146
|
|
|
* @return $this |
147
|
|
|
*/ |
148
|
|
|
public function setColumn($column) |
149
|
|
|
{ |
150
|
|
|
$this->column = $column; |
151
|
|
|
return $this; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Get item count per page |
156
|
|
|
* |
157
|
|
|
* @return int |
158
|
|
|
*/ |
159
|
|
|
public function getItemCountPerPage() |
160
|
|
|
{ |
161
|
|
|
return $this->itemCountPerPage; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* |
166
|
|
|
* @param int $itemCountPerPage |
167
|
|
|
*/ |
168
|
|
|
public function setItemCountPerPage($itemCountPerPage) |
169
|
|
|
{ |
170
|
|
|
$this->itemCountPerPage = $itemCountPerPage; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Return offset |
175
|
|
|
* |
176
|
|
|
* @return int |
177
|
|
|
*/ |
178
|
|
|
public function getOffset() |
179
|
|
|
{ |
180
|
|
|
return ($this->getPage() * $this->getItemCountPerPage()) - $this->getItemCountPerPage(); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Get quick search string |
185
|
|
|
* |
186
|
|
|
* @return string |
187
|
|
|
*/ |
188
|
|
|
public function getQuickSearch() |
189
|
|
|
{ |
190
|
|
|
return $this->quickSearch; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
public function getPureValueOfFilter($key) |
194
|
|
|
{ |
195
|
|
|
return $this->object[$key]; |
196
|
|
|
} |
197
|
|
|
} |
198
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.