JsonError::throwException()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 0
1
<?php
2
3
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
4
5
namespace Json;
6
7
class JsonError
8
{
9
    private $error;
10
11
    private $exceptions = array(
12
        JSON_ERROR_DEPTH            => 'JsonDepthException',
13
        JSON_ERROR_STATE_MISMATCH   => 'JsonStateMismatchException',
14
        JSON_ERROR_CTRL_CHAR        => 'JsonControlCharacterExeption',
15
        JSON_ERROR_SYNTAX           => 'JsonSyntaxException',
16
        JSON_ERROR_UTF8             => 'JsonUtf8Exception',
17
        JSON_ERROR_RECURSION        => 'JsonRecursionException',
18
        JSON_ERROR_INF_OR_NAN       => 'JsonInfOrNanException',
19
        JSON_ERROR_UNSUPPORTED_TYPE => 'JsonUnsupportedTypeException'
20
    );
21
22
    /**
23
     * hasError
24
     *
25
     * @access public
26
     * @return boolean
27
     */
28
    public function hasError()
29
    {
30
        return $this->getError() !== JSON_ERROR_NONE;
31
    }
32
33
    /**
34
     * Gets last json error.
35
     *
36
     * @return int
37
     */
38
    public function getError()
39
    {
40
        return $this->error ?: ($this->error = json_last_error());
41
    }
42
43
    public function throwException()
44
    {
45
        $exception = array_key_exists($this->getError(), $this->exceptions)
46
            ? __NAMESPACE__ . '\\Exception\\' . $this->exceptions[$this->getError()]
47
            : __NAMESPACE__ . '\\Exception\\JsonUnknownException';
48
49
        throw new $exception();
50
    }
51
}
52