JsCall::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 4
rs 10
1
<?php
2
3
namespace Jaxon\Script;
4
5
/**
6
 * JsCall.php
7
 *
8
 * Call to a js function.
9
 *
10
 * @package jaxon-core
11
 * @author Thierry Feuzeu
12
 * @copyright 2024 Thierry Feuzeu <[email protected]>
13
 * @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License
14
 * @link https://github.com/jaxon-php/jaxon-core
15
 */
16
17
use Jaxon\Plugin\Response\Dialog\DialogCommand;
18
use Jaxon\Script\Call\Attr;
19
use Jaxon\Script\Call\Selector;
20
use Closure;
21
22
class JsCall extends AbstractJsCall
23
{
24
    /**
25
     * @var string
26
     */
27
    protected $sJsObject;
28
29
    /**
30
     * The class constructor
31
     *
32
     * @param DialogCommand $xDialog
33
     * @param Closure|null $xExprCb
34
     * @param string $sJsObject
35
     */
36
    public function __construct(DialogCommand $xDialog, ?Closure $xExprCb, string $sJsObject)
37
    {
38
        parent::__construct($xDialog, $xExprCb);
39
        $this->sJsObject = $sJsObject;
40
    }
41
42
    /**
43
     * Create a js expression
44
     *
45
     * @return JsExpr
46
     */
47
    protected function _expr(): JsExpr
48
    {
49
        $xJsExpr = match($this->sJsObject) {
50
            // An empty string returns the js "this" var.
51
            '' => new JsExpr($this->xDialog, new Selector('js', 'this')),
52
            // The 'w' string returns the js "window" object. No data needed.
53
            'w' => new JsExpr($this->xDialog),
54
            // Otherwise, the corresponding js object will be returned.
55
            default => new JsExpr($this->xDialog, Attr::get($this->sJsObject, false)),
56
        };
57
        return $this->_initExpr($xJsExpr);
58
    }
59
}
60