Download the PHP package synatos/porta without Composer
On this page you can find all versions of the php package synatos/porta. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download synatos/porta
More information about synatos/porta
Files in synatos/porta
Informations about the package porta
Porta
Porta is greek and means door. This project can be used as door for your Rest API endpoints. The Open API standard allows to describe exactly what information in a rest api is coming in and leaving a server. The standard can be used for 1.) Description / Documentation of interfaces with tools like swagger 2.) Validation of incoming and outgoing Rest Messages 3.) Generation of Request/Response classes that allow to transfer untyped array/json to typed Objects.
Description
Porta is an Open API 3.0 Schema Validator for Http Requests. Key features of this implementation:
- Validate JSON request and responses
- Manipulate/Build Open API schema with objects
- Register your own format validator
- Generate Request and Response classes for the given schema
- Compile a Open API Schema to a class.
Installation
Import/Export
You can easily import export JSON/Yaml Files
Validation
Request Validation
Request can easily be validated against a given schema. Important: make sure the content type for the request is set in the http header. Open API allows several artifacts to be validated:
- Query Parameter
- Route Parameter
- Http Header
- Cookies
- Request Body
The example below shows how a http request is validated against the open API Schema.
Register your own format validator
The Open API Specification allows to define formats to specify a datatype in more detail
"However, format is an open value, so you can use any formats, even not those defined by the OpenAPI Specification, such as:" (Source https://swagger.io/docs/specification/data-models/data-types/#string)
With porta you can register your own validator to handle formats. See the example below:
Generate PHP Class from Schema
Schemas get easily get larger and parsing json/yaml including the file load can become time consuming. Therefore an existing schema can be compiled into a class the has the schema as array inline
This compile a class with the following content:
Generate Open API Class
You can generate for a given schema a class that will contain all the typed properties. Futhermore two method
\JsonSerializable
and fromArray(array $data)
allow to initialize from array
and generated an array again. This way you do not need to access
arrays anymore but can create the Schema object from an array and work fully typed with
the object.
The generated class will look like this
Model Manipulation
Every artifact in Open API has a object representation. So you can easily build up Schema, Operations or any other part
Further Readings
- Introduction https://swagger.io/docs/specification/about/
- Open API Schema https://swagger.io/docs/specification/data-models/
- Specification https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.0.md