summaryrefslogtreecommitdiff
path: root/libs/jsonrpc/src/JsonRPC/Validator/JsonEncodingValidator.php
blob: 0bbc4abd903ef1b4d6b270dbc6e97a4e23e13850 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<?php

namespace JsonRPC\Validator;

use JsonRPC\Exception\ResponseEncodingFailureException;

/**
 * Class JsonEncodingValidator
 *
 * @package JsonRPC\Validator
 * @author  Frederic Guillot
 */
class JsonEncodingValidator
{
    public static function validate()
    {
        $jsonError = json_last_error();

        if ($jsonError !== JSON_ERROR_NONE) {
            switch ($jsonError) {
                case JSON_ERROR_DEPTH:
                    $errorMessage = 'Maximum stack depth exceeded';
                    break;
                case JSON_ERROR_STATE_MISMATCH:
                    $errorMessage = 'Underflow or the modes mismatch';
                    break;
                case JSON_ERROR_CTRL_CHAR:
                    $errorMessage = 'Unexpected control character found';
                    break;
                case JSON_ERROR_SYNTAX:
                    $errorMessage = 'Syntax error, malformed JSON';
                    break;
                case JSON_ERROR_UTF8:
                    $errorMessage = 'Malformed UTF-8 characters, possibly incorrectly encoded';
                    break;
                default:
                    $errorMessage = 'Unknown error';
                    break;
            }

            throw new ResponseEncodingFailureException($errorMessage, $jsonError);
        }
    }
}