Skip to content
This repository was archived by the owner on Dec 20, 2021. It is now read-only.

Commit 4c14ac8

Browse files
authored
Merge pull request #38 from nreynis/xlsx-support
feat(xlsx): add supports for xlsx
2 parents d0aa2ef + c865973 commit 4c14ac8

File tree

3 files changed

+32
-3
lines changed

3 files changed

+32
-3
lines changed

src/RREST.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class RREST
2424
'json' => ['application/json', 'application/x-json'],
2525
'xml' => ['text/xml', 'application/xml', 'application/x-xml'],
2626
'csv' => ['text/csv', 'application/csv'],
27+
'xlsx' => ['application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'],
2728
];
2829

2930
/**

src/Response.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class Response
4141
/**
4242
* @var string[]
4343
*/
44-
protected $supportedFormat = ['json', 'xml', 'csv'];
44+
protected $supportedFormat = ['json', 'xml', 'csv', 'xlsx'];
4545

4646
/**
4747
* @var RouterInterface
@@ -264,10 +264,10 @@ public function serialize($data, $format)
264264
$data = json_decode(json_encode($data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE), true);
265265

266266
return $serializer->serialize($data, $format);
267-
} elseif ($format === 'csv') {
267+
} elseif ($format === 'csv' || $format === 'xlsx') {
268268
if (!is_string($data)) {
269269
throw new \RuntimeException(
270-
'auto serialization for CSV format is not supported'
270+
'auto serialization for '.strtoupper($format).' format is not supported'
271271
);
272272
}
273273

tests/units/Response.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,34 @@ public function testSerializeForCsv()
121121
;
122122
}
123123

124+
public function testNoXlsxSerialize()
125+
{
126+
$this->newTestedInstance($this->router, 'xlsx', 200);
127+
128+
$this
129+
->given($this->testedInstance)
130+
->exception(
131+
function () {
132+
$this->testedInstance->serialize([], 'xlsx');
133+
}
134+
)
135+
->isInstanceOf('\RuntimeException')
136+
->message->contains('auto serialization for XLSX format is not supported');
137+
}
138+
139+
public function testSerializeForXlsx()
140+
{
141+
$this->newTestedInstance($this->router, 'xlsx', 200);
142+
143+
$xlsx = "Placeholder for binary content";
144+
145+
$this
146+
->given($this->testedInstance)
147+
->string($this->testedInstance->serialize($xlsx, 'xlsx'))
148+
->isEqualTo($xlsx);
149+
;
150+
}
151+
124152
public function testAssertReponseSchema()
125153
{
126154
$this->newTestedInstance($this->router, 'json', 200);

0 commit comments

Comments
 (0)