Comments (10)
@pwolanin thanks for using the package.
The change is described on the CHANGELOG in the 9.12.0 release notes.
The change is described in the issue/RFC #498 and implemented with the following PR #499
The change is documented on the documentation website getRecords
The described issue is discussed there too.
Also getRecords
signature has not changed. the docblock always expected a list of string array<string>
and not an hashmap array<array-key, string>
. If you use strings as key it was already an unmaintained behaviour which was not enforced in the code. Now by implementing the full mapping feature, the behavioir is enforced.
from csv.
In my copy of the code at 9.14.0 it has this:
Parameters:
array<array-key, string|int> $header
As the doc for:
\League\Csv\Reader::computeHeader()
Which is the method that throws the exception. So I think there is a problem or unexpected change here.
from csv.
@pwolanin as stated the change is expected and described in various locations of the package.
from csv.
@pwolanin what I may do is maybe update the change and do an array_values
only in case all the keys are strings. This may reduce what your see as being a breaking changes with the following result if your array consist of mixed key (string and integer) an exception would still be thrown.
Would that be acceptable as a mitigation path for you ?
Keep in mind that this mitigation would only work in v9 and will be removed in v10. Whenever that version comes out.
from csv.
@nyamsprod I think that would be helpful to avoid a perceived API change. In my case I can fix the exception by calling array_values() in my code on the header array, but other people may not have as much control.
from csv.
@pwolanin question can you share a snippet on how you submitted a records with an header with string keys ?
Reason I am asking is because the mitigation is breaking more things than expected so I am not confident in implemeting it.
from csv.
So this is in the context of a Drupal CSV migration.
https://git.drupalcode.org/project/migrate_source_csv/-/blob/8.x-3.x/src/Plugin/migrate/source/CSV.php?ref_type=heads#L271
My code is a subclass of CSV.php and overrides the public function initializeIterator() to add some extra logic. Looking now I see there is an array_values() call in CSV.php.
My code does this after constructing the header, and where I just added the array_values() call to fix the exception:
return $this->getGenerator($this->getReader()->getRecords(array_values($header)));
If I condense that logic down, it's basically:
$csv = fopen($path, 'r');
$reader = Reader::createFromStream($csv);
$records = $reader->getRecords($header);
return $this->getGenerator($records);
Perhaps the difference is that the header array is being passed in to getRecords() and you're expecting an empty array coming in for your more common use?
from csv.
yes either an empty array or a array with integer as array-key. I find it strange or uncommon to give header an array with array-key as string.
So adding some mitigation adds some side-effects inside the internal codebase that I do not want to tackle. So I am a bit torn in a sense that I believe the error message is a better way of handling the situation as it
- clearly. state the issue
- give hints to the developer on how to solve it
While the proposed work around adds another type of implicit rule about conversion the user may not be aware of.
Also with the current code, the following is already acceptable
$reader = Reader::createFromString($csv);
dump([...$reader->getRecords(['1' => 'toto', 4 => 'tata', '3' => 'tutu'])]);
As PHP will implicitly convert the string '1'
into an integer.
On the other hand, to me, the following should have failed a long time ago. At least if it use to work, to me, it was a bug:
$reader = Reader::createFromString($csv);
dump([...$reader->getRecords(['toto' => 'toto', 'tata' => 'tata', 'tutu' => 'tutu'])]);
from csv.
Sure, I don't think it's essential to add the mitigation since the fix should usually be easy.
Maybe fix the doc on \League\Csv\Reader::computeHeader()
?
from csv.
doc block improve for internal computeHeader
method
from csv.
Related Issues (20)
- Any Idea why I am getting this error? Please help HOT 4
- Prevent character encoding HOT 2
- PHPStan errors with versions 9.9 and 9.10 HOT 2
- Add CSV column filtering and re-ordering during parsing
- Calls to rewind or fseek that fail are ignored silently HOT 5
- How to read from STDIN instead of a file HOT 2
- Is Writer::insertAll() faster than Writer::insertOne() or not? HOT 2
- how to read csv without BOM? HOT 4
- getRecords from PART of columns and random sort HOT 2
- getRecords with defined header produce wrong results HOT 1
- Add object mapping HOT 12
- Enable protection against long lines HOT 6
- Preformatting data before conversion HOT 3
- TabularDataReader::getRecords()->current() can return NULL HOT 3
- php version is locked to 8.1, it causes issues with php upgrade to 8.2 or greater HOT 1
- Parsing CSV with escape character before enclosure HOT 2
- Feature request: Trim parsed csv strings HOT 2
- Enclosure not works properly HOT 2
- Is there a simple way to format xls into csv before write/read on it
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from csv.