Passed
Push — v5.x ( 57c2ac...73bdb8 )
by Thierry
16:58 queued 05:55
created

Call::hasPageNumber()   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 0
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_shift;
26
use function func_get_args;
27
28
class Call extends JsCall
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class Call
Loading history...
29
{
30
    /**
31
     * @var DialogLibraryManager
32
     */
33
    protected $xLibraryManager;
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line(s) before first member var; 0 found
Loading history...
34
35
    /**
36
     * The arguments of the else() calls
37
     *
38
     * @var array
39
     */
40
    protected $aMessage = [];
41
42
    /**
43
     * A condition to check before making the call
44
     *
45
     * @var array
46
     */
47
    protected $aCondition = [];
48
49
    /**
50
     * The arguments of the confirm() call
51
     *
52
     * @var array
53
     */
54
    protected $aConfirm = [];
55
56
    /**
57
     * The constructor.
58
     *
59
     * @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 11 spaces after parameter name; 4 found
Loading history...
60
     * @param DialogLibraryManager $xLibraryManager
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
61
     */
62
    public function __construct(string $sName, DialogLibraryManager $xLibraryManager)
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 1 found
Loading history...
63
    {
64
        parent::__construct($sName);
65
        $this->xLibraryManager = $xLibraryManager;
66
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
67
68
    /**
69
     * @param array $aArgs
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
70
     *
71
     * @return array
72
     */
73
    private function getArgs(array $aArgs): array
74
    {
75
        array_shift($aArgs);
76
        return $aArgs;
77
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
78
79
    /**
80
     * Show a message if the condition to the call is not met
81
     *
82
     * @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...
83
     *
84
     * @return Call
85
     */
86
    public function elseShow(string $sMessage): Call
87
    {
88
        $this->aMessage = $this->xLibraryManager->warning($sMessage, $this->getArgs(func_get_args()));
89
        return $this;
90
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
91
92
    /**
93
     * Show an information message if the condition to the call is not met
94
     *
95
     * @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...
96
     *
97
     * @return Call
98
     */
99
    public function elseInfo(string $sMessage): Call
100
    {
101
        $this->aMessage = $this->xLibraryManager->info($sMessage, $this->getArgs(func_get_args()));
102
        return $this;
103
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
104
105
    /**
106
     * Show a success message if the condition to the call is not met
107
     *
108
     * @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...
109
     *
110
     * @return Call
111
     */
112
    public function elseSuccess(string $sMessage): Call
113
    {
114
        $this->aMessage = $this->xLibraryManager->success($sMessage, $this->getArgs(func_get_args()));
115
        return $this;
116
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
117
118
    /**
119
     * Show a warning message if the condition to the call is not met
120
     *
121
     * @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...
122
     *
123
     * @return Call
124
     */
125
    public function elseWarning(string $sMessage): Call
126
    {
127
        $this->aMessage = $this->xLibraryManager->warning($sMessage, $this->getArgs(func_get_args()));
128
        return $this;
129
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
130
131
    /**
132
     * Show an error message if the condition to the call is not met
133
     *
134
     * @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...
135
     *
136
     * @return Call
137
     */
138
    public function elseError(string $sMessage): Call
139
    {
140
        $this->aMessage = $this->xLibraryManager->error($sMessage, $this->getArgs(func_get_args()));
141
        return $this;
142
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
143
144
    /**
145
     * Add a confirmation question to the request
146
     *
147
     * @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...
148
     *
149
     * @return Call
150
     */
151
    public function confirm(string $sQuestion): Call
152
    {
153
        $this->aConfirm = $this->xLibraryManager->confirm($sQuestion, $this->getArgs(func_get_args()));
154
        return $this;
155
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
156
157
    /**
158
     * Add a condition to the request
159
     *
160
     * The request is sent only if the condition is true.
161
     *
162
     * @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...
163
     *
164
     * @return Call
165
     */
166
    public function when($xCondition): Call
167
    {
168
        $this->aCondition = [true, Parameter::make($xCondition)];
169
        return $this;
170
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
171
172
    /**
173
     * Add a condition to the request
174
     *
175
     * The request is sent only if the condition is false.
176
     *
177
     * @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...
178
     *
179
     * @return Call
180
     */
181
    public function unless($xCondition): Call
182
    {
183
        $this->aCondition = [false, Parameter::make($xCondition)];
184
        return $this;
185
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
186
187
    /**
188
     * Check if a value is equal to another before sending the request
189
     *
190
     * @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...
191
     * @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...
192
     *
193
     * @return Call
194
     */
195
    public function ifeq($xValue1, $xValue2): Call
196
    {
197
        $this->aCondition = ['==', Parameter::make($xValue1), Parameter::make($xValue2)];
198
        return $this;
199
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
200
201
    /**
202
     * Check if a value is equal to another before sending the request
203
     *
204
     * @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...
205
     * @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...
206
     *
207
     * @return Call
208
     */
209
    public function ifteq($xValue1, $xValue2): Call
210
    {
211
        $this->aCondition = ['===', Parameter::make($xValue1), Parameter::make($xValue2)];
212
        return $this;
213
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
214
215
    /**
216
     * Check if a value is not equal to another before sending the request
217
     *
218
     * @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...
219
     * @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...
220
     *
221
     * @return Call
222
     */
223
    public function ifne($xValue1, $xValue2): Call
224
    {
225
        $this->aCondition = ['!=', Parameter::make($xValue1), Parameter::make($xValue2)];
226
        return $this;
227
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
228
229
    /**
230
     * Check if a value is not equal to another before sending the request
231
     *
232
     * @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...
233
     * @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...
234
     *
235
     * @return Call
236
     */
237
    public function ifnte($xValue1, $xValue2): Call
238
    {
239
        $this->aCondition = ['!==', Parameter::make($xValue1), Parameter::make($xValue2)];
240
        return $this;
241
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
242
243
    /**
244
     * Check if a value is greater than another before sending the request
245
     *
246
     * @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...
247
     * @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...
248
     *
249
     * @return Call
250
     */
251
    public function ifgt($xValue1, $xValue2): Call
252
    {
253
        $this->aCondition = ['>', Parameter::make($xValue1), Parameter::make($xValue2)];
254
        return $this;
255
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
256
257
    /**
258
     * Check if a value is greater or equal to another before sending the request
259
     *
260
     * @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...
261
     * @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...
262
     *
263
     * @return Call
264
     */
265
    public function ifge($xValue1, $xValue2): Call
266
    {
267
        $this->aCondition = ['>=', Parameter::make($xValue1), Parameter::make($xValue2)];
268
        return $this;
269
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
270
271
    /**
272
     * Check if a value is lower than another before sending the request
273
     *
274
     * @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...
275
     * @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...
276
     *
277
     * @return Call
278
     */
279
    public function iflt($xValue1, $xValue2): Call
280
    {
281
        $this->aCondition = ['<', Parameter::make($xValue1), Parameter::make($xValue2)];
282
        return $this;
283
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
284
285
    /**
286
     * Check if a value is lower or equal to another before sending the request
287
     *
288
     * @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...
289
     * @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...
290
     *
291
     * @return Call
292
     */
293
    public function ifle($xValue1, $xValue2): Call
294
    {
295
        $this->aCondition = ['<=', Parameter::make($xValue1), Parameter::make($xValue2)];
296
        return $this;
297
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
298
299
    /**
300
     * Convert this call to array
301
     *
302
     * @return array
303
     */
304
    public function toArray(): array
305
    {
306
        $aCall = parent::toArray();
307
        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...
308
        {
309
            $aCall['confirm'] = [
310
                ...$this->aConfirm,
311
                'lib' => $this->xLibraryManager->getQuestionLibrary()->getName(),
312
            ];
313
        }
314
        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...
315
        {
316
            $aCall['condition'] = $this->aCondition;
317
        }
318
        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...
319
        {
320
            $aCall['else'] = [
321
                ...$this->aMessage,
322
                'lib' => $this->xLibraryManager->getMessageLibrary()->getName(),
323
            ];
324
        }
325
        return $aCall;
326
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
327
328
    /**
329
     * Check if the request has a parameter of type Parameter::PAGE_NUMBER
330
     *
331
     * @return ParameterInterface|null
332
     */
333
    private function findPageNumber(): ?ParameterInterface
334
    {
335
        foreach($this->aParameters as $xParameter)
336
        {
337
            if($xParameter->getType() === Parameter::PAGE_NUMBER)
338
            {
339
                return $xParameter;
340
            }
341
        }
342
        return null;
343
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
344
345
    /**
346
     * Check if the request has a parameter of type Parameter::PAGE_NUMBER
347
     *
348
     * @return bool
349
     */
350
    public function hasPageNumber(): bool
351
    {
352
        return $this->findPageNumber() !== null;
353
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
354
355
    /**
356
     * Set a value to the Parameter::PAGE_NUMBER parameter
357
     *
358
     * @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...
359
     *
360
     * @return Call
361
     */
362
    public function setPageNumber(int $nPageNumber): Call
363
    {
364
        /** @var Parameter */
0 ignored issues
show
Coding Style introduced by
Block comments must be started with /*
Loading history...
365
        $xParameter = $this->findPageNumber();
366
        if($xParameter !== null)
367
        {
368
            $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

368
            $xParameter->/** @scrutinizer ignore-call */ 
369
                         setValue($nPageNumber);
Loading history...
369
        }
370
        return $this;
371
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 0 found
Loading history...
372
}
373