Skip to content

Commit

Permalink
Merge pull request #90 from greg0ire/mime_typed_writer
Browse files Browse the repository at this point in the history
When applicable, provide mime type
  • Loading branch information
soullivaneuh authored Jun 16, 2016
2 parents 377d231 + f6b244d commit 1674153
Show file tree
Hide file tree
Showing 12 changed files with 210 additions and 13 deletions.
5 changes: 5 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@
"Exporter\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Exporter\\Test\\": "test/"
}
},
"extra": {
"branch-alias": {
"dev-master": "1.x-dev"
Expand Down
4 changes: 3 additions & 1 deletion docs/reference/outputs.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@ Several output formatters are supported:
* Excel XML
* XLS (MS Excel)

You may also create your own. To do so, simply create a class that implements the ``Exporter\Writer\WriterInterface``.
You may also create your own. To do so, simply create a class that implements the ``Exporter\Writer\WriterInterface``,
or better, if you know what ``Content-Type`` header should be used along with
your output and what format it produces, ``TypedWriterInterface``.
21 changes: 20 additions & 1 deletion src/Writer/CsvWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@

use Exporter\Exception\InvalidDataFormatException;

class CsvWriter implements WriterInterface
/**
* @author Thomas Rabaix <[email protected]>
*/
class CsvWriter implements TypedWriterInterface
{
/**
* @var string
Expand Down Expand Up @@ -78,6 +81,22 @@ public function __construct($filename, $delimiter = ',', $enclosure = '"', $esca
}
}

/**
* {@inheritdoc}
*/
final public function getDefaultMimeType()
{
return 'text/csv';
}

/**
* {@inheritdoc}
*/
final public function getFormat()
{
return 'csv';
}

/**
* {@inheritdoc}
*/
Expand Down
21 changes: 20 additions & 1 deletion src/Writer/JsonWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@

namespace Exporter\Writer;

class JsonWriter implements WriterInterface
/**
* @author Thomas Rabaix <[email protected]>
*/
class JsonWriter implements TypedWriterInterface
{
/**
* @var string
Expand Down Expand Up @@ -41,6 +44,22 @@ public function __construct($filename)
}
}

/**
* {@inheritdoc}
*/
final public function getDefaultMimeType()
{
return 'application/json';
}

/**
* {@inheritdoc}
*/
final public function getFormat()
{
return 'json';
}

/**
* {@inheritdoc}
*/
Expand Down
34 changes: 34 additions & 0 deletions src/Writer/TypedWriterInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Exporter\Writer;

/**
* @author Grégoire Paris <[email protected]>
*/
interface TypedWriterInterface extends WriterInterface
{
/**
* There can be several mime types for a given format, this method should
* return the most appopriate / popular one.
*
* @return string the mime type of the output
*/
public function getDefaultMimeType();

/**
* Returns a string best describing the format of the output (the file
* extension is fine, for example).
*
* @return string a string without spaces, usable in a translation string
*/
public function getFormat();
}
21 changes: 20 additions & 1 deletion src/Writer/XlsWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@

namespace Exporter\Writer;

class XlsWriter implements WriterInterface
/**
* @author Thomas Rabaix <[email protected]>
*/
class XlsWriter implements TypedWriterInterface
{
/**
* @var string
Expand Down Expand Up @@ -50,6 +53,22 @@ public function __construct($filename, $showHeaders = true)
}
}

/**
* {@inheritdoc}
*/
final public function getDefaultMimeType()
{
return 'application/vnd.ms-excel';
}

/**
* {@inheritdoc}
*/
final public function getFormat()
{
return 'xls';
}

/**
* {@inheritdoc}
*/
Expand Down
21 changes: 20 additions & 1 deletion src/Writer/XmlWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@

use Exporter\Exception\InvalidDataFormatException;

class XmlWriter implements WriterInterface
/**
* @author Thomas Rabaix <[email protected]>
*/
class XmlWriter implements TypedWriterInterface
{
/**
* @var string
Expand Down Expand Up @@ -57,6 +60,22 @@ public function __construct($filename, $mainElement = 'datas', $childElement = '
}
}

/**
* {@inheritdoc}
*/
final public function getDefaultMimeType()
{
return 'text/xml';
}

/**
* {@inheritdoc}
*/
final public function getFormat()
{
return 'xml';
}

/**
* {@inheritdoc}
*/
Expand Down
48 changes: 48 additions & 0 deletions test/Writer/AbstractTypedWriterTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Exporter\Test\Writer;

use Exporter\Writer\WriterInterface;

/**
* @author Grégoire Paris <[email protected]>
*/
abstract class AbstractTypedWriterTestCase extends \PHPUnit_Framework_TestCase
{
/**
* @var WriterInterface
*/
private $writer;

protected function setUp()
{
$this->writer = $this->getWriter();
}

public function testFormatIsString()
{
$this->assertInternalType('string', $this->writer->getFormat());
}

public function testDefaultMimeTypeIsString()
{
$this->assertInternalType('string', $this->writer->getDefaultMimeType());
}

/**
* Should return a very simple instance of the writer (no need for complex
* configuration).
*
* @return WriterInterface
*/
abstract protected function getWriter();
}
12 changes: 10 additions & 2 deletions test/Writer/CsvWriterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@

use Exporter\Writer\CsvWriter;

class CsvWriterTest extends \PHPUnit_Framework_TestCase
class CsvWriterTest extends AbstractTypedWriterTestCase
{
protected $filename;

public function setUp()
{
parent::setUp();
$this->filename = 'foobar.csv';

if (is_file($this->filename)) {
Expand All @@ -28,7 +29,9 @@ public function setUp()

public function tearDown()
{
unlink($this->filename);
if (is_file($this->filename)) {
unlink($this->filename);
}
}

/**
Expand Down Expand Up @@ -96,4 +99,9 @@ public function testWithBom()
$expected = chr(0xEF).chr(0xBB).chr(0xBF).'name,surname,year'."\n".'"Rémi , """"2""","doe ",2001';
$this->assertEquals($expected, trim(file_get_contents($this->filename)));
}

protected function getWriter()
{
return new CsvWriter('/tmp/whatever.csv');
}
}
12 changes: 10 additions & 2 deletions test/Writer/JsonWriterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@

use Exporter\Writer\JsonWriter;

class JsonWriterTest extends \PHPUnit_Framework_TestCase
class JsonWriterTest extends AbstractTypedWriterTestCase
{
protected $filename;

public function setUp()
{
parent::setUp();
$this->filename = 'foobar.json';

if (is_file($this->filename)) {
Expand All @@ -28,7 +29,9 @@ public function setUp()

public function tearDown()
{
unlink($this->filename);
if (is_file($this->filename)) {
unlink($this->filename);
}
}

public function testWrite()
Expand All @@ -53,4 +56,9 @@ public function testWrite()

$this->assertEquals($expected, json_decode($content, false));
}

protected function getWriter()
{
return new JsonWriter('/tmp/whatever.json');
}
}
12 changes: 10 additions & 2 deletions test/Writer/XlsWriterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@

use Exporter\Writer\XlsWriter;

class XlsWriterTest extends \PHPUnit_Framework_TestCase
class XlsWriterTest extends AbstractTypedWriterTestCase
{
protected $filename;

public function setUp()
{
parent::setUp();
$this->filename = 'foobar.xls';

if (is_file($this->filename)) {
Expand All @@ -28,7 +29,9 @@ public function setUp()

public function tearDown()
{
unlink($this->filename);
if (is_file($this->filename)) {
unlink($this->filename);
}
}

public function testValidDataFormat()
Expand Down Expand Up @@ -56,4 +59,9 @@ public function testWithHeaders()

$this->assertEquals($expected, trim(file_get_contents($this->filename)));
}

protected function getWriter()
{
return new XlsWriter('/tmp/whatever.xls', false);
}
}
12 changes: 10 additions & 2 deletions test/Writer/XmlWriterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@

use Exporter\Writer\XmlWriter;

class XmlWriterTest extends \PHPUnit_Framework_TestCase
class XmlWriterTest extends AbstractTypedWriterTestCase
{
protected $filename;

public function setUp()
{
parent::setUp();
$this->filename = 'foobar.xml';

if (is_file($this->filename)) {
Expand All @@ -28,7 +29,9 @@ public function setUp()

public function tearDown()
{
unlink($this->filename);
if (is_file($this->filename)) {
unlink($this->filename);
}
}

/**
Expand Down Expand Up @@ -70,4 +73,9 @@ public function testInvalidDataFormat()

$this->assertEquals($expected, file_get_contents($this->filename));
}

protected function getWriter()
{
return new XmlWriter('/tmp/whatever.xml');
}
}

0 comments on commit 1674153

Please sign in to comment.