Completed
Push — master ( 166484...24c373 )
by Patrick
02:36
created

JsonSpreadSheet   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 40
rs 10
wmc 8
lcom 0
cbo 1

1 Method

Rating   Name   Duplication   Size   Complexity  
C serializeData() 0 35 8
1
<?php
2
namespace Serialize;
3
4
class JsonSpreadSheet extends SpreadSheetSerializer
5
{
6
    protected $types = array('json-ss', 'json-ss-dt');
7
8
    public function serializeData(&$type, $array)
9
    {
10
        if($this->supportsType($type) === false)
11
        {
12
            return null;
13
        }
14
        if(count($array) === 0)
15
        {
16
            return null;
17
        }
18
        $dataTable = ($type === 'json-ss-dt');
19
        $type = 'application/json';
20
        $data = $this->getArray($array);
21
        $names = array_shift($data);
22
        $rowCount = count($data);
23
        for($i = 0; $i < $rowCount; $i++)
24
        {
25
            $row = $data[$i];
26
            $colCount = count($row);
27
            for($j = 0; $j < $colCount; $j++)
28
            {
29
                if(isset($row[$j]) && isset($names[$j]))
30
                {
31
                    $row[$names[$j]] = $row[$j];
32
                    unset($row[$j]);
33
                }
34
            }
35
            $data[$i] = $row;
36
        }
37
        if($dataTable)
38
        {
39
            return json_encode(array('data'=>$data));
40
        }
41
        return json_encode($data);
42
    }
43
}
44