/
Query.php
127 lines (111 loc) · 3.06 KB
/
Query.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
<?php
namespace Concrete\Core\Entity\Search;
use Concrete\Core\Foundation\Serializer\JsonSerializer;
use Concrete\Core\Search\Field\FieldInterface;
use Concrete\Core\Search\ProviderInterface;
use Concrete\Core\Utility\Service\Xml;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Normalizer\DenormalizableInterface;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
/**
* @ORM\Embeddable
*/
class Query implements \JsonSerializable, DenormalizableInterface
{
const MAX_ITEMS_PER_PAGE = 10;
/**
* @ORM\Column(type="object")
*/
protected $fields = [];
/**
* @ORM\Column(type="object")
*/
protected $columns;
/**
* @ORM\Column(type="smallint")
*/
private $itemsPerPage;
/**
* @return FieldInterface[]
*/
public function getFields()
{
return $this->fields;
}
/**
* @param mixed $fields
*/
public function setFields($fields)
{
$this->fields = $fields;
}
/**
* Adds a field to the query object.
*
* @param FieldInterface $field
*/
public function addField(FieldInterface $field)
{
$this->fields[] = $field;
}
/**
* @return mixed
*/
public function getColumns()
{
return $this->columns;
}
/**
* @param mixed $columns
*/
public function setColumns($columns)
{
$this->columns = $columns;
}
public function jsonSerialize()
{
return [
'fields' => $this->fields,
'columnSet' => $this->columns,
'itemsPerPage' => $this->getItemsPerPage(),
];
}
/**
* @param int
*/
public function setItemsPerPage($itemsPerPage)
{
$this->itemsPerPage = is_numeric($itemsPerPage) ? $itemsPerPage : self::MAX_ITEMS_PER_PAGE;
}
/**
* @return int
*/
public function getItemsPerPage()
{
return $this->itemsPerPage;
}
public function denormalize(DenormalizerInterface $denormalizer, $data, $format = null, array $context = [])
{
$searchProvider = $context['searchProvider'];
/**
* @var $searchProvider ProviderInterface
*/
$fieldManager = $searchProvider->getFieldManager();
foreach($data['fields'] as $fieldRecord) {
$field = $fieldManager->getFieldByKey($fieldRecord['key']);
$field = $denormalizer->denormalize($fieldRecord, get_class($field), 'json', $context);
$this->fields[] = $field;
}
$columnSet = $searchProvider->getBaseColumnSet();
$all = $searchProvider->getAllColumnSet();
foreach($data['columnSet']['columns'] as $columnRecord) {
$column = $all->getColumnByKey($columnRecord['columnKey']);
$columnSet->addColumn($column);
if ($data['columnSet']['sortColumn'] == $columnRecord['columnKey']) {
$columnSet->setDefaultSortColumn($column);
}
}
$this->setColumns($columnSet);
$this->itemsPerPage = $data['itemsPerPage'];
}
}