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