/
AbstractField.php
114 lines (101 loc) · 2.77 KB
/
AbstractField.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
<?php
namespace Concrete\Core\Search\Field;
use Concrete\Core\Http\ResponseAssetGroup;
use Concrete\Core\Utility\Service\Xml;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
abstract class AbstractField implements FieldInterface
{
/**
* The current search data.
*
* @var array
*/
protected $data = [];
/**
* The list of all the request variables.
*
* @var array
*/
protected $requestVariables = [];
/**
* Determines whether the data is loaded into the field. If this is true, loadFromRequest will not
* repopulate from request.
*
* @var bool
*/
protected $isLoaded = false;
/**
* {@inheritdoc}
*
* @see FieldInterface::renderSearchField()
*/
public function renderSearchField()
{
return '';
}
/**
* {@inheritdoc}
*
* @see \JsonSerializable::jsonSerialize()
*/
public function jsonSerialize()
{
ob_start();
echo $this->renderSearchField();
$field = ob_get_contents();
ob_end_clean();
$ag = ResponseAssetGroup::get();
$assetsResponse = [];
foreach ($ag->getAssetsToOutput() as $position => $assets) {
foreach ($assets as $asset) {
if (is_object($asset)) {
$assetsResponse[$asset->getAssetType()][] = $asset->getAssetURL();
}
}
}
return [
'key' => $this->getKey(),
'label' => $this->getDisplayName(),
'element' => $field,
'data' => $this->data,
'assets' => $assetsResponse,
];
}
public function export(\SimpleXMLElement $element)
{
$xml = new Xml();
$fieldNode = $element->addChild('field');
$fieldNode->addAttribute('key', $this->getKey());
$xml->createCDataNode($fieldNode, 'data', json_encode($this->data));
}
public function denormalize(DenormalizerInterface $denormalizer, $data, $format = null, array $context = [])
{
$this->data = $data['data'];
}
public function setData($key, $value)
{
$this->data[$key] = $value;
}
/**
* {@inheritdoc}
*
* @see FieldInterface::loadDataFromRequest()
*/
public function loadDataFromRequest(array $request)
{
if (!$this->isLoaded) {
foreach ($request as $key => $value) {
if (in_array($key, $this->requestVariables)) {
$this->data[$key] = $value;
}
}
$this->isLoaded = true;
}
}
public function loadDataFromImport(\SimpleXMLElement $element)
{
if (!$this->isLoaded) {
$this->data = json_decode($element->data);
}
}
}