Download the PHP package zero-to-prod/factory without Composer
On this page you can find all versions of the php package zero-to-prod/factory. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download zero-to-prod/factory
More information about zero-to-prod/factory
Files in zero-to-prod/factory
Package factory
Short Description A Generic Factory Pattern for Creating Arrays.
License MIT
Homepage https://github.com/zero-to-prod/factory
Informations about the package factory
Zerotoprod\Factory
Contents
- Introduction
- Requirements
- Installation
- Documentation Publishing
- Automatic Documentation Publishing
- Usage
- Basic Factory Pattern
- Model-Based Factory Pattern
- Custom Factory Methods
- Advanced Factory Features
- Callable State (Closures)
- The
set()Method - The
merge()Method - The
context()Method - Context Priority
- The
make()Method
- Local Development
- Contributing
Introduction
A Generic Factory Pattern for Creating Arrays.
Requirements
- PHP 7.1 or higher.
Installation
Install Zerotoprod\Factory via Composer:
This will add the package to your project's dependencies and create an autoloader entry for it.
Documentation Publishing
You can publish this README to your local documentation directory.
This can be useful for providing documentation for AI agents.
This can be done using the included script:
Automatic Documentation Publishing
You can automatically publish documentation by adding the following to your composer.json:
Usage
Basic Factory Pattern
The simplest factory implementation requires only the Factory trait and a definition() method:
Model-Based Factory Pattern
You can add a static factory() method to your model classes for more convenient usage:
Custom Factory Methods
Create custom methods in your factory to provide a fluent, expressive interface for setting specific values. These methods use the state() method internally to modify the factory context.
Advanced Factory Features
For more complex scenarios, you can add state manipulation with closures and other advanced patterns:
Define a factory in this way:
Callable State (Closures)
Use closures to create dynamic state based on the current context. This is powerful for creating values that depend on other values in the factory:
The closure receives the current context as its parameter, allowing you to create dynamic relationships between fields.
The set() Method
The set() method provides a flexible way to modify factory state without creating custom methods. It supports four different syntaxes to accommodate various use cases:
1. Key-Value Syntax
Set a single field by passing the key and value as separate parameters:
2. Array Syntax
Set multiple fields or use associative arrays:
3. Closure Syntax
Use closures for dynamic values based on current context:
4. Dot Notation Syntax
Access and modify nested array values:
Complete Example
Here's how all four syntaxes work together:
The merge() Method
The merge() method allows you to merge new values directly into the factory's current context. This is useful for bulk updates or overriding multiple values at once.
Important: merge() updates the context after set() methods have been evaluated, so closures will use the original values.
Use merge() when you want to:
- Apply bulk changes to multiple fields
- Override previously set values
- Update context without creating new state methods
The context() Method
Use the context() method to get the current context of the factory without creating the final result. This is useful for inspecting or debugging the factory state.
Context Priority
Context can be provided at different stages, with later contexts taking priority:
Priority order (highest to lowest):
- Context passed to
make()- Final override at creation time - Values from
merge()method - Direct context updates - Context from
state()methods - Fluent method calls - Context passed to
factory()- Initial context setup - Default values from
definition()- Base factory defaults
Note: Closures in state() methods are evaluated when the method is called, so they use the context available at that time, before any subsequent merge() operations.
The make() Method
The make() method is the final step that creates your data structure. It can accept optional context that will override any previously set values:
Contributing
Contributions, issues, and feature requests are welcome! Feel free to check the issues page if you want to contribute.
- Fork the repository.
- Create a new branch (
git checkout -b feature-branch). - Commit changes (
git commit -m 'Add some feature'). - Push to the branch (
git push origin feature-branch). - Create a new Pull Request.
All versions of factory with dependencies
zero-to-prod/arr Version ^1.1
zero-to-prod/package-helper Version ^1.1.3
ext-json Version *