JsExpr::elseWarning()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
rs 10
c 3
b 0
f 0
1
<?php
2
3
/**
4
 * JsExpr.php
5
 *
6
 * An expression to be formatted in json, that represents a call to a js function or selector.
7
 *
8
 * @package jaxon-core
9
 * @author Thierry Feuzeu <[email protected]>
10
 * @copyright 2024 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
 */
14
15
namespace Jaxon\Script;
16
17
use Jaxon\App\Dialog\Manager\DialogCommand;
18
use Jaxon\Script\Action\Attr;
19
use Jaxon\Script\Action\Event;
20
use Jaxon\Script\Action\Func;
21
use Jaxon\Script\Action\TypedValue;
22
use JsonSerializable;
23
use Stringable;
24
25
use function array_map;
26
use function is_a;
27
use function is_array;
28
use function json_encode;
29
30
class JsExpr extends TypedValue implements Stringable
31
{
32
    /**
33
     * The actions to be applied on the selected element
34
     *
35
     * @var array
36
     */
37
    protected $aCalls = [];
38
39
    /**
40
     * The arguments of the else() calls
41
     *
42
     * @var array
43
     */
44
    protected $aAlert = [];
45
46
    /**
47
     * A condition to check before making the call
48
     *
49
     * @var array
50
     */
51
    protected $aCondition = [];
52
53
    /**
54
     * The arguments of the confirm() call
55
     *
56
     * @var array
57
     */
58
    protected $aConfirm = [];
59
60
    /**
61
     * @var DialogCommand
62
     */
63
    private static DialogCommand $xDialogCommand;
64
65
    /**
66
     * The constructor
67
     */
68
    public function __construct(...$aCalls)
69
    {
70
        $this->aCalls = $aCalls;
71
    }
72
73
    /**
74
     * Set the dialog command
75
     *
76
     * @param DialogCommand $xDialogCommand
77
     *
78
     * @return void
79
     */
80
    public static function setDialogCommand(DialogCommand $xDialogCommand): void
81
    {
82
        self::$xDialogCommand = $xDialogCommand;
83
    }
84
85
    /**
86
     * Get the first function in the calls
87
     *
88
     * @return Func|null
89
     */
90
    public function func(): ?Func
91
    {
92
        foreach($this->aCalls as $xCall)
93
        {
94
            if(is_a($xCall, Func::class))
95
            {
96
                return $xCall;
97
            }
98
        }
99
        return null;
100
    }
101
102
    /**
103
     * Add a call to a js function on the current object
104
     *
105
     * @param string  $sMethod
106
     * @param array  $aArguments
107
     *
108
     * @return self
109
     */
110
    public function __call(string $sMethod, array $aArguments): self
111
    {
112
        // Append the action into the array
113
        $this->aCalls[] = new Func($sMethod, $aArguments);
114
        return $this;
115
    }
116
117
    /**
118
     * Get the value of an attribute of the current object
119
     *
120
     * @param string  $sAttribute
121
     *
122
     * @return self
123
     */
124
    public function __get(string $sAttribute): self
125
    {
126
        // Append the action into the array
127
        $this->aCalls[] = Attr::get($sAttribute);
128
        return $this;
129
    }
130
131
    /**
132
     * Set the value of an attribute of the current object
133
     *
134
     * @param string $sAttribute
135
     * @param mixed $xValue
136
     *
137
     * @return void
138
     */
139
    public function __set(string $sAttribute, $xValue)
140
    {
141
        // Append the action into the array
142
        $this->aCalls[] = Attr::set($sAttribute, $xValue);
143
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type Jaxon\Script\JsExpr which is incompatible with the documented return type void.
Loading history...
144
    }
145
146
    /**
147
     * Set an event handler on the selected elements
148
     *
149
     * @param string $sMode    The event mode: 'jq' or 'js'
150
     * @param string $sName
151
     * @param JsExpr $xHandler
152
     *
153
     * @return self
154
     */
155
    public function event(string $sMode, string $sName, JsExpr $xHandler): self
156
    {
157
        $this->aCalls[] = new Event($sMode, $sName, $xHandler);
158
        return $this;
159
    }
160
161
    /**
162
     * Show a message if the condition to the call is not met
163
     *
164
     * @param string $sMessage  The message to show
165
     * @param array $aArgs      The message arguments
166
     *
167
     * @return self
168
     */
169
    public function elseShow(string $sMessage, ...$aArgs): self
170
    {
171
        $this->aAlert = self::$xDialogCommand->warning($sMessage, $aArgs);
172
        return $this;
173
    }
174
175
    /**
176
     * Show an information message if the condition to the call is not met
177
     *
178
     * @param string $sMessage  The message to show
179
     * @param array $aArgs      The message arguments
180
     *
181
     * @return self
182
     */
183
    public function elseInfo(string $sMessage, ...$aArgs): self
184
    {
185
        $this->aAlert = self::$xDialogCommand->info($sMessage, $aArgs);
186
        return $this;
187
    }
188
189
    /**
190
     * Show a success message if the condition to the call is not met
191
     *
192
     * @param string $sMessage  The message to show
193
     * @param array $aArgs      The message arguments
194
     *
195
     * @return self
196
     */
197
    public function elseSuccess(string $sMessage, ...$aArgs): self
198
    {
199
        $this->aAlert = self::$xDialogCommand->success($sMessage, $aArgs);
200
        return $this;
201
    }
202
203
    /**
204
     * Show a warning message if the condition to the call is not met
205
     *
206
     * @param string $sMessage  The message to show
207
     * @param array $aArgs      The message arguments
208
     *
209
     * @return self
210
     */
211
    public function elseWarning(string $sMessage, ...$aArgs): self
212
    {
213
        $this->aAlert = self::$xDialogCommand->warning($sMessage, $aArgs);
214
        return $this;
215
    }
216
217
    /**
218
     * Show an error message if the condition to the call is not met
219
     *
220
     * @param string $sMessage  The message to show
221
     * @param array $aArgs      The message arguments
222
     *
223
     * @return self
224
     */
225
    public function elseError(string $sMessage, ...$aArgs): self
226
    {
227
        $this->aAlert = self::$xDialogCommand->error($sMessage, $aArgs);
228
        return $this;
229
    }
230
231
    /**
232
     * Add a confirmation question to the request
233
     *
234
     * @param string $sQuestion The question to ask
235
     * @param array $aArgs      The question arguments
236
     *
237
     * @return self
238
     */
239
    public function confirm(string $sQuestion, ...$aArgs): self
240
    {
241
        $this->aConfirm = self::$xDialogCommand->confirm($sQuestion, $aArgs);
242
        return $this;
243
    }
244
245
    /**
246
     * Check if a value is equal to another before sending the request
247
     *
248
     * @param mixed $xValue1    The first value to compare
249
     * @param mixed $xValue2    The second value to compare
250
     *
251
     * @return self
252
     */
253
    public function ifeq($xValue1, $xValue2): self
254
    {
255
        $this->aCondition = ['eq', TypedValue::make($xValue1), TypedValue::make($xValue2)];
256
        return $this;
257
    }
258
259
    /**
260
     * Check if a value is equal to another before sending the request
261
     *
262
     * @param mixed $xValue1    The first value to compare
263
     * @param mixed $xValue2    The second value to compare
264
     *
265
     * @return self
266
     */
267
    public function ifteq($xValue1, $xValue2): self
268
    {
269
        $this->aCondition = ['teq', TypedValue::make($xValue1), TypedValue::make($xValue2)];
270
        return $this;
271
    }
272
273
    /**
274
     * Check if a value is not equal to another before sending the request
275
     *
276
     * @param mixed $xValue1    The first value to compare
277
     * @param mixed $xValue2    The second value to compare
278
     *
279
     * @return self
280
     */
281
    public function ifne($xValue1, $xValue2): self
282
    {
283
        $this->aCondition = ['ne', TypedValue::make($xValue1), TypedValue::make($xValue2)];
284
        return $this;
285
    }
286
287
    /**
288
     * Check if a value is not equal to another before sending the request
289
     *
290
     * @param mixed $xValue1    The first value to compare
291
     * @param mixed $xValue2    The second value to compare
292
     *
293
     * @return self
294
     */
295
    public function ifnte($xValue1, $xValue2): self
296
    {
297
        $this->aCondition = ['nte', TypedValue::make($xValue1), TypedValue::make($xValue2)];
298
        return $this;
299
    }
300
301
    /**
302
     * Check if a value is greater than another before sending the request
303
     *
304
     * @param mixed $xValue1    The first value to compare
305
     * @param mixed $xValue2    The second value to compare
306
     *
307
     * @return self
308
     */
309
    public function ifgt($xValue1, $xValue2): self
310
    {
311
        $this->aCondition = ['gt', TypedValue::make($xValue1), TypedValue::make($xValue2)];
312
        return $this;
313
    }
314
315
    /**
316
     * Check if a value is greater or equal to another before sending the request
317
     *
318
     * @param mixed $xValue1    The first value to compare
319
     * @param mixed $xValue2    The second value to compare
320
     *
321
     * @return self
322
     */
323
    public function ifge($xValue1, $xValue2): self
324
    {
325
        $this->aCondition = ['ge', TypedValue::make($xValue1), TypedValue::make($xValue2)];
326
        return $this;
327
    }
328
329
    /**
330
     * Check if a value is lower than another before sending the request
331
     *
332
     * @param mixed $xValue1    The first value to compare
333
     * @param mixed $xValue2    The second value to compare
334
     *
335
     * @return self
336
     */
337
    public function iflt($xValue1, $xValue2): self
338
    {
339
        $this->aCondition = ['lt', TypedValue::make($xValue1), TypedValue::make($xValue2)];
340
        return $this;
341
    }
342
343
    /**
344
     * Check if a value is lower or equal to another before sending the request
345
     *
346
     * @param mixed $xValue1    The first value to compare
347
     * @param mixed $xValue2    The second value to compare
348
     *
349
     * @return self
350
     */
351
    public function ifle($xValue1, $xValue2): self
352
    {
353
        $this->aCondition = ['le', TypedValue::make($xValue1), TypedValue::make($xValue2)];
354
        return $this;
355
    }
356
357
    /**
358
     * Add a condition to the request
359
     *
360
     * The request is sent only if the condition is true.
361
     *
362
     * @param mixed $xCondition    The condition to check
363
     *
364
     * @return self
365
     */
366
    public function when($xCondition): self
367
    {
368
        return $this->ifeq(true, $xCondition);
369
    }
370
371
    /**
372
     * Add a condition to the request
373
     *
374
     * The request is sent only if the condition is false.
375
     *
376
     * @param mixed $xCondition    The condition to check
377
     *
378
     * @return self
379
     */
380
    public function unless($xCondition): self
381
    {
382
        return $this->ifeq(false, $xCondition);
383
    }
384
385
    /**
386
     * @return self
387
     */
388
    public function toInt(): self
389
    {
390
        $this->aCalls[] = [
391
            '_type' => 'func',
392
            '_name' => 'toInt',
393
        ];
394
        return $this;
395
    }
396
397
    /**
398
     * @return self
399
     */
400
    public function trim(): self
401
    {
402
        $this->aCalls[] = [
403
            '_type' => 'func',
404
            '_name' => 'trim',
405
        ];
406
        return $this;
407
    }
408
409
    /**
410
     * @inheritDoc
411
     */
412
    public function getType(): string
413
    {
414
        return 'expr';
415
    }
416
417
    /**
418
     * Convert this call to array, when converting the response into json.
419
     *
420
     * @return array
421
     */
422
    public function jsonSerialize(): array
423
    {
424
        $aJsExpr = [
425
            '_type' => $this->getType(),
426
            'calls' => array_map(fn(JsonSerializable|array $xCall) =>
427
                is_array($xCall) ? $xCall : $xCall->jsonSerialize(), $this->aCalls),
0 ignored issues
show
introduced by
The condition is_array($xCall) is always true.
Loading history...
428
        ];
429
        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...
430
        {
431
            $aJsExpr['confirm'] = $this->aConfirm;
432
        }
433
        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...
434
        {
435
            $aJsExpr['condition'] = $this->aCondition;
436
        }
437
        if(($this->aAlert))
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->aAlert 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...
438
        {
439
            $aJsExpr['alert'] = $this->aAlert;
440
        }
441
442
        return $aJsExpr;
443
    }
444
445
    /**
446
     * Returns a call to jaxon as a string
447
     *
448
     * @return string
449
     */
450
    public function __toString(): string
451
    {
452
        return 'jaxon.exec(' . json_encode($this->jsonSerialize()) . ')';
453
    }
454
}
455