It is possible to automatically create data objects in all sorts of forms with this package. Sometimes a little bit more
control is required when a data object is being created. This is where factories come in.
Factories allow you to create data objects like before but allow you to customize the creation process.
For example, we can create a data object using a factory like this:
SongData::factory()->from(['title' => 'Never gonna give you up', 'artist' => 'Rick Astley']);
Collecting a bunch of data objects using a factory can be done as such:
SongData::factory()->collect(Song::all())
##Disable property name mapping
We saw earlier that it is possible to map
property names when creating a data object from an array. This can be disabled when using a factory:
ContractData::factory()->withoutPropertyNameMapping()->from(['name' => 'Rick Astley', 'record_company' => 'RCA Records']);
##Changing the validation strategy
By default, the package will only validate Requests when creating a data object it is possible to change the validation
strategy to always validate for each type:
SongData::factory()->alwaysValidate()->from(['title' => 'Never gonna give you up', 'artist' => 'Rick Astley']);
Or completely disable validation:
SongData::factory()->withoutValidation()->from(['title' => 'Never gonna give you up', 'artist' => 'Rick Astley']);
##Disabling magic methods
A data object can be created
using magic methods , this can be disabled
when using a factory:
SongData::factory()->withoutMagicalCreation()->from('Never gonna give you up');
It is also possible to ignore the magical creation methods when creating a data object as such:
SongData::factory()->ignoreMagicalMethod('fromString')->from('Never gonna give you up');
##Adding additional global casts
When creating a data object, it is possible to add additional casts to the data object:
SongData::factory()->withCast('string', StringToUpperCast::class)->from(['title' => 'Never gonna give you up', 'artist' => 'Rick Astley']);
These casts will not replace the other global casts defined in the data.php
config file, they will though run before
the other global casts. You define them just like you would define them in the config file, the first parameter is the
type of the property that should be cast and the second parameter is the cast class.
##Using the creation context
Internally the package uses a creation context to create data objects. The factory allows you to use this context manually, but when using the from method it will be used automatically.
It is possible to inject the creation context into a magical method by adding it as a parameter:
class SongData extends Data
{
public function __construct(
public string $title,
public string $artist,
) {
}
public static function fromModel(Song $song, CreationContext $context): self
{
}
}
You can read more about creation contexts here.