Skip to content

Commit dea46c7

Browse files
committed
[Translation] support CsvDumper
[Translation] support CsvDumper [Translation] support CsvDumper [Translation] support CsvDumper [Translation] support CsvDumper
1 parent 55f5295 commit dea46c7

File tree

3 files changed

+89
-0
lines changed

3 files changed

+89
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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\Dumper;
13+
14+
use Symfony\Component\Translation\MessageCatalogue;
15+
16+
/**
17+
* CsvDumper generates a csv formated string representation of a message catalogue
18+
*
19+
* @author Stealth35
20+
*/
21+
class CsvDumper implements DumperInterface
22+
{
23+
private $delimiter = ';';
24+
private $enclosure = '"';
25+
26+
/**
27+
* {@inheritDoc}
28+
*/
29+
public function dump(MessageCatalogue $messages, $domain = 'messages')
30+
{
31+
$handle = fopen('php://memory', 'rb+');
32+
33+
foreach ($messages->all($domain) as $source => $target) {
34+
fputcsv($handle, array($source, $target), $this->delimiter, $this->enclosure);
35+
}
36+
37+
rewind($handle);
38+
$output = stream_get_contents($handle);
39+
fclose($handle);
40+
41+
return $output;
42+
}
43+
44+
/**
45+
* Sets the delimiter and escape character for CSV.
46+
*
47+
* @param string $delimiter delimiter character
48+
* @param string $enclosure enclosure character
49+
*/
50+
public function setCsvControl($delimiter = ';', $enclosure = '"')
51+
{
52+
$this->delimiter = $delimiter;
53+
$this->enclosure = $enclosure;
54+
}
55+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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\Tests\Component\Translation\Dumper;
13+
14+
use Symfony\Component\Translation\MessageCatalogue;
15+
use Symfony\Component\Translation\Dumper\CsvDumper;
16+
17+
class CsvFileDumperTest extends \PHPUnit_Framework_TestCase
18+
{
19+
public function testDump()
20+
{
21+
$catalogue = new MessageCatalogue('en');
22+
$catalogue->add(array('foo' => 'bar', 'bar' => 'foo
23+
foo', 'foo;foo' => 'bar'));
24+
25+
$dumper = new CsvDumper();
26+
$dumperString = $dumper->dump($catalogue);
27+
28+
$this->assertEquals(file_get_contents(__DIR__.'/../fixtures/valid.csv'), $dumperString);
29+
}
30+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
foo;bar
2+
bar;"foo
3+
foo"
4+
"foo;foo";bar

0 commit comments

Comments
 (0)