Open nikophil opened 8 months ago
Does this work?
// BlogPostFactory
public function defaults(): array
{
return [
'tags' => []
];
}
public function taggedWithPhp(): static
{
return $this->beforeInstantiate(function(array $attributes) {
$attribtues['tags'][] = 'php';
return $attributes;
});
}
public function taggedWithSymfony(): static
{
return $this->beforeInstantiate(function(array $attributes) {
$attribtues['tags'][] = 'symfony';
return $attributes;
});
}
If so, this feels like the best method to give complete control. We could make a helper but, like you say, do we deep merge?
From time to time there is the need to "merge" properties.
Given the following code:
It is not possible have a nice fluent api to have both tags
php
andsymfony
. I could do something likeBlogPostFactory::createOne(['tags' => ['php', 'symfony']])
, but real world examples are much less naive :)Another example would be with a bitmask:
I think there could be several solutions to mitigate this problem:
Factory::$attributeSet
protected, and let the user deal with it "at his own risk"Factory::mergeAttributes(array)
, but this may come at the cost of some complexity (should we "deeply" merge array, objects, factories, etc... ?Factory::resolveAttribute(string)
which could be used in some cases to access the state of a given attribute.not sure what would be the best solution :thinking: