Skip to content

Commit

Permalink
rename get() to toArray()
Browse files Browse the repository at this point in the history
  • Loading branch information
lpheller committed Nov 4, 2023
1 parent 890f9d2 commit 35ba409
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 26 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ composer require heller/simple-csv
```php
use Heller\SimpleCsv\Csv;

$csv = Csv::from('filepath.csv')->get();
$csv = Csv::from('filepath.csv')->toArray();

$csv = Csv::from('http://urlto.csv')->get();
$csv = Csv::from('http://urlto.csv')->toArray();
```

### Header mapping
Expand All @@ -30,7 +30,7 @@ use Heller\SimpleCsv\Csv;

$csv = Csv::from('filepath.csv')
->mapToHeaders()
->get();
->toArray();

foreach($csv as $row){
echo $row['columnname']; // instead of using $row[3]
Expand All @@ -46,7 +46,7 @@ You can do that using `skipRows` and `skipColumns` methods.
$csv = Csv::from('filepath.csv')
->skipRows(1)
->skipColumns([2, 4, 'columnname'])
->get();
->toArray();
```

Both methods accept either an `int` for a certain row / column or `array` to skip
Expand All @@ -61,7 +61,7 @@ For convenience we can also filter while collecting the data using the filter()
```php
$csv = Csv::from('filepath.csv')
->filter(fn($row) => $row['column'] != 'foo')
->get();
->toArray();
```

### Map to object
Expand All @@ -80,7 +80,7 @@ $csv->mapToObject(CsvRow::class)
->filter(function(CsvRow $item){
return $item->isValid();
})
->get();
->toArray();

```

Expand Down
4 changes: 2 additions & 2 deletions src/Csv.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ public function count()
*
* @return array
*/
public function get()
public function toArray()
{
$data = [];
foreach ($this->processor->process() as $row) {
Expand Down Expand Up @@ -212,6 +212,6 @@ public function skipEmptyRows($shouldSkip = true)
*/
public function toJson()
{
return json_encode($this->get(), JSON_THROW_ON_ERROR);
return json_encode($this->toArray(), JSON_THROW_ON_ERROR);
}
}
44 changes: 26 additions & 18 deletions tests/Unit/CsvTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

$csv = Csv::from($file);

expect($csv->get())->toBe([
expect($csv->toArray())->toBe([
[
'Foo', 'Bar', 'Baz',
],
Expand All @@ -25,7 +25,7 @@
$csv = Csv::from($file)
->delimiter(';');

expect($csv->get())->toBe([
expect($csv->toArray())->toBe([
[
'Foo', 'Bar', 'Baz',
],
Expand All @@ -42,7 +42,7 @@
$csv = Csv::from($file)
->mapToHeaders();

expect($csv->get())->toBe([
expect($csv->toArray())->toBe([
[
'Foo' => 'Foo1',
'Bar' => 'Bar1',
Expand All @@ -64,7 +64,7 @@
'Baz',
]);

expect($csv->get())->toBe([
expect($csv->toArray())->toBe([
[
'Foo' => 'Bla',
'Bar' => 'Bla',
Expand All @@ -90,7 +90,7 @@
$csv = Csv::from($file)
->skipRows(1);

expect($csv->get())->toBe([
expect($csv->toArray())->toBe([
[
'Foo1', 'Bar1', 'Baz1',
],
Expand All @@ -104,7 +104,7 @@
$csv = Csv::from($file)
->skipColumns(1);

expect($csv->get())->toBe([
expect($csv->toArray())->toBe([
['Bar', 'Baz'],
['Bar1', 'Baz1'],
]);
Expand All @@ -117,7 +117,7 @@
$csv = Csv::from($file)
->skipColumns(['Foo']);

expect($csv->get())->toBe([
expect($csv->toArray())->toBe([
['Bar', 'Baz'],
['Bar1', 'Baz1'],
]);
Expand All @@ -130,7 +130,7 @@
$csv = Csv::from($file)
->skipColumns(['Foo', 1]);

expect($csv->get())->toBe([
expect($csv->toArray())->toBe([
['Baz'],
['Baz1'],
]);
Expand All @@ -155,15 +155,15 @@
// $file = __DIR__.'/../Fixtures/data_10krows.csv';
$csv = Csv::from($file)->mapToHeaders();

expect(count($csv->get()))->toBeGreaterThan(0);
expect(count($csv->toArray()))->toBeGreaterThan(0);
// ray()->measure();

});

test('It works with a plain google spreadhset url', function () {
$url = 'https://docs.google.com/spreadsheets/d/1F4yuxvNYcBD_91MFQdgWqxXPQL1HC3PP_JOnN1Mizr0';

$csv = Csv::from($url)->get();
$csv = Csv::from($url)->toArray();

expect($csv[0])->toEqual(
[
Expand All @@ -183,7 +183,7 @@
return $row['Foo'] === 'Foo1';
});

expect($csv->get())->toBe([
expect($csv->toArray())->toBe([
[
'Foo' => 'Foo1',
'Bar' => 'Bar1',
Expand Down Expand Up @@ -218,8 +218,8 @@
->mapToHeaders()
->mapToObject();

expect($csv->get()[0])->toBeInstanceOf(stdClass::class);
expect($csv->get()[0])->toEqual((object) [
expect($csv->toArray()[0])->toBeInstanceOf(stdClass::class);
expect($csv->toArray()[0])->toEqual((object) [
'foo' => 'Foo1',
'bar' => 'Bar1',
'baz' => 'Baz1',
Expand All @@ -242,7 +242,7 @@ class TestRow
->mapToHeaders()
->mapToObject(TestRow::class);

expect($csv->get())->each(function ($row) {
expect($csv->toArray())->each(function ($row) {
expect($row->value)->toBeInstanceOf(TestRow::class);
});

Expand All @@ -261,7 +261,7 @@ class TestRow
})
->mapToObject();

expect($csv->get())->toEqual([
expect($csv->toArray())->toEqual([
(object) [
'foo' => 'Foo1',
'bar' => 'Bar1',
Expand All @@ -278,8 +278,8 @@ class TestRow
->mapToHeaders()
->mapToObject();

expect($csv->get()[0])->toBeInstanceOf(stdClass::class);
expect($csv->get()[0])->toEqual((object) [
expect($csv->toArray()[0])->toBeInstanceOf(stdClass::class);
expect($csv->toArray()[0])->toEqual((object) [
'foo' => 'Foo1',
'bar_ito' => 'Bar1',
'baz' => 'Baz1',
Expand Down Expand Up @@ -349,7 +349,7 @@ function makeTestFile($rows = 1000)
'foo',
'faa',
])
->get();
->toArray();

expect($csv)->toBe([
[
Expand All @@ -367,6 +367,14 @@ function makeTestFile($rows = 1000)
unlink($file);
});

test('bla', function () {
$csv = Csv::from('https://docs.google.com/spreadsheets/d/1QDrRXsfvu4YpRvVK6DT-bj8EO3tpBl6WFCGBjSMZJgg/edit?pli=1#gid=0');
$csv->mapToHeaders();
$csv->skipEmptyRows();
dd($csv->filter(fn ($item) => $item)->toArray()[827]);

});

test('it skips empty rows by default', function () {
$file = __DIR__.'/../Fixtures/data_with_empty_rows.csv';
$csv = Csv::from($file)
Expand Down

0 comments on commit 35ba409

Please sign in to comment.