Passed
Push — v5.x ( fe38d4...6ac3c7 )
by Thierry
10:27
created

AjaxTrait::getCharacterEncoding()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
/**
4
 * Ajax.php
5
 *
6
 * The Jaxon library.
7
 *
8
 * @package jaxon-core
0 ignored issues
show
Coding Style introduced by
Package name "jaxon-core" is not valid; consider "Jaxoncore" instead
Loading history...
9
 * @author Thierry Feuzeu <[email protected]>
10
 * @copyright 2022 Thierry Feuzeu <[email protected]>
11
 * @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License
12
 * @link https://github.com/jaxon-php/jaxon-core
13
 */
0 ignored issues
show
Coding Style introduced by
PHP version not specified
Loading history...
Coding Style introduced by
Missing @category tag in file comment
Loading history...
14
15
namespace Jaxon\App\Traits;
16
17
use Jaxon\App\Config\ConfigManager;
18
use Jaxon\App\I18n\Translator;
19
use Jaxon\Di\Container;
20
use Jaxon\Exception\SetupException;
21
use Jaxon\Plugin\Manager\PluginManager;
22
use Jaxon\Plugin\Package;
23
use Jaxon\Plugin\Request\CallableClass\CallableRegistry;
24
use Jaxon\Plugin\ResponsePlugin;
25
use Jaxon\Request\Factory\Factory;
26
use Jaxon\Request\Handler\CallbackManager;
27
use Jaxon\Response\Manager\ResponseManager;
28
use Jaxon\Utils\Http\UriException;
29
use Psr\Log\LoggerInterface;
30
31
use function trim;
32
33
trait AjaxTrait
0 ignored issues
show
Coding Style introduced by
Missing doc comment for trait AjaxTrait
Loading history...
34
{
35
    /**
36
     * @var Container
37
     */
38
    protected $xContainer = null;
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line(s) before first member var; 0 found
Loading history...
39
40
    /**
41
     * @var ConfigManager
42
     */
43
    protected $xConfigManager;
44
45
    /**
46
     * @var ResponseManager
47
     */
48
    protected $xResponseManager;
49
50
    /**
51
     * @var PluginManager
52
     */
53
    protected $xPluginManager;
54
55
    /**
56
     * @var CallableRegistry
57
     */
58
    protected $xCallableRegistry;
59
60
    /**
61
     * @var Translator
62
     */
63
    protected $xTranslator;
64
65
    /**
66
     * @return Container
67
     */
68
    public function di(): ?Container
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 1 found
Loading history...
69
    {
70
        return $this->xContainer;
71
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
72
73
    /**
74
     * @return LoggerInterface
75
     */
76
    public function logger(): LoggerInterface
77
    {
78
        return $this->di()->getLogger();
79
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
80
81
    /**
82
     * @return Translator
83
     */
84
    public function translator(): Translator
85
    {
86
        return $this->xTranslator;
87
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
88
89
    /**
90
     * Set the value of a config option
91
     *
92
     * @param string $sName    The option name
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 4 found
Loading history...
93
     * @param mixed $sValue    The option value
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
94
     *
95
     * @return void
96
     */
97
    public function setOption(string $sName, $sValue)
98
    {
99
        $this->xConfigManager->setOption($sName, $sValue);
100
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
101
102
    /**
103
     * Get the value of a config option
104
     *
105
     * @param string $sName    The option name
106
     * @param mixed $xDefault    The default value, to be returned if the option is not defined
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
107
     *
108
     * @return mixed
109
     */
110
    public function getOption(string $sName, $xDefault = null)
111
    {
112
        return $this->xConfigManager->getOption($sName, $xDefault);
113
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
114
115
    /**
116
     * Check the presence of a config option
117
     *
118
     * @param string $sName    The option name
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
119
     *
120
     * @return bool
121
     */
122
    public function hasOption(string $sName): bool
123
    {
124
        return $this->xConfigManager->hasOption($sName);
125
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
126
127
    /**
128
     * Get the configured character encoding
129
     *
130
     * @return string
131
     */
132
    public function getCharacterEncoding(): string
133
    {
134
        return trim($this->getOption('core.encoding', ''));
135
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
136
137
    /**
138
     * Get the content type of the HTTP response
139
     *
140
     * @return string
141
     */
142
    public function getContentType(): string
143
    {
144
        return $this->xResponseManager->getContentType();
145
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
146
147
    /**
148
     * Get the factory for request and parameter factories
149
     *
150
     * @return Factory
151
     */
152
    public function factory(): Factory
153
    {
154
        return $this->di()->getFactory();
155
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
156
157
    /**
158
     * Get an instance of a registered class
159
     *
160
     * @param string $sClassName The class name
161
     *
162
     * @return mixed
163
     * @throws SetupException
164
     */
165
    public function cl(string $sClassName)
166
    {
167
        $sClassName = trim($sClassName);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 5 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...
168
        $xCallableClass = $this->xCallableRegistry->getCallableObject($sClassName);
169
        return !$xCallableClass ? null : $xCallableClass->getRegisteredObject();
0 ignored issues
show
introduced by
$xCallableClass is of type Jaxon\Plugin\Request\CallableClass\CallableObject, thus it always evaluated to true.
Loading history...
170
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
171
172
    /**
173
     * Get the HTML tags to include Jaxon javascript files into the page.
174
     *
175
     * @return string
176
     */
177
    public function getJs(): string
178
    {
179
        return $this->di()->getCodeGenerator()->getJs();
180
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
181
182
    /**
183
     * Get the HTML tags to include Jaxon javascript files into the page.
184
     *
185
     * @return string  the javascript code
186
     */
187
    public function js(): string
188
    {
189
        return $this->di()->getCodeGenerator()->getJs();
190
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
191
192
    /**
193
     * Get the HTML tags to include Jaxon CSS code and files into the page.
194
     *
195
     * @return string
196
     */
197
    public function getCss(): string
198
    {
199
        return $this->di()->getCodeGenerator()->getCss();
200
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
201
202
    /**
203
     * Get the HTML tags to include Jaxon CSS code and files into the page.
204
     *
205
     * @return string
206
     */
207
    public function css(): string
208
    {
209
        return $this->di()->getCodeGenerator()->getCss();
210
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
211
212
    /**
213
     * Returns the js header and wrapper code to be printed into the page
214
     *
215
     * The javascript code returned by this function depends on the plugins
216
     * that are included and the functions and classes that are registered.
217
     *
218
     * @param bool $bIncludeJs    Also get the js code
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 4 found
Loading history...
219
     * @param bool $bIncludeCss    Also get the css code
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
220
     *
221
     * @return string
222
     * @throws UriException
223
     */
224
    public function getScript(bool $bIncludeJs = false, bool $bIncludeCss = false): string
225
    {
226
        return $this->di()->getCodeGenerator()->getScript($bIncludeJs, $bIncludeCss);
227
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
228
229
    /**
230
     * Returns the js header and wrapper code to be printed into the page
231
     *
232
     * @param bool $bIncludeJs    Also get the js code
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 4 found
Loading history...
233
     * @param bool $bIncludeCss    Also get the css code
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
234
     *
235
     * @return string  the javascript code
236
     * @throws UriException
237
     */
238
    public function script(bool $bIncludeJs = false, bool $bIncludeCss = false): string
239
    {
240
        return $this->di()->getCodeGenerator()->getScript($bIncludeJs, $bIncludeCss);
241
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
242
243
    /**
244
     * Determine if a call is a jaxon request or a page load request
245
     *
246
     * @return bool
247
     */
248
    public function canProcessRequest(): bool
249
    {
250
        return $this->di()->getRequestHandler()->canProcessRequest();
251
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
252
253
    /**
254
     * Get a registered response plugin
255
     *
256
     * @param string $sName    The name of the plugin
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
257
     *
258
     * @return ResponsePlugin|null
259
     */
260
    public function plugin(string $sName): ?ResponsePlugin
261
    {
262
        return $this->xPluginManager->getResponsePlugin($sName);
263
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
264
265
    /**
266
     * Get a package instance
267
     *
268
     * @param string $sClassName    The package class name
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
269
     *
270
     * @return Package|null
271
     */
272
    public function package(string $sClassName): ?Package
273
    {
274
        return $this->di()->getPackageManager()->getPackage($sClassName);
275
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
276
277
    /**
278
     * Get the callback manager
279
     *
280
     * @return CallbackManager
281
     */
282
    public function callback(): CallbackManager
283
    {
284
        return $this->di()->getCallbackManager();
285
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 0 found
Loading history...
286
}
287