Passed
Push — main ( 51ac7d...b8c594 )
by
unknown
16:34
created

Classes/System/RestApi/RestApiJsonFormat.php (2 issues)

1
<?php
2
namespace Aoe\Restler\System\RestApi;
3
4
/***************************************************************
5
 *  Copyright notice
6
 *
7
 *  (c) 2015 AOE GmbH <[email protected]>
8
 *
9
 *  All rights reserved
10
 *
11
 *  This script is part of the TYPO3 project. The TYPO3 project is
12
 *  free software; you can redistribute it and/or modify
13
 *  it under the terms of the GNU General Public License as published by
14
 *  the Free Software Foundation; either version 3 of the License, or
15
 *  (at your option) any later version.
16
 *
17
 *  The GNU General Public License can be found at
18
 *  http://www.gnu.org/copyleft/gpl.html.
19
 *
20
 *  This script is distributed in the hope that it will be useful,
21
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
22
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23
 *  GNU General Public License for more details.
24
 *
25
 *  This copyright notice MUST APPEAR in all copies of the script!
26
 ***************************************************************/
27
28
use Luracast\Restler\RestException;
29
use Luracast\Restler\Format\JsonFormat;
30
use TYPO3\CMS\Core\SingletonInterface;
31
use stdClass;
32
33
/**
34
 * @package Restler
35
 */
36
class RestApiJsonFormat extends JsonFormat implements SingletonInterface
37
{
38
    /**
39
     * @param string $data
40
     * @return stdClass
41
     * @throws RestException
42
     */
43
    public function decode($data)
44
    {
45
        $options = 0;
46
        if (self::$bigIntAsString) {
47
            if ((PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION >= 4) // PHP >= 5.4
48
                || PHP_MAJOR_VERSION > 5 // PHP >= 6.0
49
            ) {
50
                $options |= JSON_BIGINT_AS_STRING;
51
            } else {
52
                $data = preg_replace(
53
                    '/:\s*(\-?\d+(\.\d+)?([e|E][\-|\+]\d+)?)/',
54
                    ': "$1"',
55
                    $data
56
                );
57
            }
58
        }
59
60
        try {
61
            $decoded = json_decode($data, $options);
0 ignored issues
show
$options of type integer is incompatible with the type boolean|null expected by parameter $associative of json_decode(). ( Ignorable by Annotation )

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

61
            $decoded = json_decode($data, /** @scrutinizer ignore-type */ $options);
Loading history...
62
            $this->handleJsonError();
63
        } catch (\RuntimeException $e) {
64
            throw new RestException(400, $e->getMessage());
65
        }
66
67
        if (strlen($data) && $decoded === null || $decoded === $data) {
0 ignored issues
show
Consider adding parentheses for clarity. Current Interpretation: (strlen($data) && $decod...) || $decoded === $data, Probably Intended Meaning: strlen($data) && ($decod... || $decoded === $data)
Loading history...
68
            throw new RestException(400, 'Error parsing JSON');
69
        }
70
71
        return $decoded;
72
    }
73
}
74