PHP code example of maxbeckers / php-builder-generator

1. Go to this page and download the library: Download maxbeckers/php-builder-generator 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/ */

    

maxbeckers / php-builder-generator example snippets



// php-builder-generator.php

use MaxBeckers\PhpBuilderGenerator\Config\BuilderConfig;
use MaxBeckers\PhpBuilderGenerator\Config\PhpBuilderGeneratorConfig;

return PhpBuilderGeneratorConfig::configure()
    // Option A: explicit per-class config
    ->class(App\Model\User::class)
    ->class(App\Model\Company::class, new BuilderConfig(fluent: false, exclude: ['internalId']))

    // Option B: scan a whole directory (generates a builder for every class found)
    ->scanDirectory('src/DTO')
    ->scanDirectory('src/Model', new BuilderConfig(fluent: true))

    // Output settings
    ->outputDir('generated/php-builder-generator/')
    ->phpVersion('8.2');


// src/Model/User.php

namespace App\Model;

class User
{
    public function __construct(
        public string $name,
        public string $email,
        public ?int $age = null,
        public array $roles = []
    ) {}
}

$user = UserBuilder::builder()
    ->name('John Doe')
    ->email('[email protected]')
    ->age(30)
    ->roles(['admin'])
    ->build();
json
{
  "config": {
    "allow-plugins": {
      "maxbeckers/php-builder-generator": true
    }
  }
}
bash
composer dump-autoload
bash
./vendor/bin/php-builder-generator