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

Csv::toString()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 25
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 4
eloc 14
c 2
b 1
f 0
nc 6
nop 2
dl 0
loc 25
rs 8.5806
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