Completed
Push — master ( f22a46...046041 )
by Patrick
02:27 queued 12s
created

SpreadSheetSerializer   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 91
rs 10
c 0
b 0
f 0
wmc 18
lcom 0
cbo 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getRowArray() 0 12 3
C addValueByColName() 0 52 11
A getArray() 0 22 4
1
<?php
2
namespace Flipside\Serialize;
3
4
abstract class SpreadSheetSerializer extends Serializer
5
{
6
    private function getRowArray($row)
7
    {
8
        if(is_object($row))
9
        {
10
            return get_object_vars($row);
11
        }
12
        if(!is_array($row))
13
        {
14
            return array($row);
15
        }
16
        return $row;
17
    }
18
19
    protected function addValueByColName(&$cols, &$row, $colName, $value)
20
    {
21
        if(is_object($value))
22
        {
23
            switch($colName)
24
            {
25
                case '_id':
26
                    if(isset($value->{'$id'}))
27
                    {
28
                        $this->addValueByColName($cols, $row, $colName, $value->{'$id'});
29
                        break;
30
                    }
31
                    else if(is_a($value, 'MongoDB\BSON\ObjectId'))
32
                    {
33
                        $this->addValueByColName($cols, $row, $colName, (string)$value);
34
                        break;
35
                    }
36
                default:
37
                    $props = get_object_vars($value);
38
                    foreach($props as $key=>$newValue)
39
                    {
40
                        $this->addValueByColName($cols, $row, $colName.'.'.$key, $newValue);
41
                    }
42
            }
43
            return;
44
        }
45
        $index = array_search($colName, $cols);
46
        if($index === false)
47
        {
48
            $index = count($cols);
49
            $cols[$index] = $colName;
50
        }
51
        if(is_array($value))
52
        {
53
            if(isset($value[0]) && is_object($value[0]))
54
            {
55
                $count = count($value);
56
                for($i = 0; $i < $count; $i++)
57
                {
58
                    $this->addValueByColName($cols, $row, $colName.'['.$i.']', $value[$i]);
59
                }
60
            }
61
            else
62
            {
63
                $row[$index] = implode(',', $value);
64
            }
65
        }
66
        else
67
        {
68
            $row[$index] = $value;
69
        }
70
    }
71
72
    protected function getArray(&$array)
73
    {
74
        $res = array();
75
        if(is_object($array))
76
        {
77
            $array = array(get_object_vars($array));
78
        }
79
        $res[] = array();
80
        foreach($array as $row)
81
        {
82
            $tmp = array();
83
            $row = $this->getRowArray($row);
84
            foreach($row as $colName=>$value)
85
            {
86
                $this->addValueByColName($res[0], $tmp, $colName, $row[$colName]);
87
            }
88
            $tmp = $tmp+array_fill_keys(range(0,max(array_keys($tmp))),false);
89
            ksort($tmp);
90
            $res[] = $tmp;
91
        }
92
        return $res;
93
    }
94
}
95
/* vim: set tabstop=4 shiftwidth=4 expandtab: */
96