Passed
Push — master ( 98205f...6f7a23 )
by Thierry
02:22
created

Call::ifge()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 4
rs 10
1
<?php
2
3
/**
4
 * Call.php - The Jaxon Call
5
 *
6
 * This class is used to create js ajax 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\Call;
22
23
use Jaxon\Plugin\Response\JQuery\DomSelector;
24
use Jaxon\Ui\Dialog\Library\DialogLibraryManager;
25
26
use function array_shift;
27
use function implode;
28
29
class Call extends JsCall
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class Call
Loading history...
30
{
31
    use Traits\CallConditionTrait;
32
    use Traits\CallMessageTrait;
33
34
    /**
35
     * @var DialogLibraryManager
36
     */
37
    protected $xDialogLibraryManager;
38
39
    /**
40
     * @var Paginator
41
     */
42
    protected $xPaginator;
43
44
    /**
45
     * @var array
46
     */
47
    private $aVariables;
48
49
    /**
50
     * @var string
51
     */
52
    private $sVars;
53
54
    /**
55
     * @var int
0 ignored issues
show
Bug introduced by
Expected "integer" but found "int" for @var tag in member variable comment
Loading history...
56
     */
57
    private $nVarId;
58
59
    /**
60
     * The constructor.
61
     *
62
     * @param string $sName    The javascript function or method name
0 ignored issues
show
Coding Style introduced by
Expected 15 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Expected 17 spaces after parameter name; 4 found
Loading history...
63
     * @param DialogLibraryManager $xDialogLibraryManager
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
64
     * @param Paginator $xPaginator
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 12 spaces after parameter type; 1 found
Loading history...
65
     */
66
    public function __construct(string $sName, DialogLibraryManager $xDialogLibraryManager, Paginator $xPaginator)
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 1 found
Loading history...
67
    {
68
        parent::__construct($sName);
69
        $this->xDialogLibraryManager = $xDialogLibraryManager;
70
        $this->xPaginator = $xPaginator;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 12 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...
71
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
72
73
    /**
74
     * Make unique js vars for parameters of type DomSelector
75
     *
76
     * @param ParameterInterface $xParameter
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
77
     *
78
     * @return ParameterInterface
79
     */
80
    private function _makeUniqueJsVar(ParameterInterface $xParameter): ParameterInterface
81
    {
82
        if($xParameter instanceof DomSelector)
83
        {
84
            $sParameterStr = $xParameter->getScript();
85
            if(!isset($this->aVariables[$sParameterStr]))
86
            {
87
                // The value is not yet defined. A new variable is created.
88
                $sVarName = 'jxnVar' . $this->nVarId;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 25 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...
89
                $this->aVariables[$sParameterStr] = $sVarName;
90
                $this->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...
91
                $this->nVarId++;
92
            }
93
            else
94
            {
95
                // The value is already defined. The corresponding variable is assigned.
96
                $sVarName = $this->aVariables[$sParameterStr];
97
            }
98
            $xParameter = new Parameter(Parameter::JS_VALUE, $sVarName);
99
        }
100
        return $xParameter;
101
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
102
103
    /**
104
     * Make a phrase to be displayed in js code
105
     *
106
     * @param array $aArgs
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
107
     *
108
     * @return string
109
     */
110
    private function makePhrase(array $aArgs): string
111
    {
112
        if(empty($aArgs))
113
        {
114
            return '';
115
        }
116
        // The first array entry is the message.
117
        $sPhrase = array_shift($aArgs);
118
        if(empty($aArgs))
119
        {
120
            return $sPhrase;
121
        }
122
        $nParamId = 1;
123
        foreach($aArgs as &$xParameter)
124
        {
125
            $xParameter = $this->_makeUniqueJsVar($xParameter);
126
            $xParameter = "'$nParamId':" . $xParameter->getScript();
127
            $nParamId++;
128
        }
129
        $sPhrase .= '.supplant({' . implode(',', $aArgs) . '})';
130
        return $sPhrase;
131
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
132
133
    /**
134
     * Make a phrase to be displayed in js code
135
     *
136
     * @return string
137
     */
138
    private function makeMessage(): string
139
    {
140
        if(!($sPhrase = $this->makePhrase($this->aMessageArgs)))
0 ignored issues
show
Coding Style introduced by
Variable assignment found within a condition. Did you mean to do a comparison ?
Loading history...
141
        {
142
            return '';
143
        }
144
        $sMethod = $this->sMessageType;
145
        $xLibrary = $this->xDialogLibraryManager->getMessageLibrary();
146
        $xLibrary->setReturnCode(true);
147
        return $xLibrary->$sMethod($sPhrase);
148
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
149
150
    /**
151
     * Returns a string representation of the script output (javascript) from this request object
152
     *
153
     * @return string
154
     */
155
    public function getScript(): string
156
    {
157
        /*
158
         * JQuery variables sometimes depend on the context where they are used, eg. when their value depends on $(this).
159
         * When a confirmation question is added, the Jaxon calls are made in a different context,
160
         * making those variables invalid.
161
         * To avoid issues related to these context changes, the JQuery selectors values are first saved into
162
         * local variables, which are then used in Jaxon function calls.
163
         */
0 ignored issues
show
Coding Style introduced by
Empty line required after block comment
Loading history...
164
        $this->sVars = ''; // Javascript code defining all the variables values.
165
        $this->nVarId = 1; // Position of the variables, starting from 1.
166
        // This array will avoid declaring multiple variables with the same value.
167
        // The array key is the variable value, while the array value is the variable name.
168
        $this->aVariables = []; // Array of local variables.
169
        foreach($this->aParameters as &$xParameter)
170
        {
171
            $xParameter = $this->_makeUniqueJsVar($xParameter);
172
        }
173
174
        $sMessageScript = $this->makeMessage();
175
        $sScript = parent::getScript();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 8 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...
176
        if($this->sCondition === '__confirm__')
177
        {
178
            $sConfirmPhrase = $this->makePhrase($this->aConfirmArgs);
179
            $sScript = $this->xDialogLibraryManager->getQuestionLibrary()
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 8 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...
180
                ->confirm($sConfirmPhrase, $sScript, $sMessageScript);
0 ignored issues
show
Coding Style introduced by
Space found before object operator
Loading history...
181
        }
182
        elseif($this->sCondition !== '')
183
        {
184
            $sScript = empty($sMessageScript) ? 'if(' . $this->sCondition . '){' . $sScript . ';}' :
0 ignored issues
show
Coding Style introduced by
Expected 1 space after ":"; newline found
Loading history...
185
                'if(' . $this->sCondition . '){' . $sScript . ';}else{' . $sMessageScript . ';}';
186
        }
187
        return $this->sVars . $sScript;
188
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
189
190
    /**
191
     * Check if the request has a parameter of type Parameter::PAGE_NUMBER
192
     *
193
     * @return bool
194
     */
195
    public function hasPageNumber(): bool
196
    {
197
        foreach($this->aParameters as $xParameter)
198
        {
199
            if($xParameter->getType() === Parameter::PAGE_NUMBER)
200
            {
201
                return true;
202
            }
203
        }
204
        return false;
205
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
206
207
    /**
208
     * Set a value to the Parameter::PAGE_NUMBER parameter
209
     *
210
     * @param integer $nPageNumber    The current page number
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
211
     *
212
     * @return Call
213
     */
214
    public function setPageNumber(int $nPageNumber): Call
215
    {
216
        // Set the value of the Parameter::PAGE_NUMBER parameter
217
        foreach($this->aParameters as $xParameter)
218
        {
219
            if($xParameter->getType() === Parameter::PAGE_NUMBER)
220
            {
221
                $xParameter->setValue($nPageNumber);
222
                break;
223
            }
224
        }
225
        return $this;
226
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
227
228
    /**
229
     * Make the pagination links for this request
230
     *
231
     * @param integer $nCurrentPage    The current page
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 4 found
Loading history...
232
     * @param integer $nItemsPerPage    The number of items per page
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
233
     * @param integer $nItemsTotal    The total number of items
0 ignored issues
show
Coding Style introduced by
Expected 3 spaces after parameter name; 4 found
Loading history...
234
     *
235
     * @return array
236
     */
237
    public function pages(int $nCurrentPage, int $nItemsPerPage, int $nItemsTotal): array
238
    {
239
        // Append the page number to the parameter list, if not yet given.
240
        if(!$this->hasPageNumber())
241
        {
242
            $this->addParameter(Parameter::PAGE_NUMBER, 0);
243
        }
244
        return $this->xPaginator->setup($this, $nCurrentPage, $nItemsPerPage, $nItemsTotal)->pages();
245
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
246
247
    /**
248
     * Make the pagination links for this request
249
     *
250
     * @param integer $nCurrentPage    The current page
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 4 found
Loading history...
251
     * @param integer $nItemsPerPage    The number of items per page
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
252
     * @param integer $nItemsTotal    The total number of items
0 ignored issues
show
Coding Style introduced by
Expected 3 spaces after parameter name; 4 found
Loading history...
253
     *
254
     * @return Paginator
255
     */
256
    public function paginate(int $nCurrentPage, int $nItemsPerPage, int $nItemsTotal): Paginator
257
    {
258
        // Append the page number to the parameter list, if not yet given.
259
        if(!$this->hasPageNumber())
260
        {
261
            $this->addParameter(Parameter::PAGE_NUMBER, 0);
262
        }
263
        return $this->xPaginator->setup($this, $nCurrentPage, $nItemsPerPage, $nItemsTotal);
264
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
265
266
    /**
267
     * Make the pagination links for this request
268
     *
269
     * @param integer $nCurrentPage    The current page
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 4 found
Loading history...
270
     * @param integer $nItemsPerPage    The number of items per page
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
271
     * @param integer $nItemsTotal    The total number of items
0 ignored issues
show
Coding Style introduced by
Expected 3 spaces after parameter name; 4 found
Loading history...
272
     *
273
     * @return Paginator
274
     */
275
    public function pg(int $nCurrentPage, int $nItemsPerPage, int $nItemsTotal): Paginator
276
    {
277
        return $this->paginate($nCurrentPage, $nItemsPerPage, $nItemsTotal);
278
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 0 found
Loading history...
279
}
280