CsvDictionary::fromString()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 12
nc 3
nop 3
dl 0
loc 21
rs 9.3142
c 1
b 0
f 0
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 CsvDictionary 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 = self::fgetcsv($handle, $options)) {
35
            list($original, $translation) = $row + ['', ''];
36
37
            if ($original === '') {
38
                self::extractHeaders($translation, $translations);
39
                continue;
40
            }
41
42
            $translations->insert(null, $original)->setTranslation($translation);
43
        }
44
45
        fclose($handle);
46
    }
47
}
48