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
Contents
- Introduction
- Requirements
- Installation
- Additional Packages
- Usage
- Custom Class Instantiation
- The
set()
Method - The
merge()
Method - The
context()
Method
- Local Development
- Contributing
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.
Requirements
- PHP 7.1 or higher.
Installation
Install the package via Composer:
This will add the package to your project’s dependencies and create an autoloader entry for it.
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.
Custom Class Instantiation
To customize instantiation, override the make()
method.
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.
The merge()
Method
Sometimes it is useful to merge new values into the current context of the factory.
Use the merge()
method to merge any new values and update the factory context.
The context()
Method
Use the context()
method to get the context of the factory.
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.