Subversion-Projekte lars-tiefland.laravel_shop

Revision

Revision 148 | Details | Vergleich mit vorheriger | 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;
621 lars 33
 
148 lars 34
            case JSON_ERROR_DEPTH:
35
                return $prefix . 'Maximum stack depth exceeded';
621 lars 36
 
148 lars 37
            case JSON_ERROR_STATE_MISMATCH:
38
                return $prefix . 'Underflow or the modes mismatch';
621 lars 39
 
148 lars 40
            case JSON_ERROR_CTRL_CHAR:
41
                return $prefix . 'Unexpected control character found';
621 lars 42
 
148 lars 43
            case JSON_ERROR_SYNTAX:
44
                return $prefix . 'Syntax error, malformed JSON';
621 lars 45
 
148 lars 46
            case JSON_ERROR_UTF8:
47
                return $prefix . 'Malformed UTF-8 characters, possibly incorrectly encoded';
48
 
49
            default:
50
                return $prefix . 'Unknown error';
51
        }
52
    }
53
 
54
    /**
55
     * Translates a given type to a human readable message prefix.
56
     */
57
    public static function translateTypeToPrefix(string $type): string
58
    {
59
        switch (strtolower($type)) {
60
            case 'expected':
61
                $prefix = 'Expected value JSON decode error - ';
62
 
63
                break;
621 lars 64
 
148 lars 65
            case 'actual':
66
                $prefix = 'Actual value JSON decode error - ';
67
 
68
                break;
69
 
70
            default:
71
                $prefix = '';
72
 
73
                break;
74
        }
75
 
76
        return $prefix;
77
    }
78
}