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

Ability to set custom headers (whether or not headers exist in the file) #103

Merged
merged 6 commits into from
Sep 2, 2022

Conversation

kitbs
Copy link

@kitbs kitbs commented Sep 1, 2022

If you read a CSV file which does not contain a header row, you will use noHeaderRow(), which returns each data row with integer keys.

For example, this file:

john@example.com,john,doe
mary-jane@example.com,mary jane,doe

will be returned as:

[
    [
        0 => 'john@example.com',
        1 => 'john',
        2 => 'doe',
    ],
    [
        0 => 'mary-jane@example.com',
        1 => 'mary jane',
        2 => 'doe',
    ],
]

However, if you already know what the headers should be for the file (i.e. they are defined externally), it would be nice to be able to get each data row with the correct headers.

For example, you know the headers should be email, first_name and last_name, so you want the above file to be returned as:

[
    [
        'email' => 'john@example.com',
        'first_name' => 'john',
        'last_name' => 'doe',
    ],
    [
        'email' => 'mary-jane@example.com',
        'first_name' => 'mary jane',
        'last_name' => 'doe',
    ],
]

In order to do this currently, you would need to reimplement all the array_slice/array_combine logic from getValueFromRow() in your own code.

To support this, I have added a setHeaders(array $headers) method, where you can specify the custom headers to use. It affects the output of getValueFromRow() and therefore getRows(), so that the custom headers are used instead of integer keys.

It can either be used with noHeaderRow() (custom headers will be used instead of integer keys), or without (custom headers will be used instead of the headers defined in the file, and the header row will not be returned as a data row). It is also compatible with headerOnRow() (previous rows will be skipped, and custom headers will be used instead of headers defined in the file).

It also affects the output of getHeaders() by returning only the custom headers you have defined, so the new getOriginalHeaders() method can be used to get the header row actually defined in the file, if required.

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.

Looks good, could you rename that function? After that we're good to go. I appreciate the updated tests and readme 👍

README.md Outdated Show resolved Hide resolved
@kitbs kitbs requested a review from freekmurze September 2, 2022 09:31
@freekmurze freekmurze merged commit c5048f5 into spatie:main Sep 2, 2022
@freekmurze
Copy link
Member

Thank you!

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.

2 participants