PgJson   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 1
dl 0
loc 98
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 2
A fromPg() 0 21 4
A toPg() 0 8 2
A toPgStandardFormat() 0 8 2
A jsonEncode() 0 16 2
1
<?php
2
/*
3
 * This file is part of Pomm's Foundation package.
4
 *
5
 * (c) 2014 - 2017 Grégoire HUBERT <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace PommProject\Foundation\Converter;
11
12
use PommProject\Foundation\Exception\ConverterException;
13
use PommProject\Foundation\Session\Session;
14
15
/**
16
 * PgJson
17
 *
18
 * Json converter.
19
 *
20
 * @package   Foundation
21
 * @copyright 2014 - 2017 Grégoire HUBERT
22
 * @author    Grégoire HUBERT
23
 * @license   X11 {@link http://opensource.org/licenses/mit-license.php}
24
 * @see       ConverterInterface
25
 */
26
class PgJson implements ConverterInterface
27
{
28
    protected $is_array;
29
30
    /**
31
     * __construct
32
     *
33
     * Configure the JSON converter to decode JSON as StdObject instances or
34
     * arrays (default).
35
     *
36
     * @param boolean $is_array
37
     */
38
    public function __construct($is_array = null)
39
    {
40
        $this->is_array = $is_array === null ? true : $is_array;
41
    }
42
43
    /**
44
     * fromPg
45
     *
46
     * @see ConverterInterface
47
     */
48
    public function fromPg($data, $type, Session $session)
49
    {
50
        if (trim($data) === '') {
51
            return null;
52
        }
53
54
        $return = json_decode($data, $this->is_array);
55
56
        if (JSON_ERROR_NONE !== json_last_error()) {
57
            throw new ConverterException(
58
                sprintf(
59
                    "Could not convert Json to PHP %s, json_last_error() returned '%d'.\n%s",
60
                    $this->is_array ? 'array' : 'object',
61
                    json_last_error(),
62
                    $data
63
                )
64
            );
65
        }
66
67
        return $return;
68
    }
69
70
    /**
71
     * toPg
72
     *
73
     * @see ConverterInterface
74
     */
75
    public function toPg($data, $type, Session $session)
76
    {
77
        return
78
            $data !== null
79
            ? sprintf("json '%s'", $this->jsonEncode($data))
80
            : sprintf("NULL::%s", $type)
81
            ;
82
    }
83
84
    /**
85
     * toPgStandardFormat
86
     *
87
     * @see ConverterInterface
88
     */
89
    public function toPgStandardFormat($data, $type, Session $session)
90
    {
91
        return
92
            $data !== null
93
            ? $this->jsonEncode($data)
94
            : null
95
            ;
96
    }
97
98
    /**
99
     * jsonEncode
100
     *
101
     * Encode data to Json. Throw an exception if an error occurs.
102
     *
103
     * @param  mixed $data
104
     * @throws  ConverterException
105
     * @return string
106
     */
107
    protected function jsonEncode($data)
108
    {
109
        $return = json_encode($data);
110
111
        if ($return === false) {
112
            throw new ConverterException(
113
                sprintf(
114
                    "Could not convert %s data to JSON. Driver returned '%s'.",
115
                    gettype($data),
116
                    json_last_error()
117
                )
118
            );
119
        }
120
121
        return $return;
122
    }
123
}
124