1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Netdudes\DataSourceryBundle\Query; |
4
|
|
|
|
5
|
|
|
class Query implements QueryInterface |
6
|
|
|
{ |
7
|
|
|
protected $filter; |
8
|
|
|
|
9
|
|
|
protected $sort; |
10
|
|
|
|
11
|
|
|
protected $pagination; |
12
|
|
|
|
13
|
|
|
protected $select = []; |
14
|
|
|
|
15
|
|
|
public function __construct() |
16
|
|
|
{ |
17
|
|
|
$this->sort = new Sort(); |
18
|
|
|
$this->filter = new Filter(); |
19
|
|
|
$this->filter->setConditionType(Filter::CONDITION_TYPE_AND); |
20
|
|
|
$this->pagination = new Pagination(); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @return Filter |
25
|
|
|
*/ |
26
|
|
|
public function getFilter() |
27
|
|
|
{ |
28
|
|
|
return $this->filter; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function setFilter(Filter $filter) |
32
|
|
|
{ |
33
|
|
|
$this->filter = $filter; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @return Pagination |
38
|
|
|
*/ |
39
|
|
|
public function getPagination() |
40
|
|
|
{ |
41
|
|
|
return $this->pagination; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function setPagination(Pagination $pagination) |
45
|
|
|
{ |
46
|
|
|
$this->pagination = $pagination; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @return array |
51
|
|
|
*/ |
52
|
|
|
public function getSelect() |
53
|
|
|
{ |
54
|
|
|
return $this->select; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param array $elements |
59
|
|
|
*/ |
60
|
|
|
public function setSelect(array $elements) |
61
|
|
|
{ |
62
|
|
|
$this->select = $elements; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @return Sort |
67
|
|
|
*/ |
68
|
|
|
public function getSort() |
69
|
|
|
{ |
70
|
|
|
return $this->sort; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param Sort $sort |
75
|
|
|
*/ |
76
|
|
|
public function setSort(Sort $sort) |
77
|
|
|
{ |
78
|
|
|
$this->sort = $sort; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param SortCondition $sortCondition |
83
|
|
|
*/ |
84
|
|
|
public function addSortCondition(SortCondition $sortCondition) |
85
|
|
|
{ |
86
|
|
|
if (is_null($this->sort)) { |
87
|
|
|
$this->sort = new Sort(); |
88
|
|
|
} |
89
|
|
|
$this->sort[] = $sortCondition; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Figure out a relation of all required fields to be selected, including the fields |
94
|
|
|
* needed for the selected columns, the fields required from the set filters, and |
95
|
|
|
* any field set to be sorted by. |
96
|
|
|
* |
97
|
|
|
* @return array |
98
|
|
|
*/ |
99
|
|
|
public function extractRequiredFields() |
100
|
|
|
{ |
101
|
|
|
$requiredFields = []; |
102
|
|
|
|
103
|
|
|
// Extract required fields from the select |
104
|
|
|
if (!is_null($this->select)) { |
105
|
|
|
foreach ($this->getSelect() as $selectedField) { |
106
|
|
|
$requiredFields[] = $selectedField; |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
// Flatten and extract all filtered fields |
111
|
|
|
$flatFilterConditions = $this->getFilter()->getAllFilterConditionsFlat(); |
112
|
|
|
foreach ($flatFilterConditions as $filterCondition) { |
113
|
|
|
$requiredFields[] = $filterCondition->getFieldName(); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
// Extract all sort fields |
117
|
|
|
/** @var $sortCondition SortCondition */ |
118
|
|
|
foreach ($this->getSort() as $sortCondition) { |
119
|
|
|
$requiredFields[] = $sortCondition->getFieldName(); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
return array_unique($requiredFields); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|