Completed
Push — master ( 55caa0...3f2e9b )
by Oscar
02:05
created

Csv   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 37
rs 10
wmc 4
lcom 1
cbo 2

1 Method

Rating   Name   Duplication   Size   Complexity  
B toString() 0 25 4
1
<?php
2
3
namespace Gettext\Generators;
4
5
use Gettext\Translations;
6
use Gettext\Utils\HeadersGeneratorTrait;
7
8
/**
9
 * Class to export translations to csv.
10
 */
11
class Csv extends Generator implements GeneratorInterface
12
{
13
    use HeadersGeneratorTrait;
14
15
    public static $options = [
16
        'includeHeaders' => true,
17
    ];
18
19
    /**
20
     * {@parentDoc}.
21
     */
22
    public static function toString(Translations $translations, array $options = [])
23
    {
24
        $options += static::$options;
25
        $handle = fopen('php://memory', 'w');
26
27
        if ($options['includeHeaders']) {
28
            fputcsv($handle, ['', '', self::generateHeaders($translations)]);
29
        }
30
31
        foreach ($translations as $translation) {
32
            $line = [$translation->getContext(), $translation->getOriginal(), $translation->getTranslation()];
33
34
            if ($translation->hasPluralTranslations()) {
35
                $line = array_merge($line, $translation->getPluralTranslations());
36
            }
37
38
            fputcsv($handle, $line);
39
        }
40
41
        rewind($handle);
42
        $csv = stream_get_contents($handle);
43
        fclose($handle);
44
45
        return $csv;
46
    }
47
}
48