Skip to content

Commit

Permalink
[Translation] support CsvDumper
Browse files Browse the repository at this point in the history
[Translation] support CsvDumper
[Translation] support CsvDumper
[Translation] support CsvDumper
[Translation] support CsvDumper
  • Loading branch information
stealth35 committed Sep 5, 2011
1 parent 55f5295 commit dea46c7
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 0 deletions.
55 changes: 55 additions & 0 deletions src/Symfony/Component/Translation/Dumper/CsvDumper.php
@@ -0,0 +1,55 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Translation\Dumper;

use Symfony\Component\Translation\MessageCatalogue;

/**
* CsvDumper generates a csv formated string representation of a message catalogue
*
* @author Stealth35
*/
class CsvDumper implements DumperInterface
{
private $delimiter = ';';
private $enclosure = '"';

/**
* {@inheritDoc}
*/
public function dump(MessageCatalogue $messages, $domain = 'messages')
{
$handle = fopen('php://memory', 'rb+');

foreach ($messages->all($domain) as $source => $target) {
fputcsv($handle, array($source, $target), $this->delimiter, $this->enclosure);
}

rewind($handle);
$output = stream_get_contents($handle);
fclose($handle);

return $output;
}

/**
* Sets the delimiter and escape character for CSV.
*
* @param string $delimiter delimiter character
* @param string $enclosure enclosure character
*/
public function setCsvControl($delimiter = ';', $enclosure = '"')
{
$this->delimiter = $delimiter;
$this->enclosure = $enclosure;
}
}
@@ -0,0 +1,30 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Tests\Component\Translation\Dumper;

use Symfony\Component\Translation\MessageCatalogue;
use Symfony\Component\Translation\Dumper\CsvDumper;

class CsvFileDumperTest extends \PHPUnit_Framework_TestCase
{
public function testDump()
{
$catalogue = new MessageCatalogue('en');
$catalogue->add(array('foo' => 'bar', 'bar' => 'foo
foo', 'foo;foo' => 'bar'));

$dumper = new CsvDumper();
$dumperString = $dumper->dump($catalogue);

$this->assertEquals(file_get_contents(__DIR__.'/../fixtures/valid.csv'), $dumperString);
}
}
4 changes: 4 additions & 0 deletions tests/Symfony/Tests/Component/Translation/fixtures/valid.csv
@@ -0,0 +1,4 @@
foo;bar
bar;"foo
foo"
"foo;foo";bar

0 comments on commit dea46c7

Please sign in to comment.