Completed
Pull Request — master (#14)
by
unknown
06:16
created

JsonDecoder   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 90%

Importance

Changes 0
Metric Value
dl 0
loc 45
ccs 9
cts 10
cp 0.9
rs 10
c 0
b 0
f 0
wmc 3
lcom 1
cbo 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A decode() 0 10 2
1
<?php
2
3
namespace Activerules\JsonReference\JsonDecoder;
4
5
use Activerules\JsonReference\JsonDecoderInterface;
6
use Activerules\JsonReference\JsonDecodingException;
7
8
final class JsonDecoder implements JsonDecoderInterface
9
{
10
    /**
11
     * @var bool
12
     */
13
    private $assoc;
14
15
    /**
16
     * @var int
17
     */
18
    private $depth;
19
20
    /**
21
     * @var int
22
     */
23
    private $options;
24
25
    /**
26
     * @param bool $assoc
27
     * @param int  $depth
28
     * @param int  $options
29
     */
30 88
    public function __construct($assoc = false, $depth = 512, $options = JSON_BIGINT_AS_STRING)
31
    {
32 88
        $this->assoc   = $assoc;
33 88
        $this->depth   = $depth;
34 88
        $this->options = $options;
35 88
    }
36
37
    /**
38
     * @param string $json
39
     *
40
     * @return object
41
     */
42 50
    public function decode($json)
43
    {
44 50
        $data = json_decode($json, $this->assoc, $this->depth, $this->options);
45
46 50
        if (json_last_error() !== JSON_ERROR_NONE) {
47
            throw new JsonDecodingException(sprintf('Invalid JSON: %s', json_last_error_msg()));
48
        }
49
50 50
        return $data;
51
    }
52
}
53