Skip to content

Commit

Permalink
feat: xml export supports child keys
Browse files Browse the repository at this point in the history
close #7
  • Loading branch information
Travis CI committed Oct 31, 2021
1 parent ec24485 commit 0c63581
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 8 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ Route::apiResource('documents', DocumentController::class)
->name('documents');
```

The `ExportCsv` and `ExportXlsx` middlewares allows you to specify an array key which will be used to retrieve the data. "Dot" notation is supported.
You can specify an array key which will be used to retrieve the data. "Dot" notation is supported.

```php
Route::apiResource('documents', DocumentController::class)
Expand All @@ -63,7 +63,9 @@ Route::apiResource('documents', DocumentController::class)
ExportXlsx::with([
'key' => 'data',
]),
ExportXml::class
ExportXml::class::with([
'key' => 'data',
]),
])
->name('documents');
```
Expand Down
4 changes: 3 additions & 1 deletion src/Macros/JsonResponse/ToCsv.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ public function __invoke()
{
return function (string $filename, string $key = null): StreamedResponse {
$data = $this->getData(true);
$data = ! is_null($key) ? Arr::get($data, $key) : $data;
if (! is_null($key)) {
$data = Arr::get($data, $key);
}
$collection = Arr::isAssoc($data) ? collect([$data]) : collect($data);

return $collection->toCsv($filename);
Expand Down
4 changes: 3 additions & 1 deletion src/Macros/JsonResponse/ToXlsx.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ public function __invoke()
{
return function (string $filename, string $key = null): StreamedResponse {
$data = $this->getData(true);
$data = ! is_null($key) ? Arr::get($data, $key) : $data;
if (! is_null($key)) {
$data = Arr::get($data, $key);
}
$collection = Arr::isAssoc($data) ? collect([$data]) : collect($data);

return $collection->toXlsx($filename);
Expand Down
5 changes: 4 additions & 1 deletion src/Macros/JsonResponse/ToXml.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@ class ToXml
{
public function __invoke()
{
return function (string $filename): Response {
return function (string $filename, string $key = null): Response {
$data = $this->getData(true);
if (! is_null($key)) {
$data = Arr::get($data, $key);
}
$data = Arr::isAssoc($data) ? $data : ['data' => $data];
$xml = ArrayToXml::convert($data);

Expand Down
7 changes: 5 additions & 2 deletions src/Middleware/ExportXml.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,27 @@
use Closure;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use TiMacDonald\Middleware\HasParameters;

class ExportXml
{
use HasParameters;

/**
* @SuppressWarnings("unused")
*/
public function __construct(protected FilenameGeneratorContract $filenameGenerator)
{
}

public function handle(Request $request, Closure $next)
public function handle(Request $request, Closure $next, string $key = null)
{
$response = $next($request);

if ($request->wants(MimeType::Xml()) && $response instanceof JsonResponse) {
$filename = $this->filenameGenerator->get($request, $response);

return $response->toXml($filename);
return $response->toXml($filename, $key);
}

return $response;
Expand Down
2 changes: 1 addition & 1 deletion tests/Feature/XmlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public function testItGetsXmlFromWrappedResponse()

$this->assertEquals("attachment; filename=\"documents.xml\"; filename*=utf-8''documents.xml", $response->headers->get('Content-Disposition'));
$this->assertEquals('<?xml version="1.0"?>
<root><data><id>1</id><name>Navn</name><data><foo>bar</foo></data><meta/></data><data><id>2</id><name>Noe annet</name><data><foo>bar</foo><bar>foo</bar></data></data></root>
<root><data><id>1</id><name>Navn</name><data><foo>bar</foo></data><meta/></data><data><id>2</id><name>Noe annet</name><data><foo>bar</foo><bar>foo</bar></data></data><meta><page>1</page></meta></root>
', $response->getContent());
}
}
3 changes: 3 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,9 @@ protected function getWrappedResponse(): JsonResponse
],
],
],
'meta' => [
'page' => 1,
],
]);
}

Expand Down
13 changes: 13 additions & 0 deletions tests/Unit/JsonResponseToXmlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,19 @@ public function testItGeneratesXmlFromWrappedResponse()
Collection::macro('toXml', app(ToXml::class)());
JsonResponse::macro('JsonResponseToXml', app(ToXml::class)());

$response = $this->getWrappedResponse()->toXml('filename.xml');

$this->assertEquals("attachment; filename=\"filename.xml\"; filename*=utf-8''filename.xml", $response->headers->get('Content-Disposition'));
$this->assertEquals('<?xml version="1.0"?>
<root><data><id>1</id><name>Navn</name><data><foo>bar</foo></data><meta/></data><data><id>2</id><name>Noe annet</name><data><foo>bar</foo><bar>foo</bar></data></data><meta><page>1</page></meta></root>
', $response->getContent());
}

public function testItGeneratesXmlFromWrappedResponseUsingKey()
{
Collection::macro('toXml', app(ToXml::class)());
JsonResponse::macro('JsonResponseToXml', app(ToXml::class)());

$response = $this->getWrappedResponse()->toXml('filename.xml', 'data');

$this->assertEquals("attachment; filename=\"filename.xml\"; filename*=utf-8''filename.xml", $response->headers->get('Content-Disposition'));
Expand Down

0 comments on commit 0c63581

Please sign in to comment.