Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php declare(strict_types=1);
2
/*
3
 * This file is part of PHPUnit.
4
 *
5
 * (c) Sebastian Bergmann <sebastian@phpunit.de>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace PHPUnit\Util;
11
 
12
use const JSON_PRETTY_PRINT;
13
use const JSON_UNESCAPED_SLASHES;
14
use const JSON_UNESCAPED_UNICODE;
15
use function count;
16
use function is_array;
17
use function is_object;
18
use function json_decode;
19
use function json_encode;
20
use function json_last_error;
21
use function ksort;
22
use PHPUnit\Framework\Exception;
23
 
24
/**
25
 * @internal This class is not covered by the backward compatibility promise for PHPUnit
26
 */
27
final class Json
28
{
29
    /**
30
     * Prettify json string.
31
     *
32
     * @throws \PHPUnit\Framework\Exception
33
     */
34
    public static function prettify(string $json): string
35
    {
36
        $decodedJson = json_decode($json, false);
37
 
38
        if (json_last_error()) {
39
            throw new Exception(
40
                'Cannot prettify invalid json'
41
            );
42
        }
43
 
44
        return json_encode($decodedJson, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
45
    }
46
 
47
    /**
48
     * To allow comparison of JSON strings, first process them into a consistent
49
     * format so that they can be compared as strings.
50
     *
51
     * @return array ($error, $canonicalized_json)  The $error parameter is used
52
     *               to indicate an error decoding the json. This is used to avoid ambiguity
53
     *               with JSON strings consisting entirely of 'null' or 'false'.
54
     */
55
    public static function canonicalize(string $json): array
56
    {
57
        $decodedJson = json_decode($json);
58
 
59
        if (json_last_error()) {
60
            return [true, null];
61
        }
62
 
63
        self::recursiveSort($decodedJson);
64
 
65
        $reencodedJson = json_encode($decodedJson);
66
 
67
        return [false, $reencodedJson];
68
    }
69
 
70
    /**
71
     * JSON object keys are unordered while PHP array keys are ordered.
72
     *
73
     * Sort all array keys to ensure both the expected and actual values have
74
     * their keys in the same order.
75
     */
76
    private static function recursiveSort(&$json): void
77
    {
78
        if (!is_array($json)) {
79
            // If the object is not empty, change it to an associative array
80
            // so we can sort the keys (and we will still re-encode it
81
            // correctly, since PHP encodes associative arrays as JSON objects.)
82
            // But EMPTY objects MUST remain empty objects. (Otherwise we will
83
            // re-encode it as a JSON array rather than a JSON object.)
84
            // See #2919.
85
            if (is_object($json) && count((array) $json) > 0) {
86
                $json = (array) $json;
87
            } else {
88
                return;
89
            }
90
        }
91
 
92
        ksort($json);
93
 
94
        foreach ($json as $key => &$value) {
95
            self::recursiveSort($value);
96
        }
97
    }
98
}