Csv   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1
Metric Value
wmc 5
lcom 0
cbo 1
dl 0
loc 37
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A terminate() 0 17 3
A getName() 0 3 1
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
}