Passed
Push — feature/code_improvement ( 84d6ec...916211 )
by Thierry
02:45
created

Request::iflt()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Request.php - The Jaxon Request
5
 *
6
 * This class is used to create client side requests to callable classes and functions.
7
 *
8
 * @package jaxon-core
0 ignored issues
show
Coding Style introduced by
Package name "jaxon-core" is not valid; consider "Jaxoncore" instead
Loading history...
9
 * @author Jared White
0 ignored issues
show
Coding Style introduced by
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
10
 * @author J. Max Wilson
0 ignored issues
show
Coding Style introduced by
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
11
 * @author Joseph Woolley
0 ignored issues
show
Coding Style introduced by
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
12
 * @author Steffen Konerow
0 ignored issues
show
Coding Style introduced by
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
13
 * @author Thierry Feuzeu <[email protected]>
14
 * @copyright Copyright (c) 2005-2007 by Jared White & J. Max Wilson
0 ignored issues
show
Coding Style introduced by
@copyright tag must contain a year and the name of the copyright holder
Loading history...
15
 * @copyright Copyright (c) 2008-2010 by Joseph Woolley, Steffen Konerow, Jared White  & J. Max Wilson
0 ignored issues
show
Coding Style introduced by
@copyright tag must contain a year and the name of the copyright holder
Loading history...
16
 * @copyright 2016 Thierry Feuzeu <[email protected]>
17
 * @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License
18
 * @link https://github.com/jaxon-php/jaxon-core
19
 */
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...
20
21
namespace Jaxon\Request\Factory;
22
23
use Jaxon\Request\Factory\Parameter;
24
use Jaxon\Request\Factory\Contracts\Parameter as ParameterContract;
25
use Jaxon\Response\Plugin\JQuery\Dom\Element as DomElement;
26
27
class Request extends JsCall
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class Request
Loading history...
28
{
29
    /**
30
     * A condition to check before sending this request
31
     *
32
     * @var string
33
     */
34
    protected $sCondition = null;
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line(s) before first member var; 0 found
Loading history...
35
36
    /**
37
     * The arguments of the confirm() call
38
     *
39
     * @var array
40
     */
41
    protected $aMessageArgs = [];
42
43
    /**
44
     * The constructor.
45
     *
46
     * @param string        $sName            The javascript function or method name
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter type; 8 found
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter name; 12 found
Loading history...
47
     */
48
    public function __construct($sName)
49
    {
50
        parent::__construct($sName);
51
    }
52
53
    /**
54
     * Add a condition to the request
55
     *
56
     * The request is sent only if the condition is true.
57
     *
58
     * @param string        $sCondition               The condition to check
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter type; 8 found
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter name; 15 found
Loading history...
59
     *
60
     * @return Request
61
     */
62
    public function when($sCondition)
63
    {
64
        $this->sCondition = Parameter::make($sCondition)->getScript();
65
        return $this;
66
    }
67
68
    /**
69
     * Add a condition to the request
70
     *
71
     * The request is sent only if the condition is false.
72
     *
73
     * @param string        $sCondition               The condition to check
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter type; 8 found
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter name; 15 found
Loading history...
74
     *
75
     * @return Request
76
     */
77
    public function unless($sCondition)
78
    {
79
        $this->sCondition = '!(' . Parameter::make($sCondition)->getScript() . ')';
80
        return $this;
81
    }
82
83
    /**
84
     * Check if a value is equal to another before sending the request
85
     *
86
     * @param string        $sValue1                  The first value to compare
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter type; 8 found
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter name; 18 found
Loading history...
87
     * @param string        $sValue2                  The second value to compare
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter type; 8 found
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter name; 18 found
Loading history...
88
     *
89
     * @return Request
90
     */
91
    public function ifeq($sValue1, $sValue2)
92
    {
93
        $this->sCondition = '(' . Parameter::make($sValue1) . '==' . Parameter::make($sValue2) . ')';
94
        return $this;
95
    }
96
97
    /**
98
     * Check if a value is not equal to another before sending the request
99
     *
100
     * @param string        $sValue1                  The first value to compare
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter type; 8 found
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter name; 18 found
Loading history...
101
     * @param string        $sValue2                  The second value to compare
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter type; 8 found
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter name; 18 found
Loading history...
102
     *
103
     * @return Request
104
     */
105
    public function ifne($sValue1, $sValue2)
106
    {
107
        $this->sCondition = '(' . Parameter::make($sValue1) . '!=' . Parameter::make($sValue2) . ')';
108
        return $this;
109
    }
110
111
    /**
112
     * Check if a value is greater than another before sending the request
113
     *
114
     * @param string        $sValue1                  The first value to compare
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter type; 8 found
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter name; 18 found
Loading history...
115
     * @param string        $sValue2                  The second value to compare
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter type; 8 found
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter name; 18 found
Loading history...
116
     *
117
     * @return Request
118
     */
119
    public function ifgt($sValue1, $sValue2)
120
    {
121
        $this->sCondition = '(' . Parameter::make($sValue1) . '>' . Parameter::make($sValue2) . ')';
122
        return $this;
123
    }
124
125
    /**
126
     * Check if a value is greater or equal to another before sending the request
127
     *
128
     * @param string        $sValue1                  The first value to compare
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter type; 8 found
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter name; 18 found
Loading history...
129
     * @param string        $sValue2                  The second value to compare
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter type; 8 found
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter name; 18 found
Loading history...
130
     *
131
     * @return Request
132
     */
133
    public function ifge($sValue1, $sValue2)
134
    {
135
        $this->sCondition = '(' . Parameter::make($sValue1) . '>=' . Parameter::make($sValue2) . ')';
136
        return $this;
137
    }
138
139
    /**
140
     * Check if a value is lower than another before sending the request
141
     *
142
     * @param string        $sValue1                  The first value to compare
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter type; 8 found
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter name; 18 found
Loading history...
143
     * @param string        $sValue2                  The second value to compare
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter type; 8 found
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter name; 18 found
Loading history...
144
     *
145
     * @return Request
146
     */
147
    public function iflt($sValue1, $sValue2)
148
    {
149
        $this->sCondition = '(' . Parameter::make($sValue1) . '<' . Parameter::make($sValue2) . ')';
150
        return $this;
151
    }
152
153
    /**
154
     * Check if a value is lower or equal to another before sending the request
155
     *
156
     * @param string        $sValue1                  The first value to compare
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter type; 8 found
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter name; 18 found
Loading history...
157
     * @param string        $sValue2                  The second value to compare
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter type; 8 found
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter name; 18 found
Loading history...
158
     *
159
     * @return Request
160
     */
161
    public function ifle($sValue1, $sValue2)
162
    {
163
        $this->sCondition = '(' . Parameter::make($sValue1) . '<=' . Parameter::make($sValue2) . ')';
164
        return $this;
165
    }
166
167
    /**
168
     * Create parameters for message arguments
169
     *
170
     * @param array     $aArgs          The arguments
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter type; 5 found
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter name; 10 found
Loading history...
171
     *
172
     * @return void
173
     */
174
    private function setMessageArgs(array $aArgs)
175
    {
176
        array_walk($aArgs, function(&$xParameter) {
177
            $xParameter = Parameter::make($xParameter);
178
        });
179
        $this->aMessageArgs = $aArgs;
180
    }
181
182
    /**
183
     * Add a confirmation question to the request
184
     *
185
     * @param string        $sQuestion                The question to ask
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter type; 8 found
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter name; 16 found
Loading history...
186
     *
187
     * @return Request
188
     */
189
    public function confirm($sQuestion)
190
    {
191
        $this->sCondition = '__confirm__';
192
        $this->setMessageArgs(func_get_args());
193
        return $this;
194
    }
195
196
    /**
197
     * Set the message to show if the condition to send the request is not met
198
     *
199
     * The first parameter is the message to show. The followin allow to insert data from
200
     * the webpage in the message using positional placeholders.
201
     *
202
     * @param string        $sMessage                 The message to show if the request is not sent
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter type; 8 found
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter name; 17 found
Loading history...
203
     *
204
     * @return Request
205
     */
206
    public function elseShow($sMessage)
207
    {
208
        $this->setMessageArgs(func_get_args());
209
        return $this;
210
    }
211
212
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $xParameter should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $aVariables should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $sVars should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $nVarId should have a doc-comment as per coding-style.
Loading history...
213
     * Make unique js vars for parameters of type DomElement
214
     *
215
     * @var ParameterContract   $xParameter
216
     * @var array               &$aVariables
217
     * @var string              &$sVars
218
     * @var integer             &$nVarId
219
     *
220
     * @return ParameterContract
221
     */
222
    private function _makeUniqueJsVar(ParameterContract $xParameter, array &$aVariables, &$sVars, &$nVarId)
223
    {
224
        if($xParameter instanceof DomElement)
225
        {
226
            $sParameterStr = $xParameter->getScript();
227
            if(!array_key_exists($sParameterStr, $aVariables))
228
            {
229
                // The value is not yet defined. A new variable is created.
230
                $sVarName = "jxnVar$nVarId";
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 19 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
231
                $aVariables[$sParameterStr] = $sVarName;
232
                $sVars .= "$sVarName=$xParameter;";
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 21 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
233
                $nVarId++;
234
            }
235
            else
236
            {
237
                // The value is already defined. The corresponding variable is assigned.
238
                $sVarName = $aVariables[$sParameterStr];
239
            }
240
            $xParameter = new Parameter(Parameter::JS_VALUE, $sVarName);
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $xParameter. This often makes code more readable.
Loading history...
241
        }
242
        return $xParameter;
243
    }
244
245
    /**
246
     * Returns a string representation of the script output (javascript) from this request object
247
     *
248
     * @return string
249
     */
250
    public function getScript()
251
    {
252
        /*
253
         * JQuery variables sometimes depend on the context where they are used, eg. when their value depends on $(this).
254
         * When a confirmation question is added, the Jaxon calls are made in a different context,
255
         * making those variables invalid.
256
         * To avoid issues related to these context changes, the JQuery selectors values are first saved into
257
         * local variables, which are then used in Jaxon function calls.
258
         */
0 ignored issues
show
Coding Style introduced by
Empty line required after block comment
Loading history...
259
        $sVars = ''; // Javascript code defining all the variables values.
1 ignored issue
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
260
        $nVarId = 1; // Position of the variables, starting from 1.
261
        // This array will avoid declaring multiple variables with the same value.
262
        // The array key is the variable value, while the array value is the variable name.
263
        $aVariables = []; // Array of local variables.
264
        foreach($this->aParameters as &$xParameter)
265
        {
266
            $xParameter = $this->_makeUniqueJsVar($xParameter, $aVariables, $sVars, $nVarId);
267
        }
268
269
        $sPhrase = '';
270
        if(count($this->aMessageArgs) > 0)
271
        {
272
            $sPhrase = array_shift($this->aMessageArgs); // The first array entry is the question.
273
            // $sPhrase = "'" . addslashes($sPhrase) . "'"; // Wrap the phrase with single quotes
274
            if(count($this->aMessageArgs) > 0)
275
            {
276
                $nParamId = 1;
277
                foreach($this->aMessageArgs as &$xParameter)
278
                {
279
                    $xParameter = $this->_makeUniqueJsVar($xParameter, $aVariables, $sVars, $nVarId);
280
                    $xParameter = "'$nParamId':" . $xParameter->getScript();
281
                    $nParamId++;
282
                }
283
                $sPhrase .= '.supplant({' . implode(',', $this->aMessageArgs) . '})';
284
            }
285
        }
286
287
        $sScript = parent::getScript();
288
        $xDialog = jaxon()->dialog();
289
        if($this->sCondition == '__confirm__')
290
        {
291
            $sScript = $xDialog->confirm($sPhrase, $sScript, '');
292
        }
293
        elseif($this->sCondition !== null)
294
        {
295
            $sScript = 'if(' . $this->sCondition . '){' . $sScript . ';}';
296
            if(($sPhrase))
297
            {
298
                $xDialog->getAlert()->setReturn(true);
299
                $sScript .= 'else{' . $xDialog->warning($sPhrase) . ';}';
300
            }
301
        }
302
        return $sVars . $sScript;
303
    }
304
305
    /**
306
     * Prints a string representation of the script output (javascript) from this request object
307
     *
308
     * @return void
309
     */
310
    public function printScript()
311
    {
312
        print $this->getScript();
313
    }
314
315
    /**
316
     * Check if the request has a parameter of type Parameter::PAGE_NUMBER
317
     *
318
     * @return boolean
319
     */
320
    public function hasPageNumber()
321
    {
322
        foreach($this->aParameters as $xParameter)
323
        {
324
            if($xParameter->getType() == Parameter::PAGE_NUMBER)
325
            {
326
                return true;
327
            }
328
        }
329
        return false;
330
    }
331
332
    /**
333
     * Set a value to the Parameter::PAGE_NUMBER parameter
334
     *
335
     * @param integer        $nPageNumber        The current page number
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter type; 8 found
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter name; 8 found
Loading history...
336
     *
337
     * @return Request
338
     */
339
    public function setPageNumber($nPageNumber)
340
    {
341
        // Set the value of the Parameter::PAGE_NUMBER parameter
342
        foreach($this->aParameters as $xParameter)
343
        {
344
            if($xParameter->getType() == Parameter::PAGE_NUMBER)
345
            {
346
                $xParameter->setValue(intval($nPageNumber));
347
                break;
348
            }
349
        }
350
        return $this;
351
    }
352
353
    /**
354
     * Make the pagination links for this request
355
     *
356
     * @param integer       $nCurrentPage           The current page
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter type; 7 found
Loading history...
Coding Style introduced by
Expected 2 spaces after parameter name; 11 found
Loading history...
357
     * @param integer       $nItemsPerPage          The number of items per page page
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter type; 7 found
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter name; 10 found
Loading history...
358
     * @param integer       $nItemsTotal            The total number of items
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter type; 7 found
Loading history...
Coding Style introduced by
Expected 3 spaces after parameter name; 12 found
Loading history...
359
     *
360
     * @return \Jaxon\Utils\Pagination\Paginator
361
     */
362
    public function pg($nCurrentPage, $nItemsPerPage, $nItemsTotal)
363
    {
364
        return jaxon()->di()->getPaginator()
365
            ->setup($nItemsTotal, $nItemsPerPage, $nCurrentPage, $this);
0 ignored issues
show
Coding Style introduced by
Space found before object operator
Loading history...
366
    }
367
368
    /**
369
     * Make the pagination links for this request
370
     *
371
     * @param integer       $nCurrentPage           The current page
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter type; 7 found
Loading history...
Coding Style introduced by
Expected 2 spaces after parameter name; 11 found
Loading history...
372
     * @param integer       $nItemsPerPage          The number of items per page page
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter type; 7 found
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter name; 10 found
Loading history...
373
     * @param integer       $nItemsTotal            The total number of items
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter type; 7 found
Loading history...
Coding Style introduced by
Expected 3 spaces after parameter name; 12 found
Loading history...
374
     *
375
     * @return \Jaxon\Utils\Pagination\Paginator
376
     */
377
    public function paginate($nCurrentPage, $nItemsPerPage, $nItemsTotal)
378
    {
379
        return $this->pg($nCurrentPage, $nItemsPerPage, $nItemsTotal);
380
    }
381
}
382