1
|
|
|
<?php |
2
|
|
|
namespace Serialize; |
3
|
|
|
|
4
|
|
|
abstract class SpreadSheetSerializer extends Serializer |
5
|
|
|
{ |
6
|
|
|
private function getKeysFromData($array) |
|
|
|
|
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) |
|
|
|
|
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
|
|
|
$this->addValueByColName($cols, $row, $colName, $value->{'$id'}); |
50
|
|
|
break; |
51
|
|
|
default: |
52
|
|
|
$props = get_object_vars($value); |
53
|
|
|
foreach($props as $key=>$newValue) |
54
|
|
|
{ |
55
|
|
|
$this->addValueByColName($cols, $row, $colName.'.'.$key, $newValue); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
return; |
59
|
|
|
} |
60
|
|
|
$index = array_search($colName, $cols); |
61
|
|
|
if($index === false) |
62
|
|
|
{ |
63
|
|
|
$index = count($cols); |
64
|
|
|
$cols[$index] = $colName; |
65
|
|
|
} |
66
|
|
|
if(is_array($value)) |
67
|
|
|
{ |
68
|
|
|
$row[$index] = implode(',', $value); |
69
|
|
|
} |
70
|
|
|
else |
71
|
|
|
{ |
72
|
|
|
$row[$index] = $value; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
protected function getArray(&$array) |
77
|
|
|
{ |
78
|
|
|
$res = array(); |
79
|
|
|
if(is_object($array)) |
80
|
|
|
{ |
81
|
|
|
$array = array(get_object_vars($array)); |
82
|
|
|
} |
83
|
|
|
$res[] = array(); |
84
|
|
|
foreach($array as $row) |
85
|
|
|
{ |
86
|
|
|
$tmp = array(); |
87
|
|
|
$row = $this->getRowArray($row); |
88
|
|
|
foreach($row as $colName=>$value) |
89
|
|
|
{ |
90
|
|
|
$this->addValueByColName($res[0], $tmp, $colName, $row[$colName]); |
91
|
|
|
} |
92
|
|
|
$tmp = $tmp+array_fill_keys(range(0,max(array_keys($tmp))),false); |
93
|
|
|
ksort($tmp); |
94
|
|
|
$res[] = $tmp; |
95
|
|
|
} |
96
|
|
|
return $res; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|