Search by

ket-php / http-response

mikhno351

Simple PHP Http API Payload builder

1.0.0 2026-09-16 20:26 UTC

This package is auto-updated.

Last update: 2026-09-16 20:31:53 UTC


README

Packagist Version Packagist Downloads Static Badge

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, and JsonSerializable instances
  • Strategy-Based Payloads - Easily switch between payload builders (JsonPayload, etc.)
  • PSR-7 Integration - Seamlessly populates Psr\Http\Message\ResponseInterface instances
  • Built-in Serialization - Full support for JsonSerializable, Stringable, and native PHP serialize() / 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);