Passed
Push — master ( eddd1e...93f15f )
by Thierry
09:22
created

Library::setResponse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
/**
4
 * Library.php - Base class for javascript library adapters.
5
 *
6
 * @package jaxon-dialogs
7
 * @author Thierry Feuzeu <[email protected]>
8
 * @copyright 2016 Thierry Feuzeu <[email protected]>
9
 * @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License
10
 * @link https://github.com/jaxon-php/jaxon-dialogs
11
 */
12
13
namespace Jaxon\Dialogs\Libraries;
14
15
use Jaxon\Dialogs\DialogPlugin;
16
use Jaxon\Dialogs\PluginInterface;
17
use Jaxon\Response\Response;
18
19
use function trim;
20
use function str_repeat;
21
use function is_string;
22
use function is_bool;
23
use function is_numeric;
24
use function json_encode;
25
26
class Library implements PluginInterface
27
{
28
    /**
29
     * The plugin instance
30
     *
31
     * @var DialogPlugin
32
     */
33
    protected $xDialogPlugin = null;
34
35
    /**
36
     * The name of the plugin
37
     *
38
     * @var string
39
     */
40
    protected $sName = '';
41
42
    /**
43
     * The subdir of the JS and CSS files in the CDN
44
     *
45
     * @var string
46
     */
47
    protected $sSubDir = '';
48
49
    /**
50
     * The default version of the plugin library
51
     *
52
     * @var string
53
     */
54
    protected $sVersion = '';
55
56
    /**
57
     * The default URI where to get the library files from
58
     *
59
     * @var string
60
     */
61
    protected $sUri = 'https://cdn.jaxon-php.org/libs';
62
63
    /**
64
     * The object used to build the response that will be sent to the client browser
65
     *
66
     * @var Response
67
     */
68
    protected $xResponse;
69
70
    /**
71
     * The constructor
72
     *
73
     * @param string $sSubDir The subdir of the JS and CSS files in the CDN
74
     * @param string $sVersion The default version of the plugin library
75
     */
76
    protected function __construct(string $sSubDir, string $sVersion)
77
    {
78
        $this->sSubDir = $sSubDir;
79
        $this->sVersion = $sVersion;
80
    }
81
82
    /**
83
     * Get the <Jaxon\Response\Response> object
84
     *
85
     * @return Response
86
     */
87
    final public function response(): Response
88
    {
89
        return $this->xResponse;
90
    }
91
92
    /**
93
     * Add a client side plugin command to the response object
94
     *
95
     * @param array $aAttributes The attributes of the command
96
     * @param mixed $xData The data to be added to the command
97
     *
98
     * @return void
99
     */
100
    final public function addCommand(array $aAttributes, $xData)
101
    {
102
        $this->xResponse->addPluginCommand($this->xDialogPlugin, $aAttributes, $xData);
103
    }
104
105
    /**
106
     * Initialize the library class instance
107
     *
108
     * @param string $sName The plugin name
109
     * @param DialogPlugin $xDialogPlugin The DialogPlugin plugin instance
110
     *
111
     * @return void
112
     */
113
    final public function init(string $sName, DialogPlugin $xDialogPlugin)
114
    {
115
        // Set the library name
116
        $this->sName = $sName;
117
        // Set the dialog plugin
118
        $this->xDialogPlugin = $xDialogPlugin;
119
        // Set the default URI.
120
        $this->sUri = $xDialogPlugin->getOption('dialogs.lib.uri', $this->sUri);
121
        // Set the library URI.
122
        $this->sUri = rtrim($this->getOption('uri', $this->sUri), '/');
123
        // Set the subdir
124
        $this->sSubDir = trim($this->getOption('subdir', $this->sSubDir), '/');
125
        // Set the version number
126
        $this->sVersion = trim($this->getOption('version', $this->sVersion), '/');
127
        // Set the Response instance
128
        $this->xResponse = $xDialogPlugin->response();
129
    }
130
131
    /**
132
     * Get the value of a config option
133
     *
134
     * @param string $sName The option name
135
     * @param mixed $xDefault The default value, to be returned if the option is not defined
136
     *
137
     * @return mixed
138
     */
139
    final public function getOption(string $sName, $xDefault = null)
140
    {
141
        $sName = 'dialogs.' . $this->getName() . '.' . $sName;
142
        return $this->xDialogPlugin->getOption($sName, $xDefault);
143
    }
144
145
    /**
146
     * Check the presence of a config option
147
     *
148
     * @param string $sName The option name
149
     *
150
     * @return bool
151
     */
152
    final public function hasOption(string $sName): bool
153
    {
154
        $sName = 'dialogs.' . $this->getName() . '.' . $sName;
155
        return $this->xDialogPlugin->hasOption($sName);
156
    }
157
158
    /**
159
     * Get the names of the options matching a given prefix
160
     *
161
     * @param string $sPrefix The prefix to match
162
     *
163
     * @return array
164
     */
165
    final public function getOptionNames(string $sPrefix): array
166
    {
167
        // The options names are relative to the plugin in Dialogs configuration
168
        return $this->xDialogPlugin->getOptionNames('dialogs.' . $this->getName() . '.' . $sPrefix);
169
    }
170
171
    /**
172
     * Get the names of the options matching a given prefix
173
     *
174
     * @param string $sVarPrefix
175
     * @param string $sKeyPrefix
176
     * @param int $nSpaces
177
     *
178
     * @return string
179
     */
180
    final public function getOptionScript(string $sVarPrefix, string $sKeyPrefix, int $nSpaces = 4): string
181
    {
182
        $aOptions = $this->getOptionNames($sKeyPrefix);
183
        $sSpaces = str_repeat(' ', $nSpaces);
184
        $sScript = '';
185
        foreach($aOptions as $sShortName => $sFullName)
186
        {
187
            $value = $this->xDialogPlugin->getOption($sFullName);
188
            if(is_string($value))
189
            {
190
                $value = "'$value'";
191
            }
192
            elseif(is_bool($value))
193
            {
194
                $value = ($value ? 'true' : 'false');
195
            }
196
            elseif(!is_numeric($value))
197
            {
198
                $value = json_encode($value);
199
            }
200
            $sScript .= "\n" . $sSpaces . $sVarPrefix . $sShortName . ' = ' . $value . ';';
201
        }
202
        return $sScript;
203
    }
204
205
    /**
206
     * @inheritDoc
207
     */
208
    public function getName(): string
209
    {
210
        return $this->sName;
211
    }
212
213
    /**
214
     * @inheritDoc
215
     */
216
    public function getJs(): string
217
    {
218
        return '';
219
    }
220
221
    /**
222
     * @inheritDoc
223
     */
224
    public function getCss(): string
225
    {
226
        return '';
227
    }
228
229
    /**
230
     * @inheritDoc
231
     */
232
    public function getScript(): string
233
    {
234
        return '';
235
    }
236
237
    /**
238
     * @inheritDoc
239
     */
240
    public function getReadyScript(): string
241
    {
242
        return '';
243
    }
244
245
    /**
246
     * Get the text of the "Yes" button for confirm dialog
247
     *
248
     * @return string
249
     */
250
    public function getQuestionTitle(): string
251
    {
252
        return $this->xDialogPlugin->getOption('dialogs.question.title', '');
253
    }
254
255
    /**
256
     * Get the text of the "Yes" button for confirm dialog
257
     *
258
     * @return string
259
     */
260
    public function getYesButtonText(): string
261
    {
262
        return $this->xDialogPlugin->getOption('dialogs.question.yes', 'Yes');
263
    }
264
265
    /**
266
     * Get the text of the "No" button for confirm dialog
267
     *
268
     * @return string
269
     */
270
    public function getNoButtonText(): string
271
    {
272
        return $this->xDialogPlugin->getOption('dialogs.question.no', 'No');
273
    }
274
275
    /**
276
     * Get the javascript HTML header code
277
     *
278
     * @param string $sFile The javascript file name
279
     *
280
     * @return string
281
     */
282
    public function getJsCode(string $sFile): string
283
    {
284
        return '<script type="text/javascript" src="' . $this->sUri . '/' .
285
            $this->sSubDir . '/' . $this->sVersion . '/' . $sFile . '"></script>';
286
    }
287
288
    /**
289
     * Get the CSS HTML header code
290
     *
291
     * @param string $sFile The CSS file name
292
     *
293
     * @return string
294
     */
295
    public function getCssCode(string $sFile): string
296
    {
297
        return '<link rel="stylesheet" href="' . $this->sUri . '/' .
298
            $this->sSubDir . '/' . $this->sVersion . '/' . $sFile . '" />';
299
    }
300
301
    /**
302
     * Render a template
303
     *
304
     * @param string $sTemplate The name of template to be rendered
305
     * @param array $aVars The template vars
306
     *
307
     * @return string
308
     */
309
    protected function render(string $sTemplate, array $aVars = []): string
310
    {
311
        // Is the library the default for alert messages?
312
        $isDefaultForMessage = ($this->getName() == $this->xDialogPlugin->getOption('dialogs.default.message'));
313
        // Is the library the default for confirm questions?
314
        $isDefaultForQuestion = ($this->getName() == $this->xDialogPlugin->getOption('dialogs.default.question'));
315
        $aLocalVars = [
316
            'yes' => $this->getYesButtonText(),
317
            'no' => $this->getNoButtonText(),
318
            'defaultForMessage' => $isDefaultForMessage,
319
            'defaultForQuestion' => $isDefaultForQuestion
320
        ];
321
        return $this->xDialogPlugin->render('jaxon::dialogs::' . $sTemplate, array_merge($aLocalVars, $aVars));
322
    }
323
}
324