e454d16922ec43bb22a4dab999ee3b3047c78bb3
[yaffs-website] / vendor / symfony / translation / Loader / CsvFileLoader.php
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 $delimiter = ';';
24     private $enclosure = '"';
25     private $escape = '\\';
26
27     /**
28      * {@inheritdoc}
29      */
30     protected function loadResource($resource)
31     {
32         $messages = array();
33
34         try {
35             $file = new \SplFileObject($resource, 'rb');
36         } catch (\RuntimeException $e) {
37             throw new NotFoundResourceException(sprintf('Error opening file "%s".', $resource), 0, $e);
38         }
39
40         $file->setFlags(\SplFileObject::READ_CSV | \SplFileObject::SKIP_EMPTY);
41         $file->setCsvControl($this->delimiter, $this->enclosure, $this->escape);
42
43         foreach ($file as $data) {
44             if ('#' !== substr($data[0], 0, 1) && isset($data[1]) && 2 === count($data)) {
45                 $messages[$data[0]] = $data[1];
46             }
47         }
48
49         return $messages;
50     }
51
52     /**
53      * Sets the delimiter, enclosure, and escape character for CSV.
54      *
55      * @param string $delimiter Delimiter character
56      * @param string $enclosure Enclosure character
57      * @param string $escape    Escape character
58      */
59     public function setCsvControl($delimiter = ';', $enclosure = '"', $escape = '\\')
60     {
61         $this->delimiter = $delimiter;
62         $this->enclosure = $enclosure;
63         $this->escape = $escape;
64     }
65 }