Passed
Push — master ( b54d6e...32f105 )
by Andreas
34:18
created

midcom_helper_exporter::object2array()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4.016

Importance

Changes 0
Metric Value
cc 4
eloc 10
nc 4
nop 1
dl 0
loc 17
ccs 9
cts 10
cp 0.9
crap 4.016
rs 9.9332
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
 * Exporter baseclass
11
 *
12
 * @package midcom.helper
13
 */
14
abstract class midcom_helper_exporter
15
{
16
    abstract public function array2data(array $array) : string;
17
18
    abstract public function data2array(string $data) : array;
19
20
    /**
21
     * Take an object and return an array of useful fields (removing private properties)
22
     *
23
     * @param object $object
24
     */
25 78
    public function object2array(object $object) : array
26
    {
27 78
        $out = [];
28 78
        $fields = midcom_helper_reflector::get_object_fieldnames($object);
29
30 78
        foreach ($fields as $key) {
31 78
            if ($key[0] == '_') {
32
                // Remove private fields
33
                continue;
34
            }
35 78
            if (is_object($object->{$key})) {
36 78
                $out[$key] = $this->object2array($object->{$key});
37
            } else {
38 78
                $out[$key] = $object->{$key};
39
            }
40
        }
41 78
        return $out;
42
    }
43
44
    /**
45
     * Take data from array and move it into an object
46
     *
47
     * @param array $data
48
     * @param midcom_core_dbaobject $object The object in question
49
     * @return object the updated object (not saved)
50
     */
51 1
    public function array2object(array $data, midcom_core_dbaobject $object) : midcom_core_dbaobject
52
    {
53
        // set the object's values to the ones from the data
54 1
        $fields = midcom_helper_reflector::get_object_fieldnames($object);
55 1
        foreach ($fields as $field_name) {
56
            // skip private fields.
57 1
            if ($field_name[0] == '_') {
58
                continue;
59
            }
60
61
            // skip read_only fields
62 1
            if (in_array($field_name, ['guid', 'id'])) {
63 1
                continue;
64
            }
65
66
            // TODO: decide what to do with object metadata
67 1
            if ($field_name == 'metadata') {
68 1
                continue;
69
            }
70
71 1
            if (isset($data[$field_name])) {
72 1
                $object->{$field_name} = $data[$field_name];
73 1
                continue;
74
            }
75
76
            // unset any other value that was there before.
77
            $object->{$field_name} = null;
78
        }
79 1
        return $object;
80
    }
81
82 1
    public function data2object(array $data, midcom_core_dbaobject $object) : midcom_core_dbaobject
83
    {
84 1
        return $this->array2object($data, $object);
85
    }
86
87
    /**
88
     * Get the correct classname
89
     *
90
     * @param object $object the object
91
     */
92 76
    protected function _get_classname(object $object) : string
93
    {
94 76
        if (!empty($object->__mgdschema_class_name__)) {
95 76
            return $object->__mgdschema_class_name__;
96
        }
97 76
        return get_class($object);
98
    }
99
}
100