Passed
Push — master ( a25a71...153b53 )
by Andreas
10:54
created

midcom_helper_exporter::object2array()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 5.0144

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 13
nc 5
nop 1
dl 0
loc 21
ccs 11
cts 12
cp 0.9167
crap 5.0144
rs 9.5222
c 1
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 236
    public function object2array(object $object) : array
24
    {
25 236
        $out = [];
26 236
        $fields = midcom_helper_reflector::get_object_fieldnames($object);
27
28 236
        foreach ($fields as $key) {
29 236
            if ($key[0] == '_') {
30
                // Remove private fields
31
                continue;
32
            }
33 236
            if (is_object($object->{$key})) {
34 236
                if ($object->{$key} instanceof DateTime) {
35 24
                    $out[$key] = $object->{$key}->format('c');
36
                } else {
37 236
                    $out[$key] = $this->object2array($object->{$key});
38
                }
39
            } else {
40 236
                $out[$key] = $object->{$key};
41
            }
42
        }
43 236
        return $out;
44
    }
45
46
    /**
47
     * Take data from array and move it into an object
48
     *
49
     * @return midcom_core_dbaobject 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 234
    protected function _get_classname(object $object) : string
91
    {
92 234
        if (property_exists($object, '__mgdschema_class_name__')) {
93 234
            return $object->__mgdschema_class_name__;
94
        }
95 234
        return get_class($object);
96
    }
97
}
98