Passed
Push — v5.x ( 8eee51...080647 )
by Thierry
10:22
created

ScriptTrait::alert()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 1
c 2
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
/**
4
 * ScriptTrait.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
use JsonSerializable;
17
18
use function func_get_args;
19
use function array_shift;
20
21
trait ScriptTrait
0 ignored issues
show
Coding Style introduced by
Missing doc comment for trait ScriptTrait
Loading history...
22
{
23
     /**
24
     * Add a response command to the array of commands that will be sent to the browser
25
     *
26
     * @param string $sName    The command name
0 ignored issues
show
Coding Style introduced by
Expected 17 spaces after parameter type; 1 found
Loading history...
27
     * @param array|JsonSerializable $aOptions    The command options
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
28
     *
29
     * @return ResponseInterface
30
     */
31
    abstract public function addCommand(string $sName, array|JsonSerializable $aOptions): ResponseInterface;
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 0 found
Loading history...
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
32
33
    /**
34
     * Response command that prompts user with [ok] [cancel] style message box
35
     *
36
     * If the user clicks cancel, the specified number of response commands
37
     * following this one, will be skipped.
38
     *
39
     * @param integer $nCommandCount    The number of commands to skip upon cancel
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
40
     * @param string $sQuestion    The message to display to the user
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Expected 5 spaces after parameter name; 4 found
Loading history...
41
     *
42
     * @return ResponseInterface
43
     */
44
    public function confirmCommands(int $nCommandCount, string $sQuestion): ResponseInterface
45
    {
46
        return $this->addCommand('cc', [
47
            'count' => $nCommandCount,
48
            'question' => $this->str($sQuestion),
0 ignored issues
show
Bug introduced by
It seems like str() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

48
            'question' => $this->/** @scrutinizer ignore-call */ str($sQuestion),
Loading history...
49
        ]);
50
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
51
52
    /**
53
     * Add a command to call the specified javascript function with the given (optional) parameters
54
     *
55
     * @param string $sFunc    The name of the function to call
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
56
     *
57
     * @return ResponseInterface
58
     */
59
    public function call(string $sFunc): ResponseInterface
60
    {
61
        $aArgs = func_get_args();
62
        array_shift($aArgs);
63
        return $this->addCommand('jc', ['func' => $this->str($sFunc),'args' => $aArgs]);
64
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
65
66
    /**
67
     * Add a command to display an alert message to the user
68
     *
69
     * @param string $sMessage    The message to be displayed
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
70
     *
71
     * @return ResponseInterface
72
     */
73
    public function alert(string $sMessage): ResponseInterface
74
    {
75
        return $this->addCommand('al', ['message' => $this->str($sMessage)]);
76
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
77
78
    /**
79
     * Add a command to display a debug message to the user
80
     *
81
     * @param string $sMessage    The message to be displayed
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
82
     *
83
     * @return ResponseInterface
84
     */
85
    public function debug(string $sMessage): ResponseInterface
86
    {
87
        return $this->addCommand('dbg', ['message' => $this->str($sMessage)]);
88
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
89
90
    /**
91
     * Add a command to ask the browser to navigate to the specified URL
92
     *
93
     * @param string $sURL    The relative or fully qualified URL
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Expected 3 spaces after parameter name; 4 found
Loading history...
94
     * @param integer $nDelay    Number of seconds to delay before the redirect occurs
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
95
     *
96
     * @return ResponseInterface
97
     */
98
    public function redirect(string $sURL, int $nDelay = 0): ResponseInterface
99
    {
100
        return $this->addCommand('rd', [
101
            'delay' => $nDelay,
102
            'url' => $this->xPluginManager->getParameterReader()->parseUrl($sURL),
103
        ]);
104
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
105
106
    /**
107
     * Add a command to make Jaxon to pause execution of the response commands,
108
     * returning control to the browser so it can perform other commands asynchronously.
109
     *
110
     * After the specified delay, Jaxon will continue execution of the response commands.
111
     *
112
     * @param integer $tenths    The number of 1/10ths of a second to sleep
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
113
     *
114
     * @return ResponseInterface
115
     */
116
    public function sleep(int $tenths): ResponseInterface
117
    {
118
        return $this->addCommand('s', ['duration' => $tenths]);
119
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
120
121
    /**
122
     * Add a command to set an event handler on the specified element
123
     * This handler can take custom parameters, and is is executed in a specific context.
124
     *
125
     * @param string $sTarget    The id of the element
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
126
     * @param string $sEvent    The name of the event
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 4 found
Loading history...
127
     * @param array $aCall    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 3 spaces after parameter name; 4 found
Loading history...
128
     *
129
     * @return ResponseInterface
130
     */
131
    public function setEventHandler(string $sTarget, string $sEvent, array $aCall): ResponseInterface
132
    {
133
        return $this->addCommand('se', [
134
            'id' => $this->str($sTarget),
135
            'event' => $this->str($sEvent),
136
            'call' => $aCall,
137
        ]);
138
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
139
140
    /**
141
     * Add a command to set a click handler on the browser
142
     *
143
     * @param string $sTarget    The id of the element
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
144
     * @param array $aCall    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 3 spaces after parameter name; 4 found
Loading history...
145
     *
146
     * @return ResponseInterface
147
     */
148
    public function onClick(string $sTarget, array $aCall): ResponseInterface
149
    {
150
        return $this->setEventHandler($sTarget, 'onclick', $aCall);
151
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
152
153
    /**
154
     * Add a command to add an event handler on the specified element
155
     * This handler can take custom parameters, and is is executed in a specific context.
156
     *
157
     * @param string $sTarget    The id of the element
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
158
     * @param string $sEvent    The name of the event
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 4 found
Loading history...
159
     * @param array $aCall    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 3 spaces after parameter name; 4 found
Loading history...
160
     *
161
     * @return ResponseInterface
162
     */
163
    public function addEventHandler(string $sTarget, string $sEvent, array $aCall): ResponseInterface
164
    {
165
        return $this->addCommand('ae', [
166
            'id' => $this->str($sTarget),
167
            'event' => $this->str($sEvent),
168
            'call' => $aCall,
169
        ]);
170
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
171
172
    /**
173
     * Add a command to install an event handler on the specified element
174
     *
175
     * You can add more than one event handler to an element's event using this method.
176
     *
177
     * @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...
178
     * @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...
179
     * @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...
180
     *
181
     * @return ResponseInterface
182
     */
183
    public function addHandler(string $sTarget, string $sEvent, string $sHandler): ResponseInterface
184
    {
185
        return $this->addCommand('ah', [
186
            'id' => $this->str($sTarget),
187
            'event' => $this->str($sEvent),
188
            'func' => $this->str($sHandler),
189
        ]);
190
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
191
192
    /**
193
     * Add a command to remove an event handler from an element
194
     *
195
     * @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...
196
     * @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...
197
     * @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...
198
     *
199
     * @return ResponseInterface
200
     */
201
    public function removeHandler(string $sTarget, string $sEvent, string $sHandler): ResponseInterface
202
    {
203
        return $this->addCommand('rh', [
204
            'id' => $this->str($sTarget),
205
            'event' => $this->str($sEvent),
206
            'func' => $this->str($sHandler),
207
        ]);
208
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 0 found
Loading history...
209
}
210