Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Translation] support CsvDumper [Translation] support CsvDumper [Translation] support CsvDumper [Translation] support CsvDumper
- Loading branch information
Showing
3 changed files
with
89 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
tests/Symfony/Tests/Component/Translation/Dumper/CsvFileDumperTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
foo;bar | ||
bar;"foo | ||
foo" | ||
"foo;foo";bar |