Skip to content

ZPMLabs/from-swagger-to-php-sdk

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

4 Commits
Β 
Β 
Β 
Β 

Repository files navigation

FromSwaggerToPHPSDK – PHP SDK Generation Guide

This guide defines how to generate a complete PHP SDK from any valid Swagger/OpenAPI specification. It is used by AI coding assistants and tools integrated with Context7 to create versioned, testable, and PSR-compliant SDKs for PHP projects.


πŸ“₯ Input

  • swagger_url: A valid public or private URL to a Swagger document
  • info.title: Used to derive the Composer vendor name and PHP namespace
  • info.description: Used as the SDK description

πŸ“ Output Structure

The generated SDK will be placed into generated-sdk and follow this structure:

generated-sdk/
β”œβ”€β”€ composer.json
β”œβ”€β”€ src/
β”‚   └── v1/
β”‚       β”œβ”€β”€ Services/
β”‚       β”‚   └── {Tag}Service.php
β”‚       β”œβ”€β”€ Models/
β”‚       β”‚   β”œβ”€β”€ DTO/
β”‚       β”‚   β”‚   └── {Model}.php
β”‚       β”‚   └── Enums/
β”‚       β”‚       └── {Enum}.php
β”‚       └── Exceptions/
β”‚           └── {Exception}.php
β”œβ”€β”€ tests/
β”‚   β”œβ”€β”€ Unit/v1/Services/
β”‚   β”‚   └── {Service}Test.php
β”‚   └── Feature/v1/
β”‚       └── {Tag}Test.php
└── README.md

🧱 Components

Services

Each API tag becomes a service class (e.g., UsersService). Methods are generated from operationId, and constructor dependencies include:

  • GuzzleHttp\ClientInterface
  • string $apiKey (if Bearer auth is required)

DTOs (Data Transfer Objects)

Each OpenAPI schema is converted to a PHP class with:

  • CamelCase properties
  • Constructor accepting an array of data
  • Optional getters/setters

Enums

OpenAPI enums are generated as PHP 8.1+ native enums:

enum Status: string {
    case ACTIVE = 'active';
    case INACTIVE = 'inactive';
}

Exceptions

Each API error response (e.g., 401 Unauthorized) generates an exception:

class UnauthorizedException extends ApiException

All custom exceptions extend ApiException.

Tests

Tests are generated using Pest-compatible PHPUnit files:

  • Unit tests mock Guzzle responses
  • Feature tests simulate real API responses (optional)

πŸ”€ Type Mapping

Swagger Type PHP Type
string string
integer int
boolean bool
number float
array array
object {Class}
enum {Enum}

🧩 Templates

Templates are used to render actual PHP code for services, DTOs, enums, and exceptions. These templates include:

  • composer.json
  • Service class methods with annotations and Guzzle usage
  • DTO class with typed properties and parsing logic
  • Enums with all values listed
  • Base and custom exception classes
  • Unit test cases with mock expectations

🧠 Usage Prompt for AI (Cursor, Claude, etc.)

use context7

Generate a PHP SDK using FromSwaggerToPHPSDK instructions:
https://example.com/api/docs/

πŸ“Œ Notes

  • All code is generated using PHP 8.1+ syntax
  • The SDK supports PSR-4 autoloading for easy integration
  • The structure allows versioning (e.g., v1, v2 folders)

Version: 1.0.0
Language: PHP
Context: Context7 β†’ FromSwaggerToPHPSDK

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published