Csv   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 41
rs 10
c 0
b 0
f 0
wmc 4
lcom 1
cbo 3

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
use Gettext\Utils\CsvTrait;
8
9
/**
10
 * Class to export translations to csv.
11
 */
12
class Csv extends Generator implements GeneratorInterface
13
{
14
    use HeadersGeneratorTrait;
15
    use CsvTrait;
16
17
    public static $options = [
18
        'includeHeaders' => false,
19
        'delimiter' => ",",
20
        'enclosure' => '"',
21
        'escape_char' => "\\"
22
    ];
23
24
    /**
25
     * {@parentDoc}.
26
     */
27
    public static function toString(Translations $translations, array $options = [])
28
    {
29
        $options += static::$options;
30
        $handle = fopen('php://memory', 'w');
31
32
        if ($options['includeHeaders']) {
33
            self::fputcsv($handle, ['', '', self::generateHeaders($translations)], $options);
34
        }
35
36
        foreach ($translations as $translation) {
37
            $line = [$translation->getContext(), $translation->getOriginal(), $translation->getTranslation()];
38
39
            if ($translation->hasPluralTranslations(true)) {
40
                $line = array_merge($line, $translation->getPluralTranslations());
41
            }
42
43
            self::fputcsv($handle, $line, $options);
44
        }
45
46
        rewind($handle);
47
        $csv = stream_get_contents($handle);
48
        fclose($handle);
49
50
        return $csv;
51
    }
52
}
53