Completed
Push — master ( d2d480...28a4db )
by Patrick
03:16
created

CSVSerializer::serializeData()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 25
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 2
Metric Value
cc 5
eloc 15
c 2
b 0
f 2
nc 5
nop 2
dl 0
loc 25
rs 8.439
1
<?php
2
namespace Serialize;
3
4
class CSVSerializer extends SpreadSheetSerializer
5
{
6
    protected $types = array('csv', 'text/csv');
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
        $data = $this->getArray($array);
19
        ob_start();
20
        $df = fopen('php://output', 'w');
21
        foreach($data as $row)
22
        {
23
            if(!is_array($row))
24
            {
25
                $row = array($row);
26
            }
27
            fputcsv($df, $row);
28
        }
29
        fclose($df);
30
        $ret = ob_get_clean();
31
        return $ret;
32
    }
33
}
34
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...
35