Download the PHP package mf1dd/object-builder without Composer
On this page you can find all versions of the php package mf1dd/object-builder. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download mf1dd/object-builder
More information about mf1dd/object-builder
Files in mf1dd/object-builder
Package object-builder
Short Description Automatically creates fully populated PHP object instances with random, type-safe values. Designed for test fixtures and runtime object generation. Supports all PHP class types — regular classes, readonly classes, abstract classes, enums, traits, interfaces, and built-in stock classes like DateInterval or ArrayObject. Recursively resolves nested object graphs, detects property names for semantically meaningful values (timezone, email, firstname …), and offers constraint-based value ranges via a fluent API. No framework required; zero runtime dependencies beyond nikic/php-parser. PHP 8.2+.
License MIT
Informations about the package object-builder
ObjectBuilder
Automatic object creation with random values for PHP 8.2+.
Why?
Manually creating test objects is repetitive, error-prone, and time-consuming — especially for deeply nested object graphs with dozens of properties. Every constructor change forces you to update all test fixtures.
ObjectBuilder solves this: class name in, fully populated instance out. Recursive, type-safe, with semantically meaningful values.
Advantages
- No boilerplate —
ObjectBuilder::init(Foo::class)->build()instead of manualnew Foo(...)with 10 parameters - Type-safe — reflection-based, supports all native and custom types including enums, interfaces, traits, abstract, and readonly classes
- Deep object graphs — nested dependencies are resolved
automatically and recursively (
Person → Address → Street) - Semantic values — detects property names (
email,timezone,firstname) and generates matching random values - Constraints — value ranges and formats via the
with()API:->with('age', ['min' => 18, 'max' => 65]) - Overridable — set specific properties to fixed values while keeping the rest random
- Extensible — custom type builders and stock class handlers can be registered, builders are swappable
- No framework — pure PHP library, zero external runtime
dependencies except
nikic/php-parser
Compatibility
| PHP Version | Status |
|---|---|
| 8.2 | Fully supported, CI-tested |
| 8.3 | Fully supported, CI-tested |
| 8.4 | Fully supported, CI-tested |
| 8.5 | CI-tested (once available) |
- Runtime dependency:
nikic/php-parser ^5.0 - No framework (Symfony, Laravel, etc.) required
- Package name:
mf1dd/object-builder
Basic Usage
Simple classes are automatically populated with random values.
You can override specific values. Unset values are generated randomly. Nested objects are resolved automatically.
Enumerations
You can specify which values the enum should use.
Traits
For traits, an anonymous class is created that uses the trait. Parameters passed to the TraitBuilder are ignored.
Interfaces
The given interface is loaded and a class is dynamically generated from it. It returns the interface with the required methods and implements the interface.
The return value of methods is determined and a random value is assigned.
You can specify which values the methods should return. Pass an array with the method name as the key.
If a method returns an object and you want to set values in it, that works too.
It is also possible to pass individual parameters to the object.
Readonly Classes
Readonly classes (PHP 8.2+) are supported. Properties are automatically populated in the constructor — including nested ones.
Abstract Classes
Abstract classes are resolved via existing concrete subclasses. The builder automatically finds a suitable implementation.
Stock Classes (PHP Built-Ins)
Built-in PHP classes like DateInterval, DatePeriod, DateTime, DateTimeImmutable, ReflectionFunction, ArrayObject, and SplFileInfo are automatically supported.
Custom handlers for additional stock classes can be registered:
Value Constraints (with())
The with() method allows setting constraints for value ranges.
Available constraints:
min/max— value range for int and floatmin_length/max_length— string lengthformat— predefined formats:email,url,uuid
Semantic String Detection
The StringBuilder recognizes specific property names and generates matching values:
Custom Type Builders
Custom type builders can be registered for specific data types:
Custom Builder Override
The automatically selected builder can be overridden: