Skip to content

Commit

Permalink
Better way to create payload (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
jenky authored Jun 23, 2023
1 parent 16642f9 commit 049c498
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 19 deletions.
10 changes: 9 additions & 1 deletion src/Body/AsJson.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,15 @@

namespace Jenky\Atlas\Body;

use Jenky\Atlas\Contracts\PayloadInterface;

trait AsJson
{
protected $bodyFormat = JsonPayload::class;
/**
* Create new JSON request body.
*/
protected function definePayload(): PayloadInterface
{
return new JsonPayload(is_array($this->defaultBody()) ? $this->defaultBody() : []);
}
}
10 changes: 9 additions & 1 deletion src/Body/AsMultipart.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,15 @@

namespace Jenky\Atlas\Body;

use Jenky\Atlas\Contracts\PayloadInterface;

trait AsMultipart
{
protected $bodyFormat = MultipartPayload::class;
/**
* Create new multipart request body.
*/
protected function definePayload(): PayloadInterface
{
return new MultipartPayload(is_array($this->defaultBody()) ? $this->defaultBody() : []);
}
}
10 changes: 9 additions & 1 deletion src/Body/AsText.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,15 @@

namespace Jenky\Atlas\Body;

use Jenky\Atlas\Contracts\PayloadInterface;

trait AsText
{
protected $bodyFormat = RawPayload::class;
/**
* Create new text request body.
*/
protected function definePayload(): PayloadInterface
{
return new RawPayload($this->defaultBody() ?: '');
}
}
29 changes: 13 additions & 16 deletions src/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Jenky\Atlas;

use InvalidArgumentException;
use Jenky\Atlas\Body\FormPayload;
use Jenky\Atlas\Contracts\DecoderInterface;
use Jenky\Atlas\Contracts\PayloadInterface;
Expand Down Expand Up @@ -60,20 +59,6 @@ protected function defaultBody()
return null;
}

/**
* Create a body payload from body format.
*/
protected function definePayload(): PayloadInterface
{
$payload = property_exists($this, 'bodyFormat') ? $this->bodyFormat : FormPayload::class;

if (! is_a($payload, PayloadInterface::class, true)) {
throw new InvalidArgumentException('Payload class must be instance of '.PayloadInterface::class);
}

return new $payload(is_array($this->defaultBody()) ? $this->defaultBody() : []);
}

/**
* Get request HTTP method.
*/
Expand Down Expand Up @@ -112,12 +97,24 @@ public function headers(): Map
public function body(): PayloadInterface
{
if (! $this->body instanceof PayloadInterface) {
$this->body = $this->definePayload();
$this->body = $this->createPayload();
}

return $this->body;
}

/**
* Create a corresponding payload for request body.
*/
private function createPayload(): PayloadInterface
{
if (method_exists($this, 'definePayload')) {
return $this->definePayload();
}

return new FormPayload(is_array($this->defaultBody()) ? $this->defaultBody() : []);
}

/**
* Get the response decoder.
*/
Expand Down

0 comments on commit 049c498

Please sign in to comment.