JSONEncoder::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 0
crap 2
1
<?php
2
3
namespace CouchDB\Encoder;
4
5
use CouchDB\Exception\JsonDecodeException;
6
use CouchDB\Exception\JsonEncodeException;
7
8
/**
9
 * @author Markus Bachmann <[email protected]>
10
 */
11
final class JSONEncoder
12
{
13
    private function __construct()
14
    {
15
    }
16
17 9 View Code Duplication
    public static function encode($value)
18
    {
19
        set_error_handler(function () use ($value) {
20
            throw new JsonEncodeException($value);
21 9
        });
22
23 9
        $json = json_encode($value);
24
25 9
        if ('null' === $json && $value !== null) {
26
            throw new JsonEncodeException($value);
27
        }
28
29 9
        restore_error_handler();
30
31 9
        return $json;
32
    }
33
34 View Code Duplication
    public static function decode($json)
35
    {
36 16
        set_error_handler(function () use ($json) {
37
            throw new JsonDecodeException($json);
38 16
        });
39
40 16
        $value = json_decode($json, true);
41
42 16
        if (false === $value) {
43
            throw new JsonDecodeException($json);
44
        }
45
46 16
        restore_error_handler();
47
48 16
        return $value;
49
    }
50
}
51