-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathRequestFactory.php
178 lines (159 loc) · 4.48 KB
/
RequestFactory.php
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
<?php
/**
* Datatables PHP Model
*/
namespace Webinv\Datatables;
use Webinv\Datatables\Request\Column;
use Webinv\Datatables\Request\Order;
use Webinv\Datatables\Request\Search;
use Psr\Http\Message as Psr;
/**
* Class RequestFactory
* @see https://datatables.net/manual/server-side
* @author Krzysztof Kardasz <[email protected]>
*/
class RequestFactory
{
private const DRAW = 'draw';
private const COLUMNS = 'columns';
private const ORDER = 'order';
private const ORDER_COLUMN = 'column';
private const ORDER_DIR = 'dir';
private const START = 'start';
private const LENGTH = 'length';
private const SEARCH = 'search';
private const SEARCH_VALUE = 'value';
private const SEARCH_REGEX = 'regex';
private const COLUMNS_DATA = 'data';
private const COLUMNS_NAME = 'name';
private const COLUMNS_SEARCHABLE = 'searchable';
private const COLUMNS_ORDERABLE = 'orderable';
/** @var array */
private $request = [];
/**
* DataTablesRequestFactory constructor.
*
* @param array $request
*/
public function __construct(array $request)
{
$this->request = $request;
}
/**
* @return RequestInterface
*/
public static function fromGlobals() : RequestInterface
{
return (new self($_GET))->create();
}
/**
* @param Psr\RequestInterface $request
*
* @return RequestInterface
*/
public static function fromPsr7(Psr\RequestInterface $request) : RequestInterface
{
return self::fromUri($request->getUri());
}
/**
* @param Psr\UriInterface $uri
*
* @return RequestInterface
*/
public static function fromUri(Psr\UriInterface $uri) : RequestInterface
{
parse_str(urldecode($uri->getQuery()), $query);
return (new self($query))->create();
}
/**
* @return RequestInterface
*/
public function create() : RequestInterface
{
return new Request(
$this->getDraw(),
$this->getColumns(),
$this->getOrder(),
$this->getStart(),
$this->getLength(),
$this->getSearch()
);
}
/**
* @return int|null
*/
private function getDraw() : ?int
{
return (isset($this->request[self::DRAW])) ? (int)$this->request[self::DRAW] : null;
}
/**
* @return int|null
*/
private function getStart() : ?int
{
return (isset($this->request[self::START])) ? (int)$this->request[self::START] : null;
}
/**
* @return int|null
*/
private function getLength() : ?int
{
return (isset($this->request[self::LENGTH])) ? (int)$this->request[self::LENGTH] : null;
}
/**
* @return array
*/
private function getColumns() : array
{
if (!isset($this->request[self::COLUMNS])) {
return [];
}
return array_map(function (array $column) {
return new Column(
$column[self::COLUMNS_DATA] ?? null,
$column[self::COLUMNS_NAME] ?? null,
$column[self::COLUMNS_SEARCHABLE] ?? false,
$column[self::COLUMNS_ORDERABLE] ?? false,
$this->getSearchFromArray($column[self::SEARCH] ?? null)
);
}, $this->request[self::COLUMNS]);
}
/**
* @return array
*/
private function getOrder() : array
{
if (!isset($this->request[self::ORDER])) {
return [];
}
return array_map(function (array $column) {
return new Order(
$column[self::ORDER_COLUMN] ?? null,
$column[self::ORDER_DIR] ?? null
);
}, $this->request[self::ORDER]);
}
/**
* @return Search|null
*/
private function getSearch() : ?Search
{
return (isset($this->request[self::SEARCH])) ? $this->getSearchFromArray($this->request[self::SEARCH]) : null;
}
/**
* @param array|null $data
*
* @return Search|null
*/
private function getSearchFromArray(?array $data) : ?Search
{
if (
null === $data ||
!array_key_exists(self::SEARCH_VALUE, $data) ||
!array_key_exists(self::SEARCH_REGEX, $data)
) {
return null;
}
return new Search($data[self::SEARCH_VALUE], $data[self::SEARCH_REGEX]);
}
}