Skip to content

tiny-blocks/http

Repository files navigation

Http

License

Overview

Common implementations for HTTP protocol.

Installation

composer require tiny-blocks/http

How to use

The library exposes concrete implementations for the HTTP protocol, such as status codes, methods, etc.

Using the HttpCode

The library exposes a concrete implementation through the HttpCode enum. You can get the status codes, and their corresponding messages.

$httpCode = HttpCode::CREATED;

$httpCode->name;      # CREATED
$httpCode->value;     # 201
$httpCode->message(); # 201 Created

Using the HttpMethod

The library exposes a concrete implementation via the HttpMethod enum. You can get a set of HTTP methods.

$method = HttpMethod::GET;

$method->name;  # GET
$method->value; # GET

Using the HttpResponse

The library exposes a concrete implementation for HTTP responses via the HttpResponse class. Responses are of the ResponseInterface type, according to the specifications defined in PSR-7.

$data = new Xyz(value: 10);
$response = HttpResponse::ok(data: $data);

$response->getStatusCode();          # 200
$response->getReasonPhrase();        # 200 OK
$response->getBody()->getContents(); # {"value":10}

License

Http is licensed under MIT.

Contributing

Please follow the contributing guidelines to contribute to the project.