Csv   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 42
rs 10
wmc 5
lcom 1
cbo 5

1 Method

Rating   Name   Duplication   Size   Complexity  
A fromString() 0 27 5
1
<?php
2
3
namespace Gettext\Extractors;
4
5
use Gettext\Translations;
6
use Gettext\Utils\HeadersExtractorTrait;
7
use Gettext\Utils\CsvTrait;
8
9
/**
10
 * Class to get gettext strings from csv.
11
 */
12
class Csv extends Extractor implements ExtractorInterface
13
{
14
    use HeadersExtractorTrait;
15
    use CsvTrait;
16
17
    public static $options = [
18
        'delimiter' => ",",
19
        'enclosure' => '"',
20
        'escape_char' => "\\"
21
    ];
22
23
    /**
24
     * {@inheritdoc}
25
     */
26
    public static function fromString($string, Translations $translations, array $options = [])
27
    {
28
        $options += static::$options;
29
        $handle = fopen('php://memory', 'w');
30
31
        fputs($handle, $string);
32
        rewind($handle);
33
34
        while ($row = static::fgetcsv($handle, $options)) {
35
            $context = array_shift($row);
36
            $original = array_shift($row);
37
38
            if ($context === '' && $original === '') {
39
                static::extractHeaders(array_shift($row), $translations);
40
                continue;
41
            }
42
43
            $translation = $translations->insert($context, $original);
44
45
            if (!empty($row)) {
46
                $translation->setTranslation(array_shift($row));
47
                $translation->setPluralTranslations($row);
48
            }
49
        }
50
51
        fclose($handle);
52
    }
53
}
54