PHP code example of ipagdevs / schema-builder

1. Go to this page and download the library: Download ipagdevs/schema-builder library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

ipagdevs / schema-builder example snippets




use IpagDevs\Model\Model;
use IpagDevs\Model\Schema\Schema;
use IpagDevs\Model\Schema\SchemaBuilder;

class Review extends Model
{
    protected function schema(SchemaBuilder $schema): Schema
    {
        $schema->int('rating')->

// Parse data from an array
$review = Review::parse([
    'rating' => 5,
    'comment' => 'Excellent product!'
]);

// Get attributes
echo $review->get('rating'); // 5
echo $review->get('author'); // 'Anonymous' (from default)

// Parsing with missing N_PRETTY_PRINT);
// or
echo json_encode($review->jsonSerialize(), JSON_PRETTY_PRINT);



use IpagDevs\Model\Model;
use IpagDevs\Model\Schema\Schema;
use IpagDevs\Model\Schema\Mutator;
use IpagDevs\Model\Schema\SchemaBuilder;
use IpagDevs\Model\Schema\MutatorContext;

class Category extends Model
{
    protected function schema(SchemaBuilder $schema): Schema
    {
        $schema->int('id')->$schema->bool('is_active')->default(true);
        $schema->date('available_since', 'Y-m-d')-> An array of strings
        $schema->int('alternate_ids')->list()->nullable(); // An array of integers
        $schema->string('matrix')->list()->list()->nullable(); // An array of arrays of strings

        // Hidden Attributes
        $schema->string('internal_code')->hidden(); // Always hidden from jsonSerialize()
        $schema->string('promo_code')->nullable()->hiddenIf(
            fn($value, Product $model) => $model->get('price') > 100.0
        );

        // Relationships
        $schema->has('category', Category::class)->

$productData = [
    'id' => 101,
    'name' => 'Awesome Wireless Keyboard',
    'price' => 129.99,
    'available_since' => '2025-10-20',
    'condition' => 'new',
    'internal_code' => 'XYZ-SECRET',
    'short_description' => 'This will be truncated for sure.',
    'tags' => ['wireless', 'mechanical', 'rgb'],
    'category' => ['id' => 15, 'name' => 'Peripherals'],
    'reviews' => [
        ['rating' => 5, 'comment' => 'Best keyboard ever!'],
        ['rating' => 4, 'author' => 'A happy user'],
    ],
    'sku' => 'awk-101-blue',
    'promo_code' => 'SAVE10'
];

$product = Product::parse($productData);

// Accessing data
echo $product->get('name'); // 'Awesome Wireless Keyboard'
echo $product->get('short_description'); // 'This will '

// Mutators are applied automatically
echo $product->get('sku'); // 'SKU-AWK-101-BLUE'
echo $product->get('slug'); // 'awesome-wireless-keyboard'

// Relationships are parsed into Model instances
echo get_class($product->get('category')); // 'Category'
echo get_class($product->get('reviews')[0]); // 'Review'

$product = Product::parse($productData);
$json = $product->jsonSerialize();

// $json will NOT contain 'internal_code'.
// 'promo_code' will be hidden because price > 100.
// 'category' and 'reviews' will be arrays of serialized data.

$array = $product->toArray();

// $array WILL contain 'internal_code'.