Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
/*
4
 * This file is part of the Symfony package.
5
 *
6
 * (c) Fabien Potencier <fabien@symfony.com>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
 
12
namespace Symfony\Component\Translation\Loader;
13
 
14
use Symfony\Component\Translation\Exception\NotFoundResourceException;
15
 
16
/**
17
 * CsvFileLoader loads translations from CSV files.
18
 *
19
 * @author Saša Stamenković <umpirsky@gmail.com>
20
 */
21
class CsvFileLoader extends FileLoader
22
{
23
    private string $delimiter = ';';
24
    private string $enclosure = '"';
25
    private string $escape = '\\';
26
 
27
    protected function loadResource(string $resource): array
28
    {
29
        $messages = [];
30
 
31
        try {
32
            $file = new \SplFileObject($resource, 'rb');
33
        } catch (\RuntimeException $e) {
34
            throw new NotFoundResourceException(sprintf('Error opening file "%s".', $resource), 0, $e);
35
        }
36
 
37
        $file->setFlags(\SplFileObject::READ_CSV | \SplFileObject::SKIP_EMPTY);
38
        $file->setCsvControl($this->delimiter, $this->enclosure, $this->escape);
39
 
40
        foreach ($file as $data) {
41
            if (false === $data) {
42
                continue;
43
            }
44
 
45
            if (!str_starts_with($data[0], '#') && isset($data[1]) && 2 === \count($data)) {
46
                $messages[$data[0]] = $data[1];
47
            }
48
        }
49
 
50
        return $messages;
51
    }
52
 
53
    /**
54
     * Sets the delimiter, enclosure, and escape character for CSV.
55
     */
56
    public function setCsvControl(string $delimiter = ';', string $enclosure = '"', string $escape = '\\')
57
    {
58
        $this->delimiter = $delimiter;
59
        $this->enclosure = $enclosure;
60
        $this->escape = $escape;
61
    }
62
}