Subversion-Projekte lars-tiefland.laravel_shop

Revision

Zur aktuellen 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\Framework\Constraint;
11
 
12
use const JSON_ERROR_CTRL_CHAR;
13
use const JSON_ERROR_DEPTH;
14
use const JSON_ERROR_NONE;
15
use const JSON_ERROR_STATE_MISMATCH;
16
use const JSON_ERROR_SYNTAX;
17
use const JSON_ERROR_UTF8;
18
use function strtolower;
19
 
20
/**
21
 * @no-named-arguments Parameter names are not covered by the backward compatibility promise for PHPUnit
22
 */
23
final class JsonMatchesErrorMessageProvider
24
{
25
    /**
26
     * Translates JSON error to a human readable string.
27
     */
28
    public static function determineJsonError(string $error, string $prefix = ''): ?string
29
    {
30
        switch ($error) {
31
            case JSON_ERROR_NONE:
32
                return null;
33
            case JSON_ERROR_DEPTH:
34
                return $prefix . 'Maximum stack depth exceeded';
35
            case JSON_ERROR_STATE_MISMATCH:
36
                return $prefix . 'Underflow or the modes mismatch';
37
            case JSON_ERROR_CTRL_CHAR:
38
                return $prefix . 'Unexpected control character found';
39
            case JSON_ERROR_SYNTAX:
40
                return $prefix . 'Syntax error, malformed JSON';
41
            case JSON_ERROR_UTF8:
42
                return $prefix . 'Malformed UTF-8 characters, possibly incorrectly encoded';
43
 
44
            default:
45
                return $prefix . 'Unknown error';
46
        }
47
    }
48
 
49
    /**
50
     * Translates a given type to a human readable message prefix.
51
     */
52
    public static function translateTypeToPrefix(string $type): string
53
    {
54
        switch (strtolower($type)) {
55
            case 'expected':
56
                $prefix = 'Expected value JSON decode error - ';
57
 
58
                break;
59
            case 'actual':
60
                $prefix = 'Actual value JSON decode error - ';
61
 
62
                break;
63
 
64
            default:
65
                $prefix = '';
66
 
67
                break;
68
        }
69
 
70
        return $prefix;
71
    }
72
}