Issues (2884)

src/Response/Response.php (1 issue)

1
<?php
2
3
/**
4
 * Response.php - The Jaxon Response
5
 *
6
 * This class collects commands to be sent back to the browser in response to a jaxon request.
7
 * Commands are encoded and packaged in json format.
8
 *
9
 * @package jaxon-core
10
 * @author Jared White
11
 * @author J. Max Wilson
12
 * @author Joseph Woolley
13
 * @author Steffen Konerow
14
 * @author Thierry Feuzeu <[email protected]>
15
 * @copyright Copyright (c) 2005-2007 by Jared White & J. Max Wilson
16
 * @copyright Copyright (c) 2008-2010 by Joseph Woolley, Steffen Konerow, Jared White  & J. Max Wilson
17
 * @copyright 2016 Thierry Feuzeu <[email protected]>
18
 * @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License
19
 * @link https://github.com/jaxon-php/jaxon-core
20
 */
21
22
namespace Jaxon\Response;
23
24
use Jaxon\Plugin\Manager\PluginManager;
25
use Jaxon\Plugin\Response\DataBag\DataBagContext;
26
use Jaxon\Plugin\Response\JQuery\DomSelector;
27
use Jaxon\Plugin\ResponsePlugin;
28
use Nyholm\Psr7\Factory\Psr17Factory;
29
use Nyholm\Psr7\Stream;
30
use Psr\Http\Message\ResponseInterface as PsrResponseInterface;
31
use Psr\Http\Message\ServerRequestInterface as PsrRequestInterface;
32
33
use function array_filter;
34
use function array_map;
35
use function gmdate;
36
use function is_array;
37
use function is_integer;
38
use function json_encode;
39
use function trim;
40
41
class Response implements ResponseInterface
42
{
43
    use Traits\CommandTrait;
44
    use Traits\DomTrait;
45
    use Traits\JsTrait;
46
47
    /**
48
     * @var PluginManager
49
     */
50
    protected $xPluginManager;
51
52
    /**
53
     * @var Psr17Factory
54
     */
55
    protected $xPsr17Factory;
56
57
    /**
58
     * @var PsrRequestInterface
59
     */
60
    protected $xRequest;
61
62
    /**
63
     * The constructor
64
     *
65
     * @param PluginManager $xPluginManager
66
     * @param Psr17Factory $xPsr17Factory
67
     * @param PsrRequestInterface $xRequest
68
     */
69
    public function __construct(PluginManager $xPluginManager, Psr17Factory $xPsr17Factory, PsrRequestInterface $xRequest)
70
    {
71
        $this->xPluginManager = $xPluginManager;
72
        $this->xPsr17Factory = $xPsr17Factory;
73
        $this->xRequest = $xRequest;
0 ignored issues
show
Equals sign not aligned with surrounding assignments; expected 7 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
74
    }
75
76
    /**
77
     * @inheritDoc
78
     */
79
    public function getContentType(): string
80
    {
81
        return 'application/json';
82
    }
83
84
    /**
85
     * @inheritDoc
86
     */
87
    public function getOutput(): string
88
    {
89
        if($this->getCommandCount() === 0)
90
        {
91
            return '{}';
92
        }
93
        return json_encode(['jxnobj' => $this->aCommands]);
94
    }
95
96
    /**
97
     * Provides access to registered response plugins
98
     *
99
     * Pass the plugin name as the first argument and the plugin object will be returned.
100
     *
101
     * @param string $sName    The name of the plugin
102
     *
103
     * @return null|ResponsePlugin
104
     */
105
    public function plugin(string $sName): ?ResponsePlugin
106
    {
107
        return $this->xPluginManager->getResponsePlugin($sName, $this);
108
    }
109
110
    /**
111
     * Magic PHP function
112
     *
113
     * Used to permit plugins to be called as if they were native members of the Response instance.
114
     *
115
     * @param string $sPluginName    The name of the plugin
116
     *
117
     * @return null|ResponsePlugin
118
     */
119
    public function __get(string $sPluginName)
120
    {
121
        return $this->plugin($sPluginName);
122
    }
123
124
    /**
125
     * Create a JQuery DomSelector, and link it to the current response.
126
     *
127
     * This is a shortcut to the JQuery plugin.
128
     *
129
     * @param string $sPath    The jQuery selector path
130
     * @param mixed $xContext    A context associated to the selector
131
     *
132
     * @return DomSelector
133
     */
134
    public function jq(string $sPath = '', $xContext = null): DomSelector
135
    {
136
        return $this->plugin('jquery')->selector($sPath, $xContext);
137
    }
138
139
    /**
140
     * Get the databag with a given name
141
     *
142
     * @param string $sName
143
     *
144
     * @return DataBagContext
145
     */
146
    public function bag(string $sName): DataBagContext
147
    {
148
        return $this->plugin('bags')->bag($sName);
149
    }
150
151
    /**
152
     * Add a response command to the array of commands that will be sent to the browser
153
     *
154
     * @param array $aAttributes    Associative array of attributes that will describe the command
155
     * @param mixed $mData    The data to be associated with this command
156
     *
157
     * @return ResponseInterface
158
     */
159
    public function addRawCommand(array $aAttributes, $mData): ResponseInterface
160
    {
161
        $aAttributes['data'] = $mData;
162
        $this->aCommands[] = $aAttributes;
163
        return $this;
164
    }
165
166
    /**
167
     * Add a response command to the array of commands that will be sent to the browser
168
     * Convert all attributes, excepted integers, to string.
169
     *
170
     * @param array $aAttributes    Associative array of attributes that will describe the command
171
     * @param mixed $mData    The data to be associated with this command
172
     *
173
     * @return ResponseInterface
174
     */
175
    public function addCommand(array $aAttributes, $mData): ResponseInterface
176
    {
177
        $aAttributes = array_map(function($xAttribute) {
178
            return is_integer($xAttribute) ? $xAttribute : trim((string)$xAttribute, " \t");
179
        }, $aAttributes);
180
        return $this->addRawCommand($aAttributes, $mData);
181
    }
182
183
    /**
184
     * Add a response command to the array of commands that will be sent to the browser
185
     *
186
     * @param string $sName    The command name
187
     * @param array $aAttributes    Associative array of attributes that will describe the command
188
     * @param mixed $mData    The data to be associated with this command
189
     * @param bool $bRemoveEmpty    If true, remove empty attributes
190
     *
191
     * @return ResponseInterface
192
     */
193
    protected function _addCommand(string $sName, array $aAttributes,
194
        $mData, bool $bRemoveEmpty = false): ResponseInterface
195
    {
196
        $mData = is_array($mData) ? array_map(function($sData) {
197
            return trim((string)$sData, " \t\n");
198
        }, $mData) : trim((string)$mData, " \t\n");
199
        if($bRemoveEmpty)
200
        {
201
            $aAttributes = array_filter($aAttributes, function($xValue) {
202
                return $xValue === '';
203
            });
204
        }
205
        $aAttributes['cmd'] = $sName;
206
        return $this->addCommand($aAttributes, $mData);
207
    }
208
209
    /**
210
     * Add a response command that is generated by a plugin
211
     *
212
     * @param ResponsePlugin $xPlugin    The plugin object
213
     * @param array $aAttributes    The attributes for this response command
214
     * @param mixed $mData    The data to be sent with this command
215
     *
216
     * @return ResponseInterface
217
     */
218
    public function addPluginCommand(ResponsePlugin $xPlugin, array $aAttributes, $mData): ResponseInterface
219
    {
220
        $aAttributes['plg'] = $xPlugin->getName();
221
        return $this->addCommand($aAttributes, $mData);
222
    }
223
224
    /**
225
     * Convert this response to a PSR7 response object
226
     *
227
     * @return PsrResponseInterface
228
     */
229
    public function toPsr(): PsrResponseInterface
230
    {
231
        $xPsrResponse = $this->xPsr17Factory->createResponse(200);
232
        if($this->xRequest->getMethod() === 'GET')
233
        {
234
            $xPsrResponse = $xPsrResponse
235
                ->withHeader('Expires', 'Mon, 26 Jul 1997 05:00:00 GMT')
236
                ->withHeader('Last-Modified', gmdate("D, d M Y H:i:s") . ' GMT')
237
                ->withHeader('Cache-Control', 'no-cache, must-revalidate')
238
                ->withHeader('Pragma', 'no-cache');
239
        }
240
        return $xPsrResponse
241
            ->withHeader('content-type', $this->getContentType())
242
            ->withBody(Stream::create($this->getOutput()));
243
    }
244
}
245