JsonEncoder::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 2
1
<?php
2
3
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
4
5
namespace Json;
6
7
use InvalidArgumentException;
8
9
class JsonEncoder
10
{
11
    protected $depth = 512;
12
13
    protected $options = 0;
14
15
    protected $error;
16
17
    public function __construct($options = 0, $depth = 512)
18
    {
19
        $this->depth = $depth;
20
        $this->options = $options;
21
22
        $this->error = new JsonError();
23
    }
24
25
    /**
26
     * Returns the JSON representation of a value.
27
     *
28
     * @link http://php.net/manual/en/function.json-encode.php
29
     *
30
     * @param mixed $value The value being encoded.
31
     *                     Can be any type except a resource.
32
     * @return string
33
     */
34
    public function encode($value)
35
    {
36
        if (is_resource($value)) {
37
            throw new InvalidArgumentException('Value must not be a resource.', 500);
38
        }
39
40
        $encoded = json_encode($value, $this->options, $this->depth);
41
42
        if (false === $this->error->hasError()) {
43
            return $encoded;
44
        }
45
46
        $this->error->throwException();
47
    }
48
}
49