Download the PHP package mertcanekiz/phpydantic without Composer
On this page you can find all versions of the php package mertcanekiz/phpydantic. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download mertcanekiz/phpydantic
More information about mertcanekiz/phpydantic
Files in mertcanekiz/phpydantic
Package phpydantic
Short Description A reflection-driven BaseModel inspired by pydantic for JSON Schema generation in PHP
License MIT
Informations about the package phpydantic
Phpydantic
A lightweight PHP library for generating JSON Schemas from PHP models, inspired by Python's Pydantic. Ideal for integrating with OpenAI structured outputs, function calling, or any other JSON-schema–driven tooling.
Table of Contents
- Features
- Requirements
- Installation
- Usage
- 1. Defining Models
- 2. Generating JSON Schema
- 3. OpenAI Structured Outputs
- 4. Deserializing JSON Data
- API Reference
- Testing
- Contributing
- License
Features
- Automatic Type Inference: Infers JSON Schema types from PHP property types (
int,float,string,bool). - Nullable Support: Handles nullable types (e.g.,
?string) astype: ["string", "null"]. - Nested Models & Arrays: Supports nested
BaseModeland arrays of models via@var ModelName[]annotations. - Property Descriptions: Leverage
@Descriptiontags in docblocks to include descriptions in schemas. - Strict Mode: Generate strict schemas for OpenAI function calling or JSON validation.
Requirements
- PHP 8.0 or higher
- Composer
Installation
Install via Composer:
Usage
1. Defining Models
Extend Phpydantic\BaseModel and declare public properties. Use docblocks for metadata:
2. Generating JSON Schema
Use static methods to generate schema definitions:
Example JSON Schema
3. OpenAI Structured Outputs
Generate schemas compatible with OpenAI function calling or JSON mode:
4. Deserializing JSON Data
Use fromJson to create model instances from JSON strings:
The fromJson method handles:
- Nested model deserialization
- Arrays of models
- Type casting for primitive values
- Nullable properties
- JSON validation
API Reference
| Method | Description |
|---|---|
::schema(): array |
Returns schema as a PHP array. |
::jsonSchema(): string |
Returns pretty-printed JSON schema. |
::openAiSchema(): array |
Wraps schema() for OpenAI function-calling compatibility. |
::fromJson(string): static |
Creates a model instance from a JSON string. |
Testing
Run PHPUnit tests:
Contributing
Contributions are welcome! Please follow these steps:
- Fork the repository
- Create a new branch:
git checkout -b feature/YourFeature - Commit your changes:
git commit -m 'Add new feature' - Push to your branch:
git push origin feature/YourFeature - Open a Pull Request
Ensure all tests pass and adhere to PSR-12 coding standards.
License
This project is licensed under the MIT License. See the LICENSE file for details.