Passed
Push — master ( d1b1aa...f4995a )
by Thierry
07:48
created

Argument::__convertStringToBool()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
nc 5
nop 1
dl 0
loc 20
rs 9.2888
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Manager.php - Jaxon Request Manager
5
 *
6
 * This class processes the input arguments from the GET or POST data of the request.
7
 * If this is a request for the initial page load, no arguments will be processed.
8
 * During a jaxon request, any arguments found in the GET or POST will be converted to a PHP array.
9
 *
10
 * @package jaxon-core
0 ignored issues
show
Coding Style introduced by
Package name "jaxon-core" is not valid; consider "Jaxoncore" instead
Loading history...
11
 * @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...
12
 * @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...
13
 * @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...
14
 * @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...
15
 * @author Thierry Feuzeu <[email protected]>
16
 * @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...
17
 * @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...
18
 * @copyright 2016 Thierry Feuzeu <[email protected]>
19
 * @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License
20
 * @link https://github.com/jaxon-php/jaxon-core
21
 */
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...
22
23
namespace Jaxon\Request\Handler;
24
25
use Jaxon\Exception\Error;
26
27
class Argument
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class Argument
Loading history...
28
{
29
    use \Jaxon\Features\Config;
30
    use \Jaxon\Features\Translator;
31
32
    /*
33
     * Request methods
34
     */
0 ignored issues
show
Coding Style introduced by
Empty line required after block comment
Loading history...
35
    const METHOD_UNKNOWN = 0;
36
    const METHOD_GET = 1;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 5 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...
37
    const METHOD_POST = 2;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 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...
38
39
    /**
40
     * An array of arguments received via the GET or POST parameter jxnargs.
41
     *
42
     * @var array
43
     */
44
    private $aArgs;
45
46
    /**
47
     * Stores the method that was used to send the arguments from the client.
48
     * Will be one of: self::METHOD_UNKNOWN, self::METHOD_GET, self::METHOD_POST.
49
     *
50
     * @var integer
51
     */
52
    private $nMethod;
53
54
    /**
55
     * The function which decodes utf8 string.
56
     *
57
     * @var callable
58
     */
59
    private $cUtf8Decoder;
60
61
    /**
62
     * The constructor
63
     *
64
     * Get and decode the arguments of the HTTP request
65
     */
66
    public function __construct()
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 1 found
Loading history...
67
    {
68
        $this->aArgs = [];
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 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...
69
        $this->nMethod = self::METHOD_UNKNOWN;
70
71
        if(isset($_POST['jxnargs']))
72
        {
73
            $this->nMethod = self::METHOD_POST;
74
            $this->aArgs = $_POST['jxnargs'];
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 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...
75
        }
76
        elseif(isset($_GET['jxnargs']))
77
        {
78
            $this->nMethod = self::METHOD_GET;
79
            $this->aArgs = $_GET['jxnargs'];
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 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...
80
        }
81
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
82
83
    /**
84
     * Return the method that was used to send the arguments from the client
85
     *
86
     * The method is one of: self::METHOD_UNKNOWN, self::METHOD_GET, self::METHOD_POST.
87
     *
88
     * @return integer
89
     */
90
    public function getRequestMethod()
91
    {
92
        return $this->nMethod;
93
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
94
95
    /**
96
     * Converts a string to a boolean var
97
     *
98
     * @param string        $sValue                The string to be converted
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...
99
     *
100
     * @return boolean
101
     */
102
    private function __convertStringToBool($sValue)
0 ignored issues
show
Coding Style introduced by
Method name "Argument::__convertStringToBool" is invalid; only PHP magic methods should be prefixed with a double underscore
Loading history...
103
    {
104
        if(\strcasecmp($sValue, 'true') == 0)
105
        {
106
            return true;
107
        }
108
        if(\strcasecmp($sValue, 'false') == 0)
109
        {
110
            return false;
111
        }
112
        if(\is_numeric($sValue))
113
        {
114
            if($sValue == 0)
115
            {
116
                return false;
117
            }
118
            return true;
119
        }
120
        return false;
121
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
122
123
    /**
124
     * Strip the slashes from a string
125
     *
126
     * @param string        $sArg                The string to be stripped
127
     *
128
     * @return string
129
     */
130
    // private function __argumentStripSlashes(&$sArg)
131
    // {
132
    //     if(!is_string($sArg))
133
    //     {
134
    //         return '';
135
    //     }
136
    //     $sArg = stripslashes($sArg);
137
    //     return $sArg;
138
    // }
139
140
    /**
141
     * Convert an Jaxon request argument to its value
142
     *
143
     * Depending of its first char, the Jaxon request argument is converted to a given type.
144
     *
145
     * @param string        $sValue                The keys of the options in the file
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...
146
     *
147
     * @return string|boolean|integer|double|null
148
     */
149
    private function __convertValue($sValue)
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 1 found
Loading history...
Coding Style introduced by
Method name "Argument::__convertValue" is invalid; only PHP magic methods should be prefixed with a double underscore
Loading history...
150
    {
151
        $cType = \substr($sValue, 0, 1);
152
        $sValue = \substr($sValue, 1);
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $sValue. This often makes code more readable.
Loading history...
153
        switch($cType)
154
        {
155
        case 'S':
156
            $value = ($sValue === false ? '' : $sValue);
157
            break;
158
        case 'B':
159
            $value = $this->__convertStringToBool($sValue);
160
            break;
161
        case 'N':
162
            $value = ($sValue == \floor($sValue) ? (int)$sValue : (float)$sValue);
163
            break;
164
        case '*':
165
        default:
166
            $value = null;
167
            break;
168
        }
169
        return $value;
170
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
171
172
    /**
173
     * Decode and convert an Jaxon request argument from JSON
174
     *
175
     * @param string        $sArg                The Jaxon request argument
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...
176
     *
177
     * @return string|null
178
     */
179
    private function __argumentDecode(&$sArg)
0 ignored issues
show
Coding Style introduced by
Method name "Argument::__argumentDecode" is invalid; only PHP magic methods should be prefixed with a double underscore
Loading history...
180
    {
181
        if($sArg == '')
182
        {
183
            return '';
184
        }
185
186
        // Arguments are url encoded when uploading files
187
        $sType = 'multipart/form-data';
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...
188
        $iLen = \strlen($sType);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 9 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...
189
        $sContentType = '';
190
        if(\key_exists('CONTENT_TYPE', $_SERVER))
191
        {
192
            $sContentType = \substr($_SERVER['CONTENT_TYPE'], 0, $iLen);
193
        }
194
        elseif(\key_exists('HTTP_CONTENT_TYPE', $_SERVER))
195
        {
196
            $sContentType = \substr($_SERVER['HTTP_CONTENT_TYPE'], 0, $iLen);
197
        }
198
        if($sContentType == $sType)
199
        {
200
            $sArg = \urldecode($sArg);
201
        }
202
203
        $data = \json_decode($sArg, true);
204
205
        if($data !== null && $sArg != $data)
206
        {
207
            $sArg = $data;
208
        }
209
        else
210
        {
211
            $sArg = $this->__convertValue($sArg);
212
        }
213
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
214
215
    /**
216
     * Decode an Jaxon request argument from UTF8
217
     *
218
     * @param array             $aDst           An array to store the decoded arguments
0 ignored issues
show
Coding Style introduced by
Expected 8 spaces after parameter type; 13 found
Loading history...
Coding Style introduced by
Expected 3 spaces after parameter name; 11 found
Loading history...
219
     * @param string            $sKey           The key of the argument being decoded
0 ignored issues
show
Coding Style introduced by
Expected 7 spaces after parameter type; 12 found
Loading history...
Coding Style introduced by
Expected 3 spaces after parameter name; 11 found
Loading history...
220
     * @param string|array      $mValue         The value of the argument being decoded
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter type; 6 found
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter name; 9 found
Loading history...
221
     *
222
     * @return void
223
     */
224
    private function _decode_utf8_argument(array &$aDst, $sKey, $mValue)
0 ignored issues
show
Coding Style introduced by
This method is not in camel caps format.

This check looks for method names that are not written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection seeker becomes databaseConnectionSeeker.

Loading history...
225
    {
226
        $sDestKey = $sKey;
227
        // Decode the key
228
        if(\is_string($sDestKey))
229
        {
230
            $sDestKey = \call_user_func($this->cUtf8Decoder, $sDestKey);
231
        }
232
233
        if(is_array($mValue))
234
        {
235
            $aDst[$sDestKey] = [];
236
            foreach($mValue as $_sKey => &$_mValue)
237
            {
238
                $this->_decode_utf8_argument($aDst[$sDestKey], $_sKey, $_mValue);
239
            }
240
        }
241
        elseif(\is_numeric($mValue) || \is_bool($mValue))
242
        {
243
            $aDst[$sDestKey] = $mValue;
244
        }
245
        elseif(\is_string($mValue))
246
        {
247
            $aDst[$sDestKey] = \call_user_func($this->cUtf8Decoder, $mValue);
248
        }
249
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
250
251
    /**
252
     * Return the array of arguments that were extracted and parsed from the GET or POST data
253
     *
254
     * @return array
255
     */
256
    public function process()
257
    {
258
        // if(get_magic_quotes_gpc() == 1)
259
        // {
260
        //     \array_walk($this->aArgs, [$this, '__argumentStripSlashes']);
261
        // }
262
        \array_walk($this->aArgs, [$this, '__argumentDecode']);
263
264
        if(($this->getOption('core.decode_utf8')))
265
        {
266
            // By default, no decoding
267
            $this->cUtf8Decoder = function($sStr) {
268
                return $sStr;
269
            };
270
            if(\function_exists('iconv'))
271
            {
272
                $this->cUtf8Decoder = function($sStr) {
273
                    return \iconv("UTF-8", $this->getOption('core.encoding') . '//TRANSLIT', $sStr);
274
                };
275
            }
276
            elseif(\function_exists('mb_convert_encoding'))
277
            {
278
                $this->cUtf8Decoder = function($sStr) {
279
                    return \mb_convert_encoding($sStr, $this->getOption('core.encoding'), "UTF-8");
280
                };
281
            }
282
            elseif($this->getOption('core.encoding') == "ISO-8859-1")
283
            {
284
                $this->cUtf8Decoder = function($sStr) {
285
                    return \utf8_decode($sStr);
286
                };
287
            }
288
            else
289
            {
290
                throw new Error($this->trans('errors.request.conversion'));
291
            }
292
293
            $aDst = [];
294
            foreach($this->aArgs as $sKey => &$mValue)
295
            {
296
                $this->_decode_utf8_argument($aDst, $sKey, $mValue);
297
            };
298
            $this->aArgs = $aDst;
299
300
            $this->setOption('core.decode_utf8', false);
301
        }
302
303
        return $this->aArgs;
304
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 0 found
Loading history...
305
}
306