Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 22 additions & 7 deletions src/Network/Http.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php namespace Winter\Storm\Network;

use Winter\Storm\Exception\ApplicationException;
use Winter\Storm\Support\Str;

/**
* HTTP Network Access
Expand Down Expand Up @@ -114,7 +115,7 @@ class Http
public $requestOptions = [];

/**
* @var array Request data.
* @var array|string Request data.
*/
public $requestData;

Expand Down Expand Up @@ -441,21 +442,35 @@ protected function headerToArray($header)
return $headers;
}

/**
* Add JSON encoded payload
*/
public function json(mixed $payload): self
{
if (!Str::isJson($payload)) {
if (!$payload = json_encode($payload)) {
throw new ApplicationException('The provided payload failed to be encoded as JSON');
}
}

$this->requestData = $payload;
$this->header('Content-Type', 'application/json');

return $this;
}

/**
* Add a data to the request.
* @param string $value
* @return self
*/
public function data($key, $value = null)
public function data(array|string $key, string $value = null): self
{
if (is_array($key)) {
foreach ($key as $_key => $_value) {
$this->data($_key, $_value);
}
return $this;
} else {
$this->requestData[$key] = $value;
}

$this->requestData[$key] = $value;
return $this;
}

Expand Down