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

midcom_helper_exporter_json::array2data()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 3
nop 1
dl 0
loc 8
ccs 5
cts 5
cp 1
crap 3
rs 10
c 0
b 0
f 0
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