ket-php / http-response
Simple PHP Http API Payload builder
1.0.0
2026-09-16 20:26 UTC
Requires
- php: ^8.1
- psr/http-message: ^2.0
Requires (Dev)
- symfony/var-dumper: ^6.4
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
A lightweight, modular, and extensible HTTP response payload library for PHP applications. Designed with Strategy Pattern and PSR-7 support to handle standardized JSON API responses and custom data formats seamlessly.
Features
- Payload Normalization - Recursively processes objects, nested arrays,
DateTimeInterface, andJsonSerializableinstances - Strategy-Based Payloads - Easily switch between payload builders (
JsonPayload, etc.) - PSR-7 Integration - Seamlessly populates
Psr\Http\Message\ResponseInterfaceinstances - Built-in Serialization - Full support for
JsonSerializable,Stringable, and native PHPserialize()/unserialize() - Trait Utility - Reusable data normalization logic via
PayloadTrait
Installation
composer require ket-php/http
Usage
Basic Usage with JsonPayload allows you to construct fluent and normalized data structures independently of HTTP headers.
use KetPHP\Http\Common\Payload\JsonPayload; $payload = (new JsonPayload()) ->with('message', 'User created successfully') ->with('user', [ 'id' => 1, 'created_at' => new DateTimeImmutable(), ]); // Convert to array $arrayData = $payload->jsonSerialize(); // Serialize to JSON string $jsonString = $payload->toJson(); // or simply cast to string: $jsonString = (string) $payload;
HttpResponse with PSR-7
HttpResponse wraps payloads and emits them directly into PSR-7 HTTP response stream instances while automatically configuring content type headers.
use KetPHP\Http\HttpResponse; use KetPHP\Http\Common\Payload\JsonPayload; use Psr\Http\Message\ResponseInterface; /** @var ResponseInterface $psrResponse */ // Using default JsonPayload $response = HttpResponse::create() ->with('status', 'success') ->with('data', $userData) ->toResponse($psrResponse); // Overriding HTTP status code directly on PSR-7 response $response = HttpResponse::create() ->with('error', 'Resource Not Found') ->toResponse($psrResponse) ->withStatus(404);
Custom Payload Strategy
You can define custom formats by implementing PayloadInterface:
use KetPHP\Http\Common\Support\PayloadTrait; use KetPHP\Http\Common\PayloadInterface; final class XmlPayload implements PayloadInterface { use PayloadTrait; private array $payload = []; public function setPayload(array $payload): static { $this->payload = $this->prepareData($payload); return $this; } public function with(string $key, mixed $data): static { $this->payload[$key] = $this->prepareData($data); return $this; } public function getContentType(): string { return 'application/xml'; } public function jsonSerialize(): array { return $this->payload; } public function __toString(): string { // Custom XML rendering implementation return '<xml>...</xml>'; } // and etc... }
Then instantiate HttpResponse with your custom strategy:
$response = HttpResponse::create(XmlPayload::class) ->with('status', 'ok') ->toResponse($psrResponse);