Passed
Push — v5.x ( 00e299...57c2ac )
by Thierry
11:16
created

Call::elseShow()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
/**
4
 * Call.php - The Jaxon Call
5
 *
6
 * This class is used to create js ajax requests to callable classes and functions.
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 Jared White
0 ignored issues
show
Coding Style introduced by
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
10
 * @author J. Max Wilson
0 ignored issues
show
Coding Style introduced by
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
11
 * @author Joseph Woolley
0 ignored issues
show
Coding Style introduced by
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
12
 * @author Steffen Konerow
0 ignored issues
show
Coding Style introduced by
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
13
 * @author Thierry Feuzeu <[email protected]>
14
 * @copyright Copyright (c) 2005-2007 by Jared White & J. Max Wilson
0 ignored issues
show
Coding Style introduced by
@copyright tag must contain a year and the name of the copyright holder
Loading history...
15
 * @copyright Copyright (c) 2008-2010 by Joseph Woolley, Steffen Konerow, Jared White  & J. Max Wilson
0 ignored issues
show
Coding Style introduced by
@copyright tag must contain a year and the name of the copyright holder
Loading history...
16
 * @copyright 2016 Thierry Feuzeu <[email protected]>
17
 * @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License
18
 * @link https://github.com/jaxon-php/jaxon-core
19
 */
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...
20
21
namespace Jaxon\Request\Call;
22
23
use Jaxon\App\Dialog\Library\DialogLibraryManager;
24
25
use function array_map;
26
use function array_shift;
27
use function func_get_args;
28
29
class Call extends JsCall
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class Call
Loading history...
30
{
31
    /**
32
     * @var DialogLibraryManager
33
     */
34
    protected $xDialogLibraryManager;
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line(s) before first member var; 0 found
Loading history...
35
36
    /**
37
     * The arguments of the else() calls
38
     *
39
     * @var array
40
     */
41
    protected $aMessage = [];
42
43
    /**
44
     * A condition to check before making the call
45
     *
46
     * @var array
47
     */
48
    protected $aCondition = [];
49
50
    /**
51
     * The arguments of the confirm() call
52
     *
53
     * @var array
54
     */
55
    protected $aConfirm = [];
56
57
    /**
58
     * The constructor.
59
     *
60
     * @param string $sName    The javascript function or method name
0 ignored issues
show
Coding Style introduced by
Expected 15 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Expected 17 spaces after parameter name; 4 found
Loading history...
61
     * @param DialogLibraryManager $xDialogLibraryManager
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
62
     */
63
    public function __construct(string $sName, DialogLibraryManager $xDialogLibraryManager)
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 1 found
Loading history...
64
    {
65
        parent::__construct($sName);
66
        $this->xDialogLibraryManager = $xDialogLibraryManager;
67
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
68
69
    /**
70
     * Make a phrase to be displayed in js code
71
     *
72
     * @param string $sPhrase
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
73
     * @param array $aArgs
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Missing parameter comment
Loading history...
74
     *
75
     * @return array
76
     */
77
    private function makePhrase(string $sPhrase, array $aArgs = []): array
78
    {
79
        array_shift($aArgs);
80
        return [
81
            'str' => $sPhrase,
82
            'args' => array_map(fn($xArg) => Parameter::make($xArg), $aArgs),
83
        ];
84
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
85
86
    /**
87
     * Set the message if the condition to the call is not met
88
     *
89
     * The first parameter is the message to show. The second allows inserting data from
90
     * the webpage in the message using positional placeholders.
91
     *
92
     * @param string $sType
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
93
     * @param string $sMessage
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
94
     * @param array $aArgs
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
95
     *
96
     * @return Call
97
     */
98
    private function setMessage(string $sType, string $sMessage, array $aArgs): Call
99
    {
100
        $this->aMessage = [
101
            'lib' => $this->xDialogLibraryManager->getMessageLibrary()->getName(),
102
            'type' => $sType,
103
            'phrase' => $this->makePhrase($sMessage, $aArgs),
104
        ];
105
        return $this;
106
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
107
108
    /**
109
     * Show a message if the condition to the call is not met
110
     *
111
     * @param string $sMessage  The message to show
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 2 found
Loading history...
112
     *
113
     * @return Call
114
     */
115
    public function elseShow(string $sMessage): Call
116
    {
117
        return $this->setMessage('warning', $sMessage, func_get_args());
118
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
119
120
    /**
121
     * Show an information message if the condition to the call is not met
122
     *
123
     * @param string $sMessage  The message to show
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 2 found
Loading history...
124
     *
125
     * @return Call
126
     */
127
    public function elseInfo(string $sMessage): Call
128
    {
129
        return $this->setMessage('info', $sMessage, func_get_args());
130
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
131
132
    /**
133
     * Show a success message if the condition to the call is not met
134
     *
135
     * @param string $sMessage  The message to show
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 2 found
Loading history...
136
     *
137
     * @return Call
138
     */
139
    public function elseSuccess(string $sMessage): Call
140
    {
141
        return $this->setMessage('success', $sMessage, func_get_args());
142
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
143
144
    /**
145
     * Show a warning message if the condition to the call is not met
146
     *
147
     * @param string $sMessage  The message to show
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 2 found
Loading history...
148
     *
149
     * @return Call
150
     */
151
    public function elseWarning(string $sMessage): Call
152
    {
153
        return $this->setMessage('warning', $sMessage, func_get_args());
154
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
155
156
    /**
157
     * Show an error message if the condition to the call is not met
158
     *
159
     * @param string $sMessage  The message to show
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 2 found
Loading history...
160
     *
161
     * @return Call
162
     */
163
    public function elseError(string $sMessage): Call
164
    {
165
        return $this->setMessage('error', $sMessage, func_get_args());
166
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
167
168
    /**
169
     * Add a condition to the request
170
     *
171
     * The request is sent only if the condition is true.
172
     *
173
     * @param mixed $xCondition    The condition to check
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
174
     *
175
     * @return Call
176
     */
177
    public function when($xCondition): Call
178
    {
179
        $this->aCondition = [true, Parameter::make($xCondition)];
180
        return $this;
181
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
182
183
    /**
184
     * Add a condition to the request
185
     *
186
     * The request is sent only if the condition is false.
187
     *
188
     * @param mixed $xCondition    The condition to check
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
189
     *
190
     * @return Call
191
     */
192
    public function unless($xCondition): Call
193
    {
194
        $this->aCondition = [false, Parameter::make($xCondition)];
195
        return $this;
196
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
197
198
    /**
199
     * Check if a value is equal to another before sending the request
200
     *
201
     * @param mixed $xValue1    The first value to compare
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
202
     * @param mixed $xValue2    The second value to compare
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
203
     *
204
     * @return Call
205
     */
206
    public function ifeq($xValue1, $xValue2): Call
207
    {
208
        $this->aCondition = ['==', Parameter::make($xValue1), Parameter::make($xValue2)];
209
        return $this;
210
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
211
212
    /**
213
     * Check if a value is equal to another before sending the request
214
     *
215
     * @param mixed $xValue1    The first value to compare
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
216
     * @param mixed $xValue2    The second value to compare
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
217
     *
218
     * @return Call
219
     */
220
    public function ifteq($xValue1, $xValue2): Call
221
    {
222
        $this->aCondition = ['===', Parameter::make($xValue1), Parameter::make($xValue2)];
223
        return $this;
224
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
225
226
    /**
227
     * Check if a value is not equal to another before sending the request
228
     *
229
     * @param mixed $xValue1    The first value to compare
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
230
     * @param mixed $xValue2    The second value to compare
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
231
     *
232
     * @return Call
233
     */
234
    public function ifne($xValue1, $xValue2): Call
235
    {
236
        $this->aCondition = ['!=', Parameter::make($xValue1), Parameter::make($xValue2)];
237
        return $this;
238
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
239
240
    /**
241
     * Check if a value is not equal to another before sending the request
242
     *
243
     * @param mixed $xValue1    The first value to compare
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
244
     * @param mixed $xValue2    The second value to compare
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
245
     *
246
     * @return Call
247
     */
248
    public function ifnte($xValue1, $xValue2): Call
249
    {
250
        $this->aCondition = ['!==', Parameter::make($xValue1), Parameter::make($xValue2)];
251
        return $this;
252
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
253
254
    /**
255
     * Check if a value is greater than another before sending the request
256
     *
257
     * @param mixed $xValue1    The first value to compare
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
258
     * @param mixed $xValue2    The second value to compare
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
259
     *
260
     * @return Call
261
     */
262
    public function ifgt($xValue1, $xValue2): Call
263
    {
264
        $this->aCondition = ['>', Parameter::make($xValue1), Parameter::make($xValue2)];
265
        return $this;
266
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
267
268
    /**
269
     * Check if a value is greater or equal to another before sending the request
270
     *
271
     * @param mixed $xValue1    The first value to compare
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
272
     * @param mixed $xValue2    The second value to compare
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
273
     *
274
     * @return Call
275
     */
276
    public function ifge($xValue1, $xValue2): Call
277
    {
278
        $this->aCondition = ['>=', Parameter::make($xValue1), Parameter::make($xValue2)];
279
        return $this;
280
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
281
282
    /**
283
     * Check if a value is lower than another before sending the request
284
     *
285
     * @param mixed $xValue1    The first value to compare
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
286
     * @param mixed $xValue2    The second value to compare
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
287
     *
288
     * @return Call
289
     */
290
    public function iflt($xValue1, $xValue2): Call
291
    {
292
        $this->aCondition = ['<', Parameter::make($xValue1), Parameter::make($xValue2)];
293
        return $this;
294
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
295
296
    /**
297
     * Check if a value is lower or equal to another before sending the request
298
     *
299
     * @param mixed $xValue1    The first value to compare
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
300
     * @param mixed $xValue2    The second value to compare
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
301
     *
302
     * @return Call
303
     */
304
    public function ifle($xValue1, $xValue2): Call
305
    {
306
        $this->aCondition = ['<=', Parameter::make($xValue1), Parameter::make($xValue2)];
307
        return $this;
308
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
309
310
    /**
311
     * Add a confirmation question to the request
312
     *
313
     * @param string $sQuestion    The question to ask
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
314
     *
315
     * @return Call
316
     */
317
    public function confirm(string $sQuestion): Call
318
    {
319
        $this->aConfirm = [
320
            'lib' => $this->xDialogLibraryManager->getQuestionLibrary()->getName(),
321
            'phrase' => $this->makePhrase($sQuestion, func_get_args()),
322
        ];
323
        return $this;
324
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
325
326
    /**
327
     * Convert this call to array
328
     *
329
     * @return array
330
     */
331
    public function toArray(): array
332
    {
333
        $aCall = parent::toArray();
334
        if(($this->aConfirm))
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->aConfirm of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
335
        {
336
            $aCall['confirm'] = $this->aConfirm;
337
        }
338
        if(($this->aCondition))
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->aCondition of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
339
        {
340
            $aCall['condition'] = $this->aCondition;
341
        }
342
        if(($this->aMessage))
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->aMessage of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
343
        {
344
            $aCall['else'] = $this->aMessage;
345
        }
346
        return $aCall;
347
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
348
349
    /**
350
     * Check if the request has a parameter of type Parameter::PAGE_NUMBER
351
     *
352
     * @return ParameterInterface|null
353
     */
354
    private function findPageNumber(): ?ParameterInterface
355
    {
356
        foreach($this->aParameters as $xParameter)
357
        {
358
            if($xParameter->getType() === Parameter::PAGE_NUMBER)
359
            {
360
                return $xParameter;
361
            }
362
        }
363
        return null;
364
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
365
366
    /**
367
     * Check if the request has a parameter of type Parameter::PAGE_NUMBER
368
     *
369
     * @return bool
370
     */
371
    public function hasPageNumber(): bool
372
    {
373
        return $this->findPageNumber() !== null;
374
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
375
376
    /**
377
     * Set a value to the Parameter::PAGE_NUMBER parameter
378
     *
379
     * @param integer $nPageNumber    The current page number
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
380
     *
381
     * @return Call
382
     */
383
    public function setPageNumber(int $nPageNumber): Call
384
    {
385
        /** @var Parameter */
0 ignored issues
show
Coding Style introduced by
Block comments must be started with /*
Loading history...
386
        $xParameter = $this->findPageNumber();
387
        if($xParameter !== null)
388
        {
389
            $xParameter->setValue($nPageNumber);
0 ignored issues
show
Bug introduced by
The method setValue() does not exist on Jaxon\Request\Call\ParameterInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to Jaxon\Request\Call\ParameterInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

389
            $xParameter->/** @scrutinizer ignore-call */ 
390
                         setValue($nPageNumber);
Loading history...
390
        }
391
        return $this;
392
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 0 found
Loading history...
393
}
394