Passed
Push — v5.x ( 26d105...7911f3 )
by Thierry
03:11
created

EventTrait::addEventHandler()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 5
rs 10
cc 1
nc 1
nop 4
1
<?php
2
3
/**
4
 * EventTrait.php
5
 *
6
 * Provides javascript related commands for the Response
7
 *
8
 * @author Thierry Feuzeu <[email protected]>
9
 * @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License
10
 * @link https://github.com/jaxon-php/jaxon-core
11
 */
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...
Coding Style introduced by
Missing @package tag in file comment
Loading history...
12
13
namespace Jaxon\Response\Traits;
14
15
use Jaxon\Response\ResponseInterface;
16
trait EventTrait
0 ignored issues
show
Coding Style introduced by
Missing doc comment for trait EventTrait
Loading history...
17
{
18
    /**
19
     * Add a response command to the array of commands that will be sent to the browser
20
     *
21
     * @param string $sName    The command name
0 ignored issues
show
Coding Style introduced by
Expected 8 spaces after parameter name; 4 found
Loading history...
22
     * @param array $aAttributes    Associative array of attributes that will describe the command
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Expected 2 spaces after parameter name; 4 found
Loading history...
23
     * @param mixed $mData    The data to be associated with this command
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Expected 8 spaces after parameter name; 4 found
Loading history...
24
     * @param bool $bRemoveEmpty    If true, remove empty attributes
0 ignored issues
show
Coding Style introduced by
Expected 3 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
25
     *
26
     * @return ResponseInterface
27
     */
28
    abstract protected function _addCommand(string $sName, array $aAttributes,
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 0 found
Loading history...
29
        $mData, bool $bRemoveEmpty = false): ResponseInterface;
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
30
31
    /**
32
     * Add a command to install an event handler on the specified element
33
     *
34
     * You can add more than one event handler to an element's event using this method.
35
     *
36
     * @param string $sTarget    The id of the element
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 4 found
Loading history...
37
     * @param string $sEvent    The name of the event
0 ignored issues
show
Coding Style introduced by
Expected 3 spaces after parameter name; 4 found
Loading history...
38
     * @param string $sHandler    The name of the javascript function to call when the event is fired
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
39
     *
40
     * @return ResponseInterface
41
     */
42
    public function addHandler(string $sTarget, string $sEvent, string $sHandler): ResponseInterface
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 1 found
Loading history...
43
    {
44
        $aAttributes = ['id' => $sTarget, 'prop' => $sEvent];
45
        return $this->_addCommand('ah', $aAttributes, $sHandler);
46
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
47
48
    /**
49
     * Add a command to remove an event handler from an element
50
     *
51
     * @param string $sTarget    The id of the element
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 4 found
Loading history...
52
     * @param string $sEvent    The name of the event
0 ignored issues
show
Coding Style introduced by
Expected 3 spaces after parameter name; 4 found
Loading history...
53
     * @param string $sHandler    The name of the javascript function called when the event is fired
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
54
     *
55
     * @return ResponseInterface
56
     */
57
    public function removeHandler(string $sTarget, string $sEvent, string $sHandler): ResponseInterface
58
    {
59
        $aAttributes = ['id' => $sTarget, 'prop' => $sEvent];
60
        return $this->_addCommand('rh', $aAttributes, $sHandler);
61
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
62
63
    /**
64
     * Add a command to add an event handler on the specified element
65
     * This handler can take custom parameters, and is is executed in a specific context.
66
     *
67
     * @param string $sTarget    The id of the element
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 4 found
Loading history...
68
     * @param string $sEvent    The name of the event
0 ignored issues
show
Coding Style introduced by
Expected 3 spaces after parameter name; 4 found
Loading history...
69
     * @param string $sHandler    The name of the javascript function to call when the event is fired
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
70
     * @param array $aParams    The parameters to pass to the event handler
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Expected 2 spaces after parameter name; 4 found
Loading history...
71
     *
72
     * @return ResponseInterface
73
     */
74
    public function addEventHandler(string $sTarget, string $sEvent,
75
        string $sHandler, array $aParams = []): ResponseInterface
76
    {
77
        $aAttributes = ['id' => $sTarget, 'prop' => $sEvent, 'func' => $sHandler];
78
        return $this->_addCommand('ae', $aAttributes, $aParams);
79
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
80
81
    /**
82
     * Add a command to set an event handler on the specified element
83
     * This handler can take custom parameters, and is is executed in a specific context.
84
     *
85
     * @param string $sTarget    The id of the element
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 4 found
Loading history...
86
     * @param string $sEvent    The name of the event
0 ignored issues
show
Coding Style introduced by
Expected 3 spaces after parameter name; 4 found
Loading history...
87
     * @param string $sHandler    The name of the javascript function to call when the event is fired
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
88
     * @param array $aParams    The parameters to pass to the event handler
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Expected 2 spaces after parameter name; 4 found
Loading history...
89
     *
90
     * @return ResponseInterface
91
     */
92
    public function setEventHandler(string $sTarget, string $sEvent,
93
        string $sHandler, array $aParams = []): ResponseInterface
94
    {
95
        $aAttributes = ['id' => $sTarget, 'prop' => $sEvent, 'func' => $sHandler];
96
        return $this->_addCommand('se', $aAttributes, $aParams);
97
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
98
99
    /**
100
     * Add a command to set a click handler on the browser
101
     *
102
     * @param string $sTarget    The id of the element that contains the event
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 4 found
Loading history...
103
     * @param string $sHandler    The name of the javascript function to call when the event is fired
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
104
     * @param array $aParams    The parameters to pass to the event handler
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Expected 2 spaces after parameter name; 4 found
Loading history...
105
     *
106
     * @return ResponseInterface
107
     */
108
    public function onClick(string $sTarget, string $sHandler, array $aParams = []): ResponseInterface
109
    {
110
        return $this->setEventHandler($sTarget, 'onclick', $sHandler, $aParams);
111
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 0 found
Loading history...
112
}
113