PHPackages                             pupaxxo/swagger-builder - 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. [API Development](/categories/api)
4. /
5. pupaxxo/swagger-builder

ActiveProject[API Development](/categories/api)

pupaxxo/swagger-builder
=======================

Compile Swagger2.0 JSON using PHP.

2.0.2(8y ago)048MITPHP

Since Feb 21Pushed 8y ago1 watchersCompare

[ Source](https://github.com/pupaxxo/SwaggerBuilder)[ Packagist](https://packagist.org/packages/pupaxxo/swagger-builder)[ RSS](/packages/pupaxxo-swagger-builder/feed)WikiDiscussions master Synced 2mo ago

READMEChangelog (2)Dependencies (1)Versions (6)Used By (0)

SwaggerBuilder
==============

[](#swaggerbuilder)

Now you can write your Swagger API documentation in JSON, YAML, *or* PHP!

Do you find the Swagger config very hard to write? Well (with an informative IDE) this should make it easier.

### Usage Note:

[](#usage-note)

Fields Swagger requires appear in constructors, optional fields are set with specific setter methods. There's type hinting all over the place!

Example: Swagger Petstore (Simple)
----------------------------------

[](#example-swagger-petstore-simple)

#### Swagger

[](#swagger)

A valid Swagger object requires the Swagger version, an Info object and at least 1 Path.

```
$swagger = (new Swagger('2.0', $info, $paths))
    ->setHost('petstore.swagger.io')
    ->setBasePath('/api')
    ->setSchemes([Scheme::HTTP])
    ->setConsumedMimes([Mime::APP_JSON])
    ->setProducedMimes([Mime::APP_JSON]);
```

#### Info

[](#info)

A valid Info object requires the application name and version.

```
// (optional) contact from the example
$contact = (new Contact())
    ->setName('Swagger API team')
    ->setEmail('foo@example.com')
    ->setUrl('http://swagger.io');

// (optional) license from the example
$license = (new License('MIT'))
    ->setUrl('http://opensource.org/licenses/MIT');

// The Info object is what we really need
$info = (new Info('Swagger Petstore (Simple)', '1.0.0'))
    ->setDescription('A sample API that uses a petstore as an example to demonstrate features in the swagger-2.0 specification')
    ->setTermsOfService('http://helloreverb.com/terms/')
    ->setContact($contact)
    ->setLicense($license);
```

#### Paths

[](#paths)

A valid Path object requires a route and at least 1 Operation.

```
$paths = [
    new Path('/pets', $operations),
];
```

#### Operations

[](#operations)

A valid Operation object requires an HTTP verb and at least 1 example Response.

```
$addPet = (new Operation(Verb::POST, $responses))
    ->setDescription('Creates a new pet in the store. Duplicates are allowed')
    ->setOperationId('addPet')
    ->setProducedMimes()
    ->addParameter($addPetBody);

$operations = [
    $addPet,
];
```

#### Responses

[](#responses)

A valid Response object requires a status code (or 'default') and a description.

```
$petResponseModel = (new Schema())
    ->setProperty('id', (new Schema(Type::INTEGER))->setFormat(Format::LONG), true)
    ->setProperty('name', new Schema(Type::STRING), true)
    ->setProperty('tag', new Schema(Type::STRING));

$errorResponseModel = (new Schema())
    ->setProperty('code', (new Schema(Type::INTEGER))->setFormat(Format::INTEGER), true)
    ->setProperty('message', new Schema(Type::STRING), true);

$newPet = (new Schema())
    ->setProperty('id', (new Schema(Type::INTEGER))->setFormat(Format::LONG))
    ->setProperty('name', new Schema(Type::STRING), true)
    ->setProperty('tag', new Schema(Type::STRING));

$addPetBody = (new BodyParameter('pet', true, $newPet))
    ->setDescription('Pet to add to the store');

$responses = [
    (new Response(200, 'pet response'))->setSchema($petResponseModel),
    (new Response('default', 'unexpected error'))->setSchema($errorResponseModel),
];
```

See [`/example.php`](https://github.com/SamuelDavis/SwaggerBuilder/blob/master/example.php) for a complete implementation of all the paths in the Petstore CRUD example.

#### Swagger JSON

[](#swagger-json)

Just json\_encode the Swagger object (or any object which extends Component) to get a valid Swagger JSON blob.

```
echo str_replace(['\/'], ['/'], json_encode($swagger, JSON_PRETTY_PRINT)) . "\n";
```

#### Result

[](#result)

```
{
    "paths": {
        "/pets": {
            "post": {
                "responses": {
                    "200": {
                        "description": "pet response",
                        "schema": {
                            "type": "object",
                            "required": [
                                "id",
                                "name"
                            ],
                            "properties": {
                                "id": {
                                    "type": "integer",
                                    "format": "int64"
                                },
                                "name": {
                                    "type": "string"
                                },
                                "tag": {
                                    "type": "string"
                                }
                            }
                        }
                    },
                    "default": {
                        "description": "unexpected error",
                        "schema": {
                            "type": "object",
                            "required": [
                                "code",
                                "message"
                            ],
                            "properties": {
                                "code": {
                                    "type": "integer",
                                    "format": "int32"
                                },
                                "message": {
                                    "type": "string"
                                }
                            }
                        }
                    }
                },
                "description": "Creates a new pet in the store. Duplicates are allowed",
                "operationId": "addPet",
                "produces": [
                    "application/json"
                ],
                "parameters": [
                    {
                        "name": "pet",
                        "in": "body",
                        "required": true,
                        "schema": {
                            "type": "object",
                            "properties": {
                                "id": {
                                    "type": "integer",
                                    "format": "int64"
                                },
                                "name": {
                                    "type": "string"
                                },
                                "tag": {
                                    "type": "string"
                                }
                            },
                            "required": [
                                "name"
                            ]
                        },
                        "description": "Pet to add to the store"
                    }
                ]
            }
        }
    },
    "swagger": "2.0",
    "info": {
        "title": "Swagger Petstore (Simple)",
        "version": "1.0.0",
        "description": "A sample API that uses a petstore as an example to demonstrate features in the swagger-2.0 specification",
        "termsOfService": "http://helloreverb.com/terms/",
        "contact": {
            "name": "Swagger API team",
            "email": "foo@example.com",
            "url": "http://swagger.io"
        },
        "license": {
            "name": "MIT",
            "url": "http://opensource.org/licenses/MIT"
        }
    },
    "host": "petstore.swagger.io",
    "basePath": "/api",
    "schemes": [
        "http"
    ],
    "consumes": [
        "application/json"
    ],
    "produces": [
        "application/json"
    ]
}
```

###  Health Score

29

—

LowBetter than 60% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity8

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity67

Established project with proven stability

 Bus Factor1

Top contributor holds 96.6% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~66 days

Total

5

Last Release

3099d ago

Major Versions

1.0.1 → 2.0.02017-02-23

### Community

Maintainers

![](https://www.gravatar.com/avatar/209437b782838eb19058a44cc4b26c04f0917bed24681be789744e8553190240?d=identicon)[pupaxxo](/maintainers/pupaxxo)

---

Top Contributors

[![SamuelDavis](https://avatars.githubusercontent.com/u/2657052?v=4)](https://github.com/SamuelDavis "SamuelDavis (56 commits)")[![pupaxxo](https://avatars.githubusercontent.com/u/2815664?v=4)](https://github.com/pupaxxo "pupaxxo (2 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/pupaxxo-swagger-builder/health.svg)

```
[![Health](https://phpackages.com/badges/pupaxxo-swagger-builder/health.svg)](https://phpackages.com/packages/pupaxxo-swagger-builder)
```

###  Alternatives

[stripe/stripe-php

Stripe PHP Library

4.0k143.3M475](/packages/stripe-stripe-php)[twilio/sdk

A PHP wrapper for Twilio's API

1.6k92.9M270](/packages/twilio-sdk)[knplabs/github-api

GitHub API v3 client

2.2k15.8M186](/packages/knplabs-github-api)[facebook/php-business-sdk

PHP SDK for Facebook Business

90121.9M34](/packages/facebook-php-business-sdk)[microsoft/microsoft-graph

The Microsoft Graph SDK for PHP

65723.5M95](/packages/microsoft-microsoft-graph)[meilisearch/meilisearch-php

PHP wrapper for the Meilisearch API

73813.7M114](/packages/meilisearch-meilisearch-php)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
