Csv::terminate()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 17
rs 9.4286
cc 3
eloc 12
nc 4
nop 2
1
<?php
2
3
/*
4
 * (c) Jean-François Lépine <https://twitter.com/Halleck45>
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace Hal\Application\Formater\Details;
11
use Hal\Application\Formater\FormaterInterface;
12
use Hal\Component\Result\ResultCollection;
13
14
15
/**
16
 * Formater for xml export
17
 *
18
 * @author Jean-François Lépine <https://twitter.com/Halleck45>
19
 */
20
class Csv implements FormaterInterface {
21
22
    /**
23
     * Constructor
24
     */
25
    public function __construct()
26
    {
27
    }
28
29
    /**
30
     * @inheritdoc
31
     */
32
    public function terminate(ResultCollection $collection, ResultCollection $groupedResults){
33
34
        $fwd = fopen('php://memory', 'w');
35
        if(sizeof($collection, COUNT_NORMAL) > 0) {
36
            $r = current($collection->asArray());
37
            $labels = array_keys($r);
38
            fputcsv($fwd, $labels);
39
        }
40
        foreach($collection as $item) {
41
            fputcsv($fwd, $item->asArray());
42
        }
43
44
        rewind($fwd);
45
        $r =  stream_get_contents($fwd);
46
        fclose($fwd);
47
        return $r;
48
    }
49
50
    /**
51
     * @inheritdoc
52
     */
53
    public function getName() {
54
        return 'CSV';
55
    }
56
}