Completed
Pull Request — master (#145)
by
unknown
06:03 queued 01:17
created

Csv::fputcsv()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 3
Ratio 37.5 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 3
dl 3
loc 8
rs 9.4285
c 0
b 0
f 0
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' => false,
17
        'delimiter' => ",",
18
        'enclosure' => '"',
19
        'escape_char' => "\\"
20
    ];
21
22
    /**
23
     * {@parentDoc}.
24
     */
25
    public static function toString(Translations $translations, array $options = [])
26
    {
27
        $options += static::$options;
28
        $handle = fopen('php://memory', 'w');
29
30
        if ($options['includeHeaders']) {
31
            self::fputcsv($handle, ['', '', self::generateHeaders($translations)], $options);
32
        }
33
34
        foreach ($translations as $translation) {
35
            $line = [$translation->getContext(), $translation->getOriginal(), $translation->getTranslation()];
36
37
            if ($translation->hasPluralTranslations(true)) {
38
                $line = array_merge($line, $translation->getPluralTranslations());
39
            }
40
41
            self::fputcsv($handle, $line, $options);
42
        }
43
44
        rewind($handle);
45
        $csv = stream_get_contents($handle);
46
        fclose($handle);
47
48
        return $csv;
49
    }
50
51
    /**
52
     * @param resource $handle
53
     * @param array $fields
54
     * @param array $options
55
     *
56
     * @return bool|int
57
     */
58
    private static function fputcsv($handle, $fields, $options)
59
    {
60 View Code Duplication
        if (version_compare(PHP_VERSION, '5.5.4') >= 0) { // >= 5.5.4
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
61
            return fputcsv($handle, $fields, $options['delimiter'], $options['enclosure'], $options['escape_char']);
62
        }
63
64
        return fputcsv($handle, $fields, $options['delimiter'], $options['enclosure']);
65
    }
66
}
67