ArrayLoader.php
2 months ago
CsvFileLoader.php
2 months ago
FileLoader.php
2 months ago
IcuDatFileLoader.php
2 months ago
IcuResFileLoader.php
2 months ago
IniFileLoader.php
2 months ago
JsonFileLoader.php
2 months ago
LoaderInterface.php
2 months ago
MoFileLoader.php
2 months ago
PhpFileLoader.php
2 months ago
PoFileLoader.php
2 months ago
QtFileLoader.php
2 months ago
XliffFileLoader.php
2 months ago
YamlFileLoader.php
2 months ago
CsvFileLoader.php
57 lines
| 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 | namespace ProfilePressVendor\Symfony\Component\Translation\Loader; |
| 12 | |
| 13 | use ProfilePressVendor\Symfony\Component\Translation\Exception\NotFoundResourceException; |
| 14 | /** |
| 15 | * CsvFileLoader loads translations from CSV files. |
| 16 | * |
| 17 | * @author Saša Stamenković <umpirsky@gmail.com> |
| 18 | */ |
| 19 | class CsvFileLoader extends FileLoader |
| 20 | { |
| 21 | private $delimiter = ';'; |
| 22 | private $enclosure = '"'; |
| 23 | private $escape = ''; |
| 24 | /** |
| 25 | * {@inheritdoc} |
| 26 | */ |
| 27 | protected function loadResource(string $resource) |
| 28 | { |
| 29 | $messages = []; |
| 30 | try { |
| 31 | $file = new \SplFileObject($resource, 'rb'); |
| 32 | } catch (\RuntimeException $e) { |
| 33 | throw new NotFoundResourceException(sprintf('Error opening file "%s".', $resource), 0, $e); |
| 34 | } |
| 35 | $file->setFlags(\SplFileObject::READ_CSV | \SplFileObject::SKIP_EMPTY); |
| 36 | $file->setCsvControl($this->delimiter, $this->enclosure, '' === $this->escape && \PHP_VERSION_ID < 70400 ? '\\' : $this->escape); |
| 37 | foreach ($file as $data) { |
| 38 | if (\false === $data) { |
| 39 | continue; |
| 40 | } |
| 41 | if ('#' !== substr($data[0], 0, 1) && isset($data[1]) && 2 === \count($data)) { |
| 42 | $messages[$data[0]] = $data[1]; |
| 43 | } |
| 44 | } |
| 45 | return $messages; |
| 46 | } |
| 47 | /** |
| 48 | * Sets the delimiter, enclosure, and escape character for CSV. |
| 49 | */ |
| 50 | public function setCsvControl(string $delimiter = ';', string $enclosure = '"', string $escape = '') |
| 51 | { |
| 52 | $this->delimiter = $delimiter; |
| 53 | $this->enclosure = $enclosure; |
| 54 | $this->escape = '' === $escape && \PHP_VERSION_ID < 70400 ? '\\' : $escape; |
| 55 | } |
| 56 | } |
| 57 |