Skip to content

Commit c24e7dd

Browse files
Update RouteMatch's toArray method and test
The toArray method in RouteMatch has been updated to only include properties that are not empty. Accordingly, the related unit test has been adjusted to expect an empty array when no data is set.
1 parent 6ba5c42 commit c24e7dd

File tree

2 files changed

+44
-24
lines changed

2 files changed

+44
-24
lines changed

src/Config/Routes/RouteMatch.php

Lines changed: 43 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -312,17 +312,48 @@ public function parseFromArray(array $data): void
312312
*/
313313
#[\Override] public function toArray(): array
314314
{
315-
return [
316-
'host' => $this->getHost(),
317-
'method' => $this->getMethod(),
318-
'destination' => $this->getDestination(),
319-
'scheme' => $this->getScheme(),
320-
'uri' => $this->getUri(),
321-
'arguments' => $this->getArguments(),
322-
'query' => $this->getQuery(),
323-
'cookies' => $this->getCookies(),
324-
'headers' => $this->getHeaders(),
325-
'source' => $this->getSource(),
326-
];
315+
$data = [];
316+
317+
if (!empty($this->getHost())) {
318+
$data['host'] = $this->getHost();
319+
}
320+
321+
if (!empty($this->getMethod())) {
322+
$data['method'] = $this->getMethod();
323+
}
324+
325+
if (!empty($this->getDestination())) {
326+
$data['destination'] = $this->getDestination();
327+
}
328+
329+
if (!empty($this->getScheme())) {
330+
$data['scheme'] = $this->getScheme();
331+
}
332+
333+
if (!empty($this->getUri())) {
334+
$data['uri'] = $this->getUri();
335+
}
336+
337+
if (!empty($this->getArguments())) {
338+
$data['arguments'] = $this->getArguments();
339+
}
340+
341+
if (!empty($this->getQuery())) {
342+
$data['query'] = $this->getQuery();
343+
}
344+
345+
if (!empty($this->getCookies())) {
346+
$data['cookies'] = $this->getCookies();
347+
}
348+
349+
if (!empty($this->getHeaders())) {
350+
$data['headers'] = $this->getHeaders();
351+
}
352+
353+
if (!empty($this->getSource())) {
354+
$data['source'] = $this->getSource();
355+
}
356+
357+
return $data;
327358
}
328359
}

tests/Unit/Routes/RouteMatchTest.php

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -148,16 +148,5 @@
148148

149149
it('should return empty array if no data is set', function () {
150150
$routeMatch = new RouteMatch();
151-
expect($routeMatch->toArray())->toBe([
152-
'host' => '',
153-
'method' => null,
154-
'destination' => '',
155-
'scheme' => null,
156-
'uri' => '',
157-
'arguments' => [],
158-
'query' => [],
159-
'cookies' => [],
160-
'headers' => [],
161-
'source' => '',
162-
]);
151+
expect($routeMatch->toArray())->toBe([]);
163152
});

0 commit comments

Comments
 (0)