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
Made str_getcsv a removable formatter #153
Made str_getcsv a removable formatter #153
Conversation
For example this would enable me to do this: $this->writer = Writer::createFromFileObject(new SplTempFileObject())
->clearFormatters()
->addFormatter(function ($row) {
// Formatter to transform every ORM object to array.
if ($row instanceof Model) {
return $row->toArray();
}
return $row;
})
->addFormatter([$transformer, 'transform']);
// Insert all data
$writer->insertOne($transformer->getHeaders());
$writer->insertAll($repository->all()); // This returns a generator that yields every orm object
// When done, make a response or something.
return new Response((string) $writer); |
TL;DR So you are basically enabling
Question/Issue |
You're concerned about BC and rightfully so.
|
I'll make sure to send a PR to adjust this too if you decide to accept this PR: |
But the documentation does states:
So you are indeed introducing a BC on expectations. Also according to the documentation around formatters the signature is function(array $row): array So you are indeed changing a public API signature even if you are relaxing it, so to me this is also a BC break. It may not be obvious but the formatters goals is to format an already created/pushed array. It is not to generate this array to begin with. So here's what I would have done with your example. (I only kept the important parts). function convertModelToArray(Iterator $iterator)
{
foreach ($iterator as $model) {
yield $model->toArray();
}
}
$writer = Writer::createFromFileObject(new SplTempFileObject());
$writer->insertAll(convertModelToArray($repository->all())); There's also a reason behind the current formatter signature. By expecting an Now to be totally honest I do feel that the |
Fair enough 😉
Good suggestion! Very clean and efficient.
The php league's pipeline doesn't have a restriction on what is piped or what one stage might expect, though: https://github.com/thephpleague/pipeline I feel you're not inclined to merge this. I'm ok with this PR being closed. |
To tell the truth I'm torn because the idea is good (the part where you remove str_getcsv was a eye opener to me) but the consequences makes me hesitant :( . Yes the League pipeline is not that strict because it needs to be general purpose I guess. When I adapted it in League/Uri the first thing I did was to enforce typechecking on input and output. So yes I'm gonna reject the PR even if it has great part in it that will definetly be added in futur versions of the lib. Thanks anyway. PS: I see that you live in Belgium just like me, I'm in Brussels!! (damn I should go to more PHP groups in belgium but I always fail to find time for that) |
No problem, it was worth the try. Thanks for your help as well. Hope to see you at this meetup in 2 weeks? |
Like this, users get the option to call
$writer->clearFormatters()
and pass in any type of object and let their custom transformers do the converting to arrays.