Completed
Push — master ( da2115...dd0feb )
by Andreas
18:06
created

midcom_helper_exporter::array2object()   A

Complexity

Conditions 6
Paths 6

Size

Total Lines 29
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 6.105

Importance

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