|
1
|
|
|
<?php |
|
2
|
|
|
|
|
|
|
|
|
|
3
|
|
|
namespace Jaxon\Plugin\Response\DataBag; |
|
4
|
|
|
|
|
5
|
|
|
use Jaxon\Di\Container; |
|
6
|
|
|
use Jaxon\Plugin\ResponsePlugin; |
|
7
|
|
|
|
|
8
|
|
|
use function is_array; |
|
9
|
|
|
use function is_string; |
|
10
|
|
|
use function json_decode; |
|
11
|
|
|
|
|
12
|
|
|
class DataBagPlugin extends ResponsePlugin |
|
|
|
|
|
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @const The plugin name |
|
16
|
|
|
*/ |
|
17
|
|
|
const NAME = 'bags'; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var Container |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $di; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var DataBag |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $xDataBag = null; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
|
|
|
|
|
30
|
|
|
* The constructor |
|
31
|
|
|
*/ |
|
32
|
|
|
public function __construct(Container $di) |
|
|
|
|
|
|
33
|
|
|
{ |
|
34
|
|
|
$this->di = $di; |
|
35
|
|
|
} |
|
|
|
|
|
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @return void |
|
39
|
|
|
*/ |
|
40
|
|
|
public function setDataBag() |
|
41
|
|
|
{ |
|
42
|
|
|
if($this->xDataBag !== null) |
|
43
|
|
|
{ |
|
44
|
|
|
return; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
$xRequest = $this->di->getRequest(); |
|
|
|
|
|
|
48
|
|
|
$aBody = $xRequest->getParsedBody(); |
|
|
|
|
|
|
49
|
|
|
$aParams = $xRequest->getQueryParams(); |
|
|
|
|
|
|
50
|
|
|
$aData = is_array($aBody) ? |
|
|
|
|
|
|
51
|
|
|
$this->readData($aBody['jxnbags'] ?? []) : |
|
|
|
|
|
|
52
|
|
|
$this->readData($aParams['jxnbags'] ?? []); |
|
53
|
|
|
$this->xDataBag = new DataBag($aData); |
|
54
|
|
|
} |
|
|
|
|
|
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @inheritDoc |
|
58
|
|
|
*/ |
|
|
|
|
|
|
59
|
|
|
public function getName(): string |
|
60
|
|
|
{ |
|
61
|
|
|
return self::NAME; |
|
62
|
|
|
} |
|
|
|
|
|
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @param mixed $xData |
|
|
|
|
|
|
66
|
|
|
* |
|
67
|
|
|
* @return array |
|
68
|
|
|
*/ |
|
69
|
|
|
private function readData($xData): array |
|
70
|
|
|
{ |
|
71
|
|
|
// Todo: clean input data. |
|
72
|
|
|
if(is_string($xData)) |
|
73
|
|
|
{ |
|
74
|
|
|
return json_decode($xData, true) ?: []; |
|
75
|
|
|
} |
|
76
|
|
|
return is_array($xData) ? $xData : []; |
|
77
|
|
|
} |
|
|
|
|
|
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @inheritDoc |
|
81
|
|
|
*/ |
|
|
|
|
|
|
82
|
|
|
public function getHash(): string |
|
83
|
|
|
{ |
|
84
|
|
|
// Use the version number as hash |
|
85
|
|
|
return '4.0.0'; |
|
86
|
|
|
} |
|
|
|
|
|
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @inheritDoc |
|
90
|
|
|
*/ |
|
|
|
|
|
|
91
|
|
|
public function getReadyScript(): string |
|
92
|
|
|
{ |
|
93
|
|
|
return ' |
|
94
|
|
|
jaxon.command.handler.register("bags.set", function(args) { |
|
95
|
|
|
for (const bag in args.data) { |
|
96
|
|
|
jaxon.ajax.parameters.bags[bag] = args.data[bag]; |
|
97
|
|
|
} |
|
98
|
|
|
}); |
|
99
|
|
|
'; |
|
100
|
|
|
} |
|
|
|
|
|
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @return void |
|
104
|
|
|
*/ |
|
105
|
|
|
public function writeCommand() |
|
106
|
|
|
{ |
|
107
|
|
|
$this->setDataBag(); |
|
108
|
|
|
if($this->xDataBag->touched()) |
|
109
|
|
|
{ |
|
110
|
|
|
$this->addCommand(['cmd' => 'bags.set'], $this->xDataBag->getAll()); |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
|
|
|
|
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* @param string $sName |
|
|
|
|
|
|
116
|
|
|
* |
|
117
|
|
|
* @return DataBagContext |
|
118
|
|
|
*/ |
|
119
|
|
|
public function bag(string $sName): DataBagContext |
|
120
|
|
|
{ |
|
121
|
|
|
$this->setDataBag(); |
|
122
|
|
|
return new DataBagContext($this->xDataBag, $sName); |
|
123
|
|
|
} |
|
|
|
|
|
|
124
|
|
|
} |
|
125
|
|
|
|