/php-api-descriptions

A PHP7 library for loading api descriptions and using them to validate PSR7 messages.

Primary LanguagePHPGNU Lesser General Public License v3.0LGPL-3.0

KleijnWeb\PhpApi\Descriptions

Build Status Coverage Status Scrutinizer Code Quality Latest Stable Version

A PHP7 library for loading api descriptions and using them to validate PSR7 messages. Enables object hydration in combination with KleijnWeb\PhpApi\Hydrator.

Supported formats:

Limited:

* RAML is much more feature-rich and generally elaborate standard than OpenAPI, it will take some time to support the full set. Help is appreciated.

The aim is to provide full support and interchangeability.

Typical Usage

Validating Requests And Responses

Namespaces omitted for brevity:

$validator = new MessageValidator(
  (new Repository('some/path'))->get('some-service/v1.0.1/swagger.yml')
);
/** @var ServerRequestInterface $request */
$result = $validator->validateRequest($request, $path);

/** @var ResponseInterface $response */
$result = $validator->validateResponse($body, $request, $response, $path);

If you're feeling frisky and want to try RAML support:

$validator = new MessageValidator(
    (new Repository())
        ->setFactory(new DescriptionFactory(DescriptionFactory::BUILDER_RAML))
        ->get('tests/definitions/raml/mobile-order-api/api.raml')
);

Integration

If you want OpenAPI support in combination with Symfony, you should check out SwaggerBundle.

And there's PSR-7/PSR-15 Middleware, which behaves pretty much the same but is much more reusable.

Limitations

  • Very limited RAML support
  • Does not work with form data
  • Requires a router to determine the matching path
  • If the request has a body, it will have to be deserialized using objects, not as an associative array
  • Requires the response body to be passed unserialized
  • Response validation does not validate headers and content-types (yet)

Contributing

Pull requests are very welcome, but the code has to be PSR2 compliant, follow used conventions concerning parameter and return type declarations, and the coverage can not go down.

License

KleijnWeb\PhpApi\Descriptions is made available under the terms of the LGPL, version 3.0.