Skip to content

Commit

Permalink
Merge 813b5b8 into 06da52e
Browse files Browse the repository at this point in the history
  • Loading branch information
audunru committed Oct 31, 2021
2 parents 06da52e + 813b5b8 commit a1d0053
Show file tree
Hide file tree
Showing 13 changed files with 321 additions and 160 deletions.
26 changes: 15 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
Currently supported:

- CSV
- XLSX
- XML

# Installation
Expand All @@ -17,17 +18,13 @@ Currently supported:
composer require audunru/export-response
```

This package depends on [league/csv](https://csv.thephpleague.com/). You will have to install this separately if you want to export to CSV:
Depending on which formats you want to export to, you will have to install additional packages:

```bash
composer require league/csv
```

This package depends on [spatie/array-to-xml](https://github.com/spatie/array-to-xml). You will have to install this separately if you want to export to XML:

```bash
composer require spatie/array-to-xml
```
| Format | Package |
| ------ | ------------------------------------------------------------- |
| CSV | [spatie/simple-excel](https://github.com/spatie/simple-excel) |
| XLSX | [spatie/simple-excel](https://github.com/spatie/simple-excel) |
| XML | [spatie/array-to-xml](https://github.com/spatie/array-to-xml) |

## Step 2: Add middleware to your routes

Expand All @@ -38,6 +35,7 @@ To support CSV and XML exports for all your API endpoints, add it to `Kernel.php
'throttle:api',
\Illuminate\Routing\Middleware\SubstituteBindings::class,
\audunru\ExportResponse\Middleware\ExportCsv::class,
\audunru\ExportResponse\Middleware\ExportXlsx::class,
\audunru\ExportResponse\Middleware\ExportXml::class,
],
```
Expand All @@ -48,19 +46,23 @@ To add it to one particular API resource, you can use this in `api.php`:
Route::apiResource('documents', DocumentController::class)
->middleware([
ExportCsv::class,
ExportXlsx::class,
ExportXml::class
])
->name('documents');
```

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

```php
Route::apiResource('documents', DocumentController::class)
->middleware([
ExportCsv::with([
'key' => 'data',
]),
ExportXlsx::with([
'key' => 'data',
]),
ExportXml::class
])
->name('documents');
Expand Down Expand Up @@ -108,6 +110,8 @@ In order to retrieve an API response as CSV instead of JSON, send a request to y

For XML, set the header to `application/xml`.

For XLSX, set the header to `application/vnd.openxmlformats-officedocument.spreadsheetml.sheet`.

# Configuration

Publish the configuration file by running:
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^3.0",
"league/csv": "^9.7",
"orchestra/testbench": "^4.0|^5.0|^6.7",
"php-coveralls/php-coveralls": "^2.2",
"phpmd/phpmd": "^2.10",
"phpunit/phpunit": "^8.5|^9.0",
"roave/security-advisories": "dev-latest",
"spatie/array-to-xml": "^3.1"
"spatie/array-to-xml": "^3.1",
"spatie/simple-excel": "^1.14"
},
"config": {
"optimize-autoloader": true,
Expand Down
220 changes: 135 additions & 85 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions config/export-response.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php

use audunru\ExportResponse\Macros\Collection\FlattenArrays;
use audunru\ExportResponse\Macros\Collection\ToCsv;
use audunru\ExportResponse\Macros\JsonResponse\ToCsv as JsonResponseToCsv;
use audunru\ExportResponse\Macros\JsonResponse\ToXlsx as JsonResponseToXlsx;
use audunru\ExportResponse\Macros\JsonResponse\ToXml as JsonResponseToXml;
use audunru\ExportResponse\Macros\Request\Wants;
use audunru\ExportResponse\Macros\Response\Filename;
Expand All @@ -20,11 +20,11 @@
'macros' => [
'collection' => [
'flattenArrays' => FlattenArrays::class,
'toCsv' => ToCsv::class,
],
'json-response' => [
'toCsv' => JsonResponseToCsv::class,
'toXml' => JsonResponseToXml::class,
'toCsv' => JsonResponseToCsv::class,
'toXlsx' => JsonResponseToXlsx::class,
'toXml' => JsonResponseToXml::class,
],
'response' => [
'filename' => Filename::class,
Expand Down
6 changes: 4 additions & 2 deletions src/Enums/MimeType.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@

/**
* @method static self Csv()
* @method static self Xlsx()
* @method static self Xml()
*/
class MimeType extends Enum
{
protected static function values(): array
{
return [
'Csv' => 'text/csv',
'Xml' => 'application/xml',
'Csv' => 'text/csv',
'Xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
'Xml' => 'application/xml',
];
}
}

0 comments on commit a1d0053

Please sign in to comment.