Issues (2884)

src/App/Dialog/Library/DialogLibraryHelper.php (30 issues)

1
<?php
2
0 ignored issues
show
Missing file doc comment
Loading history...
3
namespace Jaxon\App\Dialog\Library;
4
5
use Jaxon\App\Config\ConfigManager;
6
use Jaxon\App\Dialog\LibraryInterface;
7
use Jaxon\Utils\Template\TemplateEngine;
8
9
use function array_merge;
10
use function is_bool;
11
use function is_numeric;
12
use function is_string;
13
use function json_encode;
14
use function rtrim;
15
use function str_repeat;
16
use function trim;
17
18
class DialogLibraryHelper
0 ignored issues
show
Missing doc comment for class DialogLibraryHelper
Loading history...
19
{
20
    /**
21
     * @var ConfigManager
22
     */
23
    protected $xConfigManager;
0 ignored issues
show
Expected 1 blank line(s) before first member var; 0 found
Loading history...
24
25
    /**
26
     * The Jaxon template engine
27
     *
28
     * @var TemplateEngine
29
     */
30
    protected $xTemplateEngine;
31
32
    /**
33
     * The name of the library
34
     *
35
     * @var string
36
     */
37
    protected $sName = '';
38
39
    /**
40
     * The URI where to get the library files from
41
     *
42
     * @var string
43
     */
44
    protected $sUri = '';
45
46
    /**
47
     * The subdir of the JS and CSS files in the CDN
48
     *
49
     * @var string
50
     */
51
    protected $sSubDir = '';
52
53
    /**
54
     * The default version of the plugin library
55
     *
56
     * @var string
57
     */
58
    protected $sVersion = '';
59
60
    /**
61
     * The constructor
62
     *
63
     * @param LibraryInterface $xDialogLibrary
0 ignored issues
show
Missing parameter comment
Loading history...
64
     * @param ConfigManager $xConfigManager
0 ignored issues
show
Missing parameter comment
Loading history...
Expected 4 spaces after parameter type; 1 found
Loading history...
65
     * @param TemplateEngine $xTemplateEngine
0 ignored issues
show
Expected 3 spaces after parameter type; 1 found
Loading history...
Missing parameter comment
Loading history...
66
     */
67
    public function __construct(LibraryInterface $xDialogLibrary,
0 ignored issues
show
Expected 2 blank lines before function; 1 found
Loading history...
68
        ConfigManager $xConfigManager, TemplateEngine $xTemplateEngine)
69
    {
70
        $this->xConfigManager = $xConfigManager;
71
        $this->xTemplateEngine = $xTemplateEngine;
72
73
        // Set the library name
74
        $this->sName = $xDialogLibrary->getName();
75
        // Set the default URI.
76
        $sDefaultUri = $xConfigManager->getOption('dialogs.lib.uri', $xDialogLibrary->getUri());
77
        // Set the library URI.
78
        $this->sUri = rtrim($this->getOption('uri', $sDefaultUri), '/');
79
        // Set the subdir
80
        $this->sSubDir = trim($this->getOption('subdir', $xDialogLibrary->getSubDir()), '/');
81
        // Set the version number
82
        $this->sVersion = trim($this->getOption('version', $xDialogLibrary->getVersion()), '/');
83
    }
0 ignored issues
show
Expected 2 blank lines after function; 1 found
Loading history...
84
85
    /**
86
     * Get the value of a config option
87
     *
88
     * @param string $sOptionName The option name
89
     * @param mixed $xDefault The default value, to be returned if the option is not defined
0 ignored issues
show
Expected 2 spaces after parameter type; 1 found
Loading history...
Expected 4 spaces after parameter name; 1 found
Loading history...
90
     *
91
     * @return mixed
92
     */
93
    public function getOption(string $sOptionName, $xDefault = null)
94
    {
95
        $sOptionName = 'dialogs.' . $this->sName . '.' . $sOptionName;
96
        return $this->xConfigManager->getOption($sOptionName, $xDefault);
97
    }
0 ignored issues
show
Expected 2 blank lines after function; 1 found
Loading history...
98
99
    /**
100
     * Check the presence of a config option
101
     *
102
     * @param string $sOptionName The option name
103
     *
104
     * @return bool
105
     */
106
    public function hasOption(string $sOptionName): bool
107
    {
108
        $sOptionName = 'dialogs.' . $this->sName . '.' . $sOptionName;
109
        return $this->xConfigManager->hasOption($sOptionName);
110
    }
0 ignored issues
show
Expected 2 blank lines after function; 1 found
Loading history...
111
112
    /**
113
     * Get the names of the options matching a given prefix
114
     *
115
     * @param string $sPrefix The prefix to match
116
     *
117
     * @return array
118
     */
119
    public function getOptionNames(string $sPrefix): array
120
    {
121
        // The options names are relative to the plugin in Dialogs configuration
122
        return $this->xConfigManager->getOptionNames('dialogs.' . $this->sName . '.' . $sPrefix);
123
    }
0 ignored issues
show
Expected 2 blank lines after function; 1 found
Loading history...
124
125
    /**
126
     * Get the names of the options matching a given prefix
127
     *
128
     * @param string $sVarPrefix
0 ignored issues
show
Missing parameter comment
Loading history...
129
     * @param string $sKeyPrefix
0 ignored issues
show
Missing parameter comment
Loading history...
130
     * @param int $nSpaces
0 ignored issues
show
Missing parameter comment
Loading history...
Expected 4 spaces after parameter type; 1 found
Loading history...
131
     *
132
     * @return string
133
     */
134
    public function getOptionScript(string $sVarPrefix, string $sKeyPrefix, int $nSpaces = 4): string
135
    {
136
        $aOptions = $this->getOptionNames($sKeyPrefix);
137
        $sSpaces = str_repeat(' ', $nSpaces);
138
        $sScript = '';
139
        foreach($aOptions as $sShortName => $sFullName)
140
        {
141
            $value = $this->xConfigManager->getOption($sFullName);
142
            if(is_string($value))
143
            {
144
                $value = "'$value'";
145
            }
146
            elseif(is_bool($value))
147
            {
148
                $value = ($value ? 'true' : 'false');
149
            }
150
            elseif(!is_numeric($value))
151
            {
152
                $value = json_encode($value);
153
            }
154
            $sScript .= "\n" . $sSpaces . $sVarPrefix . $sShortName . ' = ' . $value . ';';
155
        }
156
        return $sScript;
157
    }
0 ignored issues
show
Expected 2 blank lines after function; 1 found
Loading history...
158
159
    /**
160
     * Get the text of the "Yes" button for confirm dialog
161
     *
162
     * @return string
163
     */
164
    public function getQuestionTitle(): string
165
    {
166
        return $this->xConfigManager->getOption('dialogs.question.title', '');
167
    }
0 ignored issues
show
Expected 2 blank lines after function; 1 found
Loading history...
168
169
    /**
170
     * Get the javascript HTML header code
171
     *
172
     * @param string $sFile The javascript file name
173
     *
174
     * @return string
175
     */
176
    public function getJsCode(string $sFile): string
177
    {
178
        if($this->hasOption('assets.js'))
179
        {
180
            // If this expression evaluates to false, then the asset is not displayed.
181
            if(!($sUri = $this->getOption('assets.js')))
0 ignored issues
show
Variable assignment found within a condition. Did you mean to do a comparison ?
Loading history...
182
            {
183
                return '';
184
            }
185
        }
186
        else
187
        {
188
            $sUri = $this->sUri . '/' . ($this->sSubDir ? $this->sSubDir . '/' : '') .
189
                ($this->sVersion ? $this->sVersion . '/' : '') . $sFile;
190
        }
191
        return '<script type="text/javascript" src="' . $sUri . '"></script>';
192
    }
0 ignored issues
show
Expected 2 blank lines after function; 1 found
Loading history...
193
194
    /**
195
     * Get the CSS HTML header code
196
     *
197
     * @param string $sFile The CSS file name
198
     *
199
     * @return string
200
     */
201
    public function getCssCode(string $sFile): string
202
    {
203
        if($this->hasOption('assets.css'))
204
        {
205
            // If this expression evaluates to false, then the asset is not displayed.
206
            if(!($sUri = $this->getOption('assets.css')))
0 ignored issues
show
Variable assignment found within a condition. Did you mean to do a comparison ?
Loading history...
207
            {
208
                return '';
209
            }
210
        }
211
        else
212
        {
213
            $sUri = $this->sUri . '/' . ($this->sSubDir ? $this->sSubDir . '/' : '') .
214
                ($this->sVersion ? $this->sVersion . '/' : '') . $sFile;
215
        }
216
        return '<link rel="stylesheet" href="' . $sUri . '" />';
217
    }
0 ignored issues
show
Expected 2 blank lines after function; 1 found
Loading history...
218
219
    /**
220
     * Render a template
221
     *
222
     * @param string $sTemplate The name of template to be rendered
223
     * @param array $aVars The template vars
0 ignored issues
show
Expected 2 spaces after parameter type; 1 found
Loading history...
Expected 5 spaces after parameter name; 1 found
Loading history...
224
     *
225
     * @return string
226
     */
227
    public function render(string $sTemplate, array $aVars = []): string
228
    {
229
        // Is the library the default for alert messages?
230
        $isDefaultForMessage = ($this->sName == $this->xConfigManager->getOption('dialogs.default.message'));
231
        // Is the library the default for confirm questions?
232
        $isDefaultForQuestion = ($this->sName == $this->xConfigManager->getOption('dialogs.default.question'));
233
        $aLocalVars = [
0 ignored issues
show
Equals sign not aligned with surrounding assignments; expected 11 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...
234
            'yes' => $this->xConfigManager->getOption('dialogs.question.yes', 'Yes'),
235
            'no' =>  $this->xConfigManager->getOption('dialogs.question.no', 'No'),
0 ignored issues
show
Expected 1 space after "=>"; 2 found
Loading history...
236
            'defaultForMessage' => $isDefaultForMessage,
237
            'defaultForQuestion' => $isDefaultForQuestion
238
        ];
239
        return $this->xTemplateEngine->render('jaxon::dialogs::' . $sTemplate, array_merge($aLocalVars, $aVars));
240
    }
0 ignored issues
show
Expected 2 blank lines after function; 0 found
Loading history...
241
}
242