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