PHP Classes

How to Use a PHP AI Library that Returns Data Transfer Objects as Response to User Prompts Using the Package AI DTO Hydrator: Create objects (DTOs) from responses to AI prompts

Recommend this page to a friend!
  Info   Documentation   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2026-06-15 (8 days ago) RSS 2.0 feedNot yet rated by the usersTotal: Not yet counted Not yet ranked
Version License PHP version Categories
ai-dto-hydrator 1.0MIT/X Consortium ...8Web services, Language, Artificial in..., P...
Description 

Author

This package can create objects (DTOs) from responses to AI prompts.

It provides a hydrator class and provider classes to access artificial intelligence API services to send prompt texts with requests using natural language.

The artificial intelligence provider classes can adapt the accesses to different artificial intelligence services like OpenAI.

The hydrator class can use the PHP reflection API to return a data transfer object that applications can use to process the artificial intelligence service response in some useful way for the application.

The returned data transfer object has getter functions to return values of types supported by PHP (int, float, bool, string, DateTime).

Picture of Robson Antonio Lima de Mendonça
  Performance   Level  
Name: Robson Antonio Lima de ... <contact>
Classes: 1 package by
Country: Brazil Brazil
Age: 44
All time rank: Not yet ranked
Week rank: Not yet ranked
Innovation award
Innovation award
Nominee: 1x

Instructions

Setup

Ensure your package files are structured using PSR-4 autoloading (configured in your composer.json targeting the "Innovation\AIHydrator\" namespace to the "src/" directory). Run composer dump-autoload to initialize the autoloader.

Quick Start Guide

  1. Define your target Data Transfer Object (DTO) using PHP 8+ native strict typing:
class OrderDTO
{
    private string $customerName;
    private int $quantity;
    private float $totalPrice;
    private \DateTime $purchaseDate;
    private bool $isPaid;

    public function getCustomerName(): string { return $this->customerName; }
    public function getQuantity(): int { return $this->quantity; }
    public function getTotalPrice(): float { return $this->totalPrice; }
    public function getPurchaseDate(): \DateTime { return $this->purchaseDate; }
    public function isPaid(): bool { return $this->isPaid; }
}

  1. Initialize the Hydrator and pass your unstructured raw text (e.g., from an email, SMS, or chat webhook):
use Innovation\AIHydrator\AIHydrator;
use Innovation\AIHydrator\OpenAIProvider;

require_once 'vendor/autoload.php';

// Instantiate your preferred AI provider adapter
$provider = new OpenAIProvider('your-api-key-here');
$hydrator = new AIHydrator($provider);

// Unstructured natural language input
$rawText = "Hey team, I am Robson. I just transferred 249.90 via Pix yesterday for those 5 t-shirts. Please confirm order.";

// Map and hydrate into a strictly-typed PHP Object dynamically using Reflection
/ @var OrderDTO $order */
$order = $hydrator->hydrate($rawText, OrderDTO::class);

// Access your data safely with full IDE autocomplete support
echo $order->getCustomerName();                 // Output: Robson
echo $order->getQuantity();                     // Output: 5 (as int)
echo $order->getPurchaseDate()->format('Y-m-d'); // Output: 2026-06-15 (as DateTime)

Running Tests

You can run the built-in isolated test script locally to verify the Reflection mapping and type coercion without spending API credits:

php -d assert.active=1 tests/HydratorTest.php

Documentation

AI-Driven DTO Hydrator & Context Mapper

PHP Version License

An innovative, lightweight, and framework-agnostic PHP 8+ library designed to bridge the gap between unstructured AI responses and PHP strict typing.

Using PHP's native Reflection API, this package automatically generates a type-safe schema from any Data Transfer Object (DTO) or class, prompts an AI provider to extract data from raw text, and safely hydrates the object while enforcing native PHP types (int, float, bool, string, DateTime).

? Why is it Innovative?

  1. Strict Type Enforcement: Most AI packages only return raw strings or loose JSON. This library uses reflection to inspect your target class properties and dynamically forces the AI to output schema-matching data types.
  2. Native Type Coercion & Safety: It shields your application from `TypeError` exceptions. The engine sanitizes and casts data before injecting it into private or protected properties.
  3. Zero Dependencies: It does not rely on heavy frameworks like Laravel or Symfony. It can be dropped into any legacy or modern PHP project.
  4. Provider Agnostic: Easily switch between OpenAI, Gemini, Claude, or local LLMs (like Ollama) by implementing a simple interface.

? Installation & Structure

Clone the repository or download the package structure:

ai-hydrator-package/
??? src/
?   ??? AIHydrator.php
?   ??? AIProviderInterface.php
?   ??? OpenAIProvider.php
??? tests/
?   ??? HydratorTest.php
??? composer.json
??? README.md

If using Composer, ensure your autoloader is updated:

composer dump-autoload

? Quick Start / Usage Example

1. Define your Target Class (DTO)

Create any standard PHP class using native type-hinting:

class OrderDTO
{
    private string $customerName;
    private int $quantity;
    private float $totalPrice;
    private \DateTime $purchaseDate;
    private bool $isPaid;

    // Getters
    public function getCustomerName(): string { return $this->customerName; }
    public function getQuantity(): int { return $this->quantity; }
    public function getTotalPrice(): float { return $this->totalPrice; }
    public function getPurchaseDate(): \DateTime { return $this->purchaseDate; }
    public function isPaid(): bool { return $this->isPaid; }
}

2. Hydrate the Object from Unstructured Text

use Innovation\AIHydrator\AIHydrator;
use Innovation\AIHydrator\OpenAIProvider;

// 1. Initialize the AI Provider (or any custom provider)
$provider = new OpenAIProvider('your-api-key-here');
$hydrator = new AIHydrator($provider);

// 2. Raw unstructured text (e.g., from an email or chat webhook)
$rawEmailText = "Hey team, I'm Robson. I just transferred 249.90 via Pix yesterday for those 5 t-shirts. Please confirm order.";

// 3. Magic happens here: Turn text into a strictly-typed PHP Object
/ @var OrderDTO $order */
$order = $hydrator->hydrate($rawEmailText, OrderDTO::class);

// 4. Use your object safely with full IDE autocomplete support
echo $order->getCustomerName();       // Output: Robson
echo $order->getQuantity();           // Output: 5 (as an integer)
echo $order->getPurchaseDate()->format('Y-m-d'); // Output: 2026-06-15 (as DateTime)

?? Testing

The package includes a built-in test suite that simulates an AI response using a manual Mock, allowing you to verify the reflection engine and type coercion without spending API credits.

To run the test localy, execute:

php -d assert.active=1 tests/HydratorTest.php

? License

This project is open-source and licensed under the MIT License. Feel free to use, modify, and share.

Submitted to the PHP Innovation Award on PHPClasses.org.


  Files folder image Files (18)  
File Role Description
Files folder imageai-hydrator-package (3 files, 3 directories)
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files (18)  /  ai-hydrator-package  
File Role Description
Files folder imagesrc (3 files)
Files folder imagetests (1 file)
Files folder imagevendor (1 file, 1 directory)
  Accessible without login Plain text file composer.json Data Auxiliary data
  Accessible without login Plain text file LICENSE Lic. License text
  Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files (18)  /  ai-hydrator-package  /  src  
File Role Description
  Plain text file AIHydrator.php Class Class source
  Plain text file AIProviderInterface.php Class Class source
  Plain text file OpenAIProvider.php Class Class source

  Files folder image Files (18)  /  ai-hydrator-package  /  tests  
File Role Description
  Plain text file HydratorTest.php Class Class source

  Files folder image Files (18)  /  ai-hydrator-package  /  vendor  
File Role Description
Files folder imagecomposer (8 files)
  Accessible without login Plain text file autoload.php Aux. Configuration script

  Files folder image Files (18)  /  ai-hydrator-package  /  vendor  /  composer  
File Role Description
  Accessible without login Plain text file autoload_classmap.php Aux. Configuration script
  Accessible without login Plain text file autoload_namespaces.php Aux. Configuration script
  Accessible without login Plain text file autoload_psr4.php Aux. Configuration script
  Plain text file autoload_real.php Class Class source
  Plain text file autoload_static.php Class Class source
  Plain text file ClassLoader.php Class Class source
  Accessible without login Plain text file LICENSE Lic. License text
  Accessible without login Plain text file platform_check.php Aux. Configuration script

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads  
 100%
Total:0
This week:0