Skip to content

Commit 720c294

Browse files
committed
refactor: adds discreet $body prop for type safety
1 parent a632982 commit 720c294

File tree

3 files changed

+60
-30
lines changed

3 files changed

+60
-30
lines changed

src/Providers/Http/Collections/HeadersCollection.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,17 +70,17 @@ public function getAll(): array
7070
}
7171

7272
/**
73-
* Gets the first value of a specific header.
73+
* Gets header values as a comma-separated string.
7474
*
7575
* @since n.e.x.t
7676
*
7777
* @param string $name The header name (case-insensitive).
78-
* @return string|null The first header value or null if not found.
78+
* @return string|null The header values as a comma-separated string or null if not found.
7979
*/
8080
public function getAsString(string $name): ?string
8181
{
8282
$values = $this->get($name);
83-
return $values !== null ? $values[0] : null;
83+
return $values !== null ? implode(', ', $values) : null;
8484
}
8585

8686
/**

src/Providers/Http/DTO/Request.php

Lines changed: 55 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,14 @@ class Request extends AbstractDataTransferObject
5050
protected HeadersCollection $headers;
5151

5252
/**
53-
* @var string|array<string, mixed>|null The request data.
53+
* @var array<string, mixed>|null The request data (for query params or form data).
5454
*/
55-
protected $data;
55+
protected ?array $data = null;
56+
57+
/**
58+
* @var string|null The request body (raw string content).
59+
*/
60+
protected ?string $body = null;
5661

5762
/**
5863
* Constructor.
@@ -75,7 +80,13 @@ public function __construct(HttpMethodEnum $method, string $uri, array $headers
7580
$this->method = $method;
7681
$this->uri = $uri;
7782
$this->headers = new HeadersCollection($headers);
78-
$this->data = $data;
83+
84+
// Separate data and body based on type
85+
if (is_string($data)) {
86+
$this->body = $data;
87+
} elseif (is_array($data)) {
88+
$this->data = $data;
89+
}
7990
}
8091

8192
/**
@@ -101,8 +112,8 @@ public function getMethod(): HttpMethodEnum
101112
*/
102113
public function getUri(): string
103114
{
104-
// If GET request with array data, append as query parameters
105-
if ($this->method === HttpMethodEnum::GET() && is_array($this->data) && !empty($this->data)) {
115+
// If GET request with data, append as query parameters
116+
if ($this->method === HttpMethodEnum::GET() && $this->data !== null && !empty($this->data)) {
106117
$separator = strpos($this->uri, '?') === false ? '?' : '&';
107118
return $this->uri . $separator . http_build_query($this->data);
108119
}
@@ -136,12 +147,12 @@ public function getHeader(string $name): ?array
136147
}
137148

138149
/**
139-
* Gets the first value of a specific header.
150+
* Gets header values as a comma-separated string.
140151
*
141152
* @since n.e.x.t
142153
*
143154
* @param string $name The header name (case-insensitive).
144-
* @return string|null The header value as a concatenated string, or null if not found.
155+
* @return string|null The header values as a comma-separated string, or null if not found.
145156
*/
146157
public function getHeaderAsString(string $name): ?string
147158
{
@@ -166,9 +177,9 @@ public function hasHeader(string $name): bool
166177
*
167178
* For GET requests, returns null.
168179
* For POST/PUT/PATCH requests:
169-
* - If data is a string, returns it as-is
170-
* - If data is an array and Content-Type is JSON, returns JSON-encoded data
171-
* - If data is an array and Content-Type is form, returns URL-encoded data
180+
* - If body is set, returns it as-is
181+
* - If data is set and Content-Type is JSON, returns JSON-encoded data
182+
* - If data is set and Content-Type is form, returns URL-encoded data
172183
*
173184
* @since n.e.x.t
174185
*
@@ -182,18 +193,13 @@ public function getBody(): ?string
182193
return null;
183194
}
184195

185-
// If data is null, return null
186-
if ($this->data === null) {
187-
return null;
188-
}
189-
190-
// If data is already a string, return it as-is
191-
if (is_string($this->data)) {
192-
return $this->data;
196+
// If body is set, return it as-is
197+
if ($this->body !== null) {
198+
return $this->body;
193199
}
194200

195-
// If data is an array, encode based on content type
196-
if (is_array($this->data)) {
201+
// If data is set, encode based on content type
202+
if ($this->data !== null) {
197203
$contentType = $this->getContentType();
198204

199205
// JSON encoding
@@ -249,22 +255,43 @@ public function withHeader(string $name, $value): self
249255
public function withData($data): self
250256
{
251257
$new = clone $this;
252-
$new->data = $data;
258+
if (is_string($data)) {
259+
$new->body = $data;
260+
$new->data = null;
261+
} elseif (is_array($data)) {
262+
$new->data = $data;
263+
$new->body = null;
264+
} else {
265+
$new->data = null;
266+
$new->body = null;
267+
}
253268
return $new;
254269
}
255270

256271
/**
257-
* Gets the request data.
272+
* Gets the request data array.
258273
*
259274
* @since n.e.x.t
260275
*
261-
* @return string|array<string, mixed>|null The request data.
276+
* @return array<string, mixed>|null The request data array.
262277
*/
263-
public function getData()
278+
public function getData(): ?array
264279
{
265280
return $this->data;
266281
}
267282

283+
/**
284+
* Gets the request body string.
285+
*
286+
* @since n.e.x.t
287+
*
288+
* @return string|null The request body string.
289+
*/
290+
public function getBodyString(): ?string
291+
{
292+
return $this->body;
293+
}
294+
268295
/**
269296
* {@inheritDoc}
270297
*
@@ -315,7 +342,10 @@ public function toArray(): array
315342
self::KEY_HEADERS => $this->headers->getAll(),
316343
];
317344

318-
if ($this->data !== null) {
345+
// Include whichever data type is set
346+
if ($this->body !== null) {
347+
$array[self::KEY_DATA] = $this->body;
348+
} elseif ($this->data !== null) {
319349
$array[self::KEY_DATA] = $this->data;
320350
}
321351

src/Providers/Http/DTO/Response.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,12 +105,12 @@ public function getHeader(string $name): ?array
105105
}
106106

107107
/**
108-
* Gets the first value of a specific header.
108+
* Gets header values as a comma-separated string.
109109
*
110110
* @since n.e.x.t
111111
*
112112
* @param string $name The header name (case-insensitive).
113-
* @return string|null The first header value or null if not found.
113+
* @return string|null The header values as a comma-separated string or null if not found.
114114
*/
115115
public function getHeaderAsString(string $name): ?string
116116
{

0 commit comments

Comments
 (0)