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

Csv   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 3
Bugs 2 Features 0
Metric Value
c 3
b 2
f 0
dl 0
loc 36
rs 10
wmc 5
lcom 0
cbo 4

1 Method

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