1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace OpenSkill\Datatable\Versions; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use OpenSkill\Datatable\Columns\ColumnConfiguration; |
7
|
|
|
use OpenSkill\Datatable\Data\ResponseData; |
8
|
|
|
use OpenSkill\Datatable\Queries\Parser\Datatable19QueryParser; |
9
|
|
|
use OpenSkill\Datatable\Queries\QueryConfiguration; |
10
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
11
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class Datatable19Version |
15
|
|
|
* @package OpenSkill\Datatable\Versions |
16
|
|
|
* |
17
|
|
|
* Version that supports the 1.9 version of datatables |
18
|
|
|
* http://legacy.datatables.net/index |
19
|
|
|
* |
20
|
|
|
*/ |
21
|
|
|
class Datatable19Version extends Version |
22
|
|
|
{ |
23
|
|
|
/** @var Datatable19QueryParser */ |
24
|
|
|
private $queryParser; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Datatable19Version constructor. |
28
|
|
|
* |
29
|
|
|
* @param RequestStack $requestStack The current request |
30
|
|
|
*/ |
31
|
|
|
public function __construct(RequestStack $requestStack) |
32
|
|
|
{ |
33
|
|
|
parent::__construct($requestStack); |
34
|
|
|
$this->queryParser = new Datatable19QueryParser(); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Method to determine if this parser can handle the query parameters. If so then the parser should return true |
39
|
|
|
* and be able to return a DTQueryConfiguration |
40
|
|
|
* |
41
|
|
|
* @return bool true if the parser is able to parse the query parameters and to return a DTQueryConfiguration |
42
|
|
|
*/ |
43
|
|
View Code Duplication |
public function canParseRequest() |
|
|
|
|
44
|
|
|
{ |
45
|
|
|
$request = $this->requestStack->getCurrentRequest(); |
46
|
|
|
if (is_null($request)) { |
47
|
|
|
throw new \InvalidArgumentException("Can not determine a request that is null"); |
48
|
|
|
} |
49
|
|
|
return $this->queryParser->canParse($request); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Method that should parse the request and return a DTQueryConfiguration |
54
|
|
|
* |
55
|
|
|
* @param ColumnConfiguration[] $columnConfiguration The configuration of the columns |
56
|
|
|
* |
57
|
|
|
* @return QueryConfiguration the configuration the provider can use to prepare the data |
58
|
|
|
*/ |
59
|
|
View Code Duplication |
public function parseRequest(array $columnConfiguration) |
|
|
|
|
60
|
|
|
{ |
61
|
|
|
$request = $this->requestStack->getCurrentRequest(); |
62
|
|
|
if (is_null($request)) { |
63
|
|
|
throw new \InvalidArgumentException("Can not parse a request that is null"); |
64
|
|
|
} |
65
|
|
|
return $this->queryParser->parse($request, $columnConfiguration); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Is responsible to take the generated data and prepare a response for it. |
70
|
|
|
* @param ResponseData $data The processed data. |
71
|
|
|
* @param QueryConfiguration $queryConfiguration the query configuration for the current request. |
72
|
|
|
* @param ColumnConfiguration[] $columnConfigurations the column configurations for the current data table. |
73
|
|
|
* @return JsonResponse the response that should be returned to the client. |
74
|
|
|
*/ |
75
|
|
|
public function createResponse( |
76
|
|
|
ResponseData $data, |
77
|
|
|
QueryConfiguration $queryConfiguration, |
78
|
|
|
array $columnConfigurations |
79
|
|
|
) { |
80
|
|
|
$responseData = [ |
81
|
|
|
'sEcho' => $queryConfiguration->drawCall(), |
82
|
|
|
'iTotalRecords' => $data->totalDataCount(), |
83
|
|
|
'iTotalDisplayRecords' => $data->data()->count(), |
84
|
|
|
'aaData' => $data->data()->toArray() |
85
|
|
|
]; |
86
|
|
|
return new JsonResponse($responseData); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @return string The name of the view that this version should use fot the table. |
91
|
|
|
*/ |
92
|
|
|
public function tableView() |
93
|
|
|
{ |
94
|
|
|
return "viewTableStuff"; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @return string The name of the view that this version should use for the script. |
99
|
|
|
*/ |
100
|
|
|
public function scriptView() |
101
|
|
|
{ |
102
|
|
|
return "scriptViewStuff"; |
103
|
|
|
} |
104
|
|
|
} |
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.