Completed
Push — master ( 24c373...a81578 )
by Patrick
03:13 queued 03:01
created

SpreadSheetSerializer::addValueByColName()   C

Complexity

Conditions 10
Paths 11

Size

Total Lines 47
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 10
eloc 25
c 2
b 0
f 0
nc 11
nop 4
dl 0
loc 47
rs 5.1578

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
namespace Serialize;
3
4
abstract class SpreadSheetSerializer extends Serializer
5
{
6
    private function getKeysFromData($array)
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
7
    {
8
        $first = reset($array);
9
        if(is_array($first))
10
        {
11
            return array_keys($first);
12
        }
13
        else if(is_object($first))
14
        {
15
            return array_keys(get_object_vars($first));
16
        }
17
        return false;
18
    }
19
20
    private function getRowArray($row)
21
    {
22
        if(is_object($row))
23
        {
24
            return get_object_vars($row);
25
        }
26
        if(!is_array($row))
27
        {
28
            return array($row);
29
        }
30
        return $row;
31
    }
32
33
    private function prependAndMergeKeys(&$keys, $prefix, $newKeys)
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
34
    {
35
        foreach($newKeys as &$key)
36
        {
37
            $key = $prefix.$key;
38
        }
39
        $keys = array_unique(array_merge($keys, $newKeys));
40
    }
41
42
    protected function addValueByColName(&$cols, &$row, $colName, $value)
43
    {
44
        if(is_object($value))
45
        {
46
            switch($colName)
47
            {
48
                case '_id':
49
                    if(isset($value->{'$id'}))
50
                    {
51
                        $this->addValueByColName($cols, $row, $colName, $value->{'$id'});
52
                        break;
53
                    }
54
                default:
55
                    $props = get_object_vars($value);
56
                    foreach($props as $key=>$newValue)
57
                    {
58
                        $this->addValueByColName($cols, $row, $colName.'.'.$key, $newValue);
59
                    }
60
            }
61
            return;
62
        }
63
        $index = array_search($colName, $cols);
64
        if($index === false)
65
        {
66
            $index = count($cols);
67
            $cols[$index] = $colName;
68
        }
69
        if(is_array($value))
70
        {
71
            if(isset($value[0]) && is_object($value[0]))
72
            {
73
                $count = count($value);
74
                for($i = 0; $i < $count; $i++)
75
                {
76
                    $this->addValueByColName($cols, $row, $colName.'['.$i.']', $value[$i]);
77
                }
78
            }
79
            else
80
            {
81
                $row[$index] = implode(',', $value);
82
            }
83
        }
84
        else
85
        {
86
            $row[$index] = $value;
87
        }
88
    }
89
90
    protected function getArray(&$array)
91
    {
92
        $res = array();
93
        if(is_object($array))
94
        {
95
            $array = array(get_object_vars($array));
96
        }
97
        $res[] = array();
98
        foreach($array as $row)
99
        {
100
            $tmp = array();
101
            $row = $this->getRowArray($row);
102
            foreach($row as $colName=>$value)
103
            {
104
                $this->addValueByColName($res[0], $tmp, $colName, $row[$colName]);
105
            }
106
            $tmp = $tmp+array_fill_keys(range(0,max(array_keys($tmp))),false);
107
            ksort($tmp);
108
            $res[] = $tmp;
109
        }
110
        return $res;
111
    }
112
}
113