Event::__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 2
dl 0
loc 4
rs 10
1
<?php
2
3
namespace Jaxon\Script\Call;
4
5
use Jaxon\Script\JsExpr;
6
use JsonSerializable;
7
use Stringable;
8
9
class Event implements JsonSerializable, Stringable
10
{
11
    /**
12
     * @var string
13
     */
14
    private $sName;
15
16
    /**
17
     * @var JsExpr
18
     */
19
    private $xHandler;
20
21
    /**
22
     * The constructor.
23
     *
24
     * @param string $sName    The event name
25
     * @param JsExpr $xHandler   The event handler
26
     */
27
    public function __construct(string $sName, JsExpr $xHandler)
28
    {
29
        $this->sName = $sName;
30
        $this->xHandler = $xHandler;
31
    }
32
33
    /**
34
     * Convert this call to array, when converting the response into json.
35
     *
36
     * @return array
37
     */
38
    public function jsonSerialize(): array
39
    {
40
        return [
41
            '_type' => 'event',
42
            '_name' => $this->sName,
43
            'func' => $this->xHandler->jsonSerialize(),
44
        ];
45
    }
46
47
    /**
48
     * Returns a string representation of this call
49
     *
50
     * @return string
51
     */
52
    public function __toString(): string
53
    {
54
        return "on('{$this->sName}', () => { " . $this->xHandler->__toString() . '; })';
55
    }
56
}
57