thefrozenfire/swagger

There is no license information available for the latest version (2.0.10) of this package.

Swagger parser for PHP

2.0.10 2016-08-04 22:00 UTC

This package is not auto-updated.

Last update: 2021-11-22 12:32:47 UTC


README

Parse Swagger specification documents programmatically, and also parse data structures according to a specification.

Installation

composer require thefrozenfire/swagger:^2.0

License

Use

Describe an Operation

<?php
$spec = file_get_contents('http://petstore.swagger.io/v2/swagger.json');
$decodedSpec = json_decode($spec);

$document = new Swagger\Document($decodedSpec);

$operation = $document->getOperationById('addPet');
?>
Operation ID: <?=$operation->getOperationId()?> 
Tags: <?=implode(', ', $operation->getTags())?> 
Summary: <?=$operation->getSummary()?> 
Description: <?=$operation->getDescription()?> 
External Documentation: <?=$operation->getExternalDocs()?> 

Parse a data structure

<?php
$spec = file_get_contents('http://petstore.swagger.io/v2/swagger.json');
$decodedSpec = json_decode($spec);

$dataToParse = '...';

$document = new Swagger\Document($decodedSpec);
$schemaResolver = $document->getSchemaResolver();

$dataType = $document->getSchemaForOperationResponse(
    'getPetById',
    200
);

$schemaObject = $schemaResolver->parseType($dataType, $dataToParse);
?>
Pet name: <?=$schemaObject->getProperty('name')?>
Pet status: <?=$schemaObject->status?>