Download the PHP package zero-to-prod/data-model-factory without Composer
On this page you can find all versions of the php package zero-to-prod/data-model-factory. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download zero-to-prod/data-model-factory
More information about zero-to-prod/data-model-factory
Files in zero-to-prod/data-model-factory
Package data-model-factory
Short Description Build a Factory for Anything
License MIT
Homepage https://github.com/zero-to-prod/data-model-factory
Informations about the package data-model-factory
Zerotoprod\DataModelFactory
Introduction
This package is a fresh take on how to set the state of your DTOs in a simple and delightful way.
The API is takes some hints from Laravel's Eloquent Factories, but adds some niceties such as setting state via dot syntax and using the set() helper method on the fly.
This package does not require any other dependencies, allowing you to make a factory for anything.
The examples use the DataModel trait, making easier to build your DTOs, but it is not required.
Installation
Install the package via Composer:
Additional Packages
- DataModel: Transform data into a class.
- DataModelHelper: Helpers for a
DataModel
. - Transformable: Transform a
DataModel
into different types.
Usage
This example makes use of the DataModel trait to instantiate the User
class.
You can install the DataModel package like this:
If you don't want to use this trait, you can customize the class instantiation this way.
- Include the Factory trait in your factory class.
- Set the
$model
property to the class you want to instantiate. -
Implement a
definition()
method that returns an array of default values. - NOTE: The
$this->state()
method accepts dot syntax, arrays, or a callback.
Using the set()
Method
You can use the set()
helper method to fluently modify the state of your model in a convenient way.
This is a great way to modify a model without having to implement a method in the factory.
Custom Class Instantiation
To customize instantiation, override the make()
method.