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

getHeaders() #52

Merged
merged 7 commits into from
Jan 18, 2021
Merged

getHeaders() #52

merged 7 commits into from
Jan 18, 2021

Conversation

AdamGaskins
Copy link
Contributor

PR for #51.

Love this package, and thanks for all the value you've added to the community (Laravel Beyond Crud has had a huge impact on our company 🙌🏻 ).

Copy link
Member

@freekmurze freekmurze left a comment

Choose a reason for hiding this comment

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

Let's make the code a bit simpler!

@AdamGaskins
Copy link
Contributor Author

My understanding is the spreadsheet needs to be opened and the headers processed after any of the fluent methods are called (useDelimiter(), trimHeaderRow() etc.) but before either of getRows() or getHeaders() is called (e.g. we have a page on our site that opens the spreadsheet solely to read the headers for mapping purposes). Extracting this processing to its own method seemed to make the most sense to me, and also avoided getHeaders() needing to rewind the iterator. Let me know your thoughts!

@freekmurze
Copy link
Member

I think the clearest things to do, would be to have getHeaders to open the file and have it's own iterator independent of the getRows function. This would you won't need to keep a boolean tracking whether things have been processed or not.

I really don't mind that the file is opened twice, the performance impact will probably be negligible. Typing this message likely takes more time 🙂

@@ -154,6 +161,31 @@ public function getRows(): LazyCollection
});
}

public function getHeaders(): ?array
{
if ($this->processHeader && ! $this->headers) {
Copy link
Member

Choose a reason for hiding this comment

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

There's a big piece of indented code here. I can be simplified by first checking if headers is set to something. If it is, we can already return the headers.

@freekmurze freekmurze merged commit 1c4a51a into spatie:master Jan 18, 2021
@freekmurze
Copy link
Member

Thank you!

@avdb
Copy link
Contributor

avdb commented Mar 18, 2021

Is there a reason why $reader is created in the getHeaders()?
I think you cannot set the delimiter for the header row of csv files how it's now implemented but maybe I'm missing something!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants