JSONType   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Test Coverage

Coverage 76.92%

Importance

Changes 0
Metric Value
eloc 13
c 0
b 0
f 0
dl 0
loc 35
ccs 10
cts 13
cp 0.7692
rs 10
wmc 8

2 Methods

Rating   Name   Duplication   Size   Complexity  
A convertToPHP() 0 11 4
A convertToSQL() 0 13 4
1
<?php
2
3
namespace mindplay\sql\model\types;
4
5
use mindplay\sql\model\schema\Type;
6
use RuntimeException;
7
8
class JSONType implements Type
9
{
10
    /**
11
     * @var int options for json_encode()
12
     *
13
     * @see json_encode()
14
     */
15
    public $json_encode_options = JSON_UNESCAPED_UNICODE;
16
17 1
    public function convertToSQL($value)
18
    {
19 1
        if ($value === null || $value === '') {
20
            return null;
21
        }
22
23 1
        $json = json_encode($value, $this->json_encode_options);
24
25 1
        if ($json === false) {
26 1
            throw new RuntimeException(json_last_error_msg());
27
        }
28
29 1
        return $json;
30
    }
31
32 1
    public function convertToPHP($value)
33
    {
34 1
        if ($value === null || $value === '') {
35
            return null;
36
        }
37
38 1
        if (is_string($value)) {
39 1
            return json_decode($value, true);
40
        }
41
42
        throw new RuntimeException("expected JSON string, got: " . gettype($value));
43
    }
44
}
45