PHPackages                             vertilia/response - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [HTTP &amp; Networking](/categories/http)
4. /
5. vertilia/response

ActiveLibrary[HTTP &amp; Networking](/categories/http)

vertilia/response
=================

Library to abstract server responses (CLI and/or HTTP)

v1.1.1(3y ago)012BSD-2-ClausePHPPHP &gt;=7.4CI failing

Since May 12Pushed 3y ago1 watchersCompare

[ Source](https://github.com/vertilia/response)[ Packagist](https://packagist.org/packages/vertilia/response)[ RSS](/packages/vertilia-response/feed)WikiDiscussions master Synced 3w ago

READMEChangelogDependencies (2)Versions (7)Used By (0)

response
========

[](#response)

Simple library to abstract response data for several popular formats and provide output schema validation capabilities based on `php-filter` extension.

The use of [`ValidArray`](https://github.com/vertilia/valid-array) mechanism guarantees the output will match the predefined response schema.

Use case
--------

[](#use-case)

Your controller is an `HttpResponse` object with predefined filters. Filters configuration corresponds to the response schema for the particular route and is defined in leaf data of routes file under `response` element.

During pre-rendering phase controller sets corresponding elements as defined in `response`. At this moment validation mechanism evaluates values and sanitizes them if necessary to correspond to the filtering rules.

Items not present in filters list are discarded. Invalid values are set to `false`. Correct values are set as is. Items present in validation schema but not set take `null` value. Rules may be modified using `php-filter` flags constants and `ValidArray` enhancements, including default values for all filters (not only `FILTER_VALIDATE_*` filters).

In the following example request controller `UserGetEmail` is defined in route file as a handler for `GET /api/users/{id}/email` route. On creation, it receives validation filters and an `HttpRequest` object with request information. When `render()` method is called on this controller, it renders an HTTP response as JSON text with corresponding headers, this work is done by `JsonResponse`. What is handled by the controller itself is located in `preRender()` method. It populates controller elements `id` (from request) and `email` (from `User` object). These pre-validated elements will correspond the initial response schema and will be output in a JSON format (like `{"id":42,"email":"user@example.com"}`).

```
