Completed
Push — master ( 01c28b...0d4f0a )
by Andreas
17:26
created

midcom_helper_exporter_json   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 39
ccs 10
cts 10
cp 1
rs 10
c 0
b 0
f 0
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A array2data() 0 8 3
A data2array() 0 3 1
A object2data() 0 4 1
1
<?php
2
/**
3
 * @author tarjei huse
4
 * @package midcom.helper
5
 * @copyright The Midgard Project, http://www.midgard-project.org
6
 * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
7
 */
8
9
/**
10
 * Move midgard objects to and from JSON
11
 *
12
 * @package midcom.helper
13
 */
14
class midcom_helper_exporter_json extends midcom_helper_exporter
15
{
16
    /**
17
     * Make JSON out of an array.
18
     *
19
     * @param array $array
20
     * @return string
21
     */
22 1
    public function array2data(array $array) : string
23
    {
24 1
        foreach ($array as $key => $val) {
25 1
            if (is_object($array[$key])) {
26 1
                $array[$key] = $this->object2array($val);
27
            }
28
        }
29 1
        return json_encode($array);
30
    }
31
32
    /**
33
     * Make an array out of some JSON
34
     *
35
     * @param string $data
36
     * @return array with attribute => key values
37
     */
38 1
    public function data2array(string $data) : array
39
    {
40 1
        return json_decode($data, true);
41
    }
42
43
    /**
44
     * Make JSON out of an object
45
     *
46
     * @param midcom_core_dbaobject $object
47
     * @return string
48
     */
49 1
    public function object2data($object) : string
50
    {
51 1
        $arr = $this->object2array($object);
52 1
        return json_encode($arr);
53
    }
54
}
55