Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: introduce distribute() method #826

Merged

Conversation

nikophil
Copy link
Member

@nikophil nikophil commented Feb 22, 2025

I wanted since a lot of time improve a little bit the API around collections, to facilitate complex cases with one-to-many, mainly to be used with sequences.

here is one new method:

  • FactoryCollection::distribute($field, $values): it can be use to distribute the values to all factories inside the collection for a given field:
$categories = CatagoryFactory::createSequence(2, [['name' => 'category 1'], ['name' => 'category 2']]);
$contacts = ContactFactory::new()->many(2)->forEach('category', $categories)->create();

@nikophil nikophil requested a review from kbond February 22, 2025 17:22
Copy link
Member

@kbond kbond left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks cool. Admittedly, I find it hard to reason about. I'd love to see some concrete examples in the docs to help visualize the end result (one with relations).

src/Factory.php Outdated
*
* @return list<T>
*/
final public static function createForEach(string $field, array $values): array
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think this is common enough to warrant a dedicated static method?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nope you're right, I'll remove it

@nikophil
Copy link
Member Author

This looks cool.

nice :)

Admittedly, I find it hard to reason about.

yeah, this might be for some edge cases only

I'd love to see some concrete examples in the docs to help visualize the end result (one with relations).

I'll add concrete examples in the docs. I have plenty of cases where this could be useful in the project I'm working on

I'm still looking for better names for the first one 🤔
maybe spread('field', $values) ?

@kbond
Copy link
Member

kbond commented Feb 24, 2025

Oo I do like spread. Could mapEach be combined into forEach? spread(string, array|callable)?

@nikophil
Copy link
Member Author

Oo I do like spread

let's go for spread() then :)

Could mapEach be combined into forEach? spread(string, array|callable)?

not sure what you mean:

  • in the current forEach() (which I will rename to spread()), we take a field, and the second parameter is an array which is spread in each element of the collection for the given field.
  • in mapEach(), we give a callable which will be called on each element of the collection, but we will pass to the callable a different parameter for each element.

@kbond
Copy link
Member

kbond commented Feb 24, 2025

My bad, I misread the signature for mapEach. Does it make sense for this one to include "spread" in the name?

@nikophil
Copy link
Member Author

Not sure... we actually "spread" the values to a callable which will be used in a array_map()

I'm wondering if mapForEachValue() would be better?

@kbond
Copy link
Member

kbond commented Feb 24, 2025

Got it.

I'm wondering if mapForEachValue() would be better?

Feels like a bit of a mouthful. Let's keep this one as is

@smnandre
Copy link
Contributor

I love the feature (and we needed exactly this on the work project :))

But yes we can do better for the API :)

$contacts = ContactFactory::new()->many(2)->forEach('category', $categories)->create();

The many->forEach(singular, plural)->create() really is not a good friend of my brain

You suggest to use spread then

which is spread in each element of the collection for the given field.

... and i'm not sure it really is perfect either...

Iwould suggest .... (and taken from the PR comment)

distribute()

@nikophil
Copy link
Member Author

hi @smnandre

thanks for this proposal, distribute() would be a perfect naming indeed 👍

@nikophil nikophil force-pushed the feat/factory-collection-improvments branch from 95a5d18 to 4a5e003 Compare February 25, 2025 07:50
@nikophil nikophil changed the title feat: introduce forEach() and mapEach() method feat: introduce distribute() method Feb 25, 2025
@nikophil nikophil force-pushed the feat/factory-collection-improvments branch from 4a5e003 to 439d9ee Compare February 25, 2025 08:03
@nikophil
Copy link
Member Author

ok, I've only kept the distribute() method in this PR. I'll create another PR for the other method, with which I'm not 100% happy.

I've added some docs for the distrubute() method @kbond @smnandre does that make sense to you?

@nikophil nikophil force-pushed the feat/factory-collection-improvments branch 2 times, most recently from 6a5fcde to 4da79cc Compare February 25, 2025 08:17
Copy link
Member

@kbond kbond left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call to update this PR to just distribute().

{
$factories = $this->all();

if (\count($factories) !== \count($values)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is perfect for a start.

I wonder if we could allow some options there in the future

A couple of ideas:

Cycle

categories : A, B
posts: P1, P2, P3

P1: A
P2: B
P3: A

All

categories : A, B, C
posts: P1, P2

P1: A, C
P2: B

Ignore missing

categories : A, B
posts: P1, P2, P3

P1: A
P2: B
P3: 

Copy link
Member Author

@nikophil nikophil Feb 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, we can totally pass a third parameter $strategy or something like that! Let's see what kind of use case will be needed

@nikophil nikophil force-pushed the feat/factory-collection-improvments branch from 4da79cc to b3f8b40 Compare February 26, 2025 17:29
@nikophil nikophil merged commit d15de0e into zenstruck:2.x Feb 26, 2025
67 checks passed
@nikophil nikophil deleted the feat/factory-collection-improvments branch February 26, 2025 21:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants