Download the PHP package wakeonweb/swagger without Composer
On this page you can find all versions of the php package wakeonweb/swagger. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package swagger
WakeOnWeb Swagger Validation Component
The WakeOnWeb Swagger Validation Component is an extensible component for validating API data using the Swagger - OpenAPI specification. The component supports both YAML and JSON Open API file formats. The component has very a small dependency set in order to be usable in different PHP frameworks.
The component uses:
- PSR-6: For caching your OpenApi specification files
- PSR-7: For processing HTTP messages (requests and responses)
Installation
The component can easily be installed using
composer require wakeonweb/swagger
The component uses a JSON Schema validator, by default, the justinrainbow/json-schema is in the dev dependencies. If you intend to use the component in production you need to execute:
composer require justinrainbow/json-schema
Loading an OpenAPI specification file
The component supports both YAML and JSON OpenAPI format. Swagger files are loaded by the SwaggerFactory
. The factory
accepts a PSR-6 CacheItemPoolInterface
. If none provided it will use the
ArrayCachePool
provided by cache/array-adapter.
Executing this code will result in retrieving a tree representation of the specification into an instance of a Swagger
document. At the moment, the cache contains the instance of the Swagger
document.
Creating the a content validator
Content validation in the component is based on JSON Schema Validation. The OpenAPI Specification handles much more than this. For example it allows to define query string parameters or the format of any HTTP Headers. The component supports all kind of validation.
Content validators are used to validate the content of a request or a response. Any content validator must implement the
ContentValidatorInterface
and should be registered into an instance of a ContentValidator
. The resulting instance can be
used into an instance of a SwaggerValidator
.
Validating a response
Validating response makes sense only for testing... As you are supposed to have valid code respectful of your interface agreements in production!
Validating a request
Validating response makes sense only for testing... As you are supposed to have valid code respectful of your interface agreements in production!
Complete sample
The following sample code demonstrates a complete usage of the component.