Skip to content

Commit

Permalink
from -> read
Browse files Browse the repository at this point in the history
  • Loading branch information
lpheller committed Nov 4, 2023
1 parent 35ba409 commit 3cf05f8
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 31 deletions.
10 changes: 5 additions & 5 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')->toArray();
$csv = Csv::read('filepath.csv')->toArray();

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

### Header mapping
Expand All @@ -28,7 +28,7 @@ using the `mapToHeader` function makes handling the data much easier.
```php
use Heller\SimpleCsv\Csv;

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

Expand All @@ -43,7 +43,7 @@ Sometimes it can be helpful to skip certain rows or columns.
You can do that using `skipRows` and `skipColumns` methods.

```php
$csv = Csv::from('filepath.csv')
$csv = Csv::read('filepath.csv')
->skipRows(1)
->skipColumns([2, 4, 'columnname'])
->toArray();
Expand All @@ -59,7 +59,7 @@ As the csv data is just array, we can of course filter the data however you like
For convenience we can also filter while collecting the data using the filter() callback.

```php
$csv = Csv::from('filepath.csv')
$csv = Csv::read('filepath.csv')
->filter(fn($row) => $row['column'] != 'foo')
->toArray();
```
Expand Down
2 changes: 1 addition & 1 deletion src/Csv.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function __construct(string $filePath)
* @param string $filePath The path or url to the CSV file
* @return $this
*/
public static function from(string $filePath)
public static function read(string $filePath)
{
return new self($filePath);
}
Expand Down
50 changes: 25 additions & 25 deletions tests/Unit/CsvTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

$file = __DIR__.'/../Fixtures/data.csv';

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

expect($csv->toArray())->toBe([
[
Expand All @@ -22,7 +22,7 @@

$file = __DIR__.'/../Fixtures/data_delimiter.csv';

$csv = Csv::from($file)
$csv = Csv::read($file)
->delimiter(';');

expect($csv->toArray())->toBe([
Expand All @@ -39,7 +39,7 @@

$file = __DIR__.'/../Fixtures/data.csv';

$csv = Csv::from($file)
$csv = Csv::read($file)
->mapToHeaders();

expect($csv->toArray())->toBe([
Expand All @@ -55,7 +55,7 @@

$file = __DIR__.'/../Fixtures/data_headers.csv';

$csv = Csv::from($file)
$csv = Csv::read($file)
->mapToHeaders(2);

expect($csv->getHeaderRow())->toBe([
Expand Down Expand Up @@ -87,7 +87,7 @@

$file = __DIR__.'/../Fixtures/data.csv';

$csv = Csv::from($file)
$csv = Csv::read($file)
->skipRows(1);

expect($csv->toArray())->toBe([
Expand All @@ -101,7 +101,7 @@

$file = __DIR__.'/../Fixtures/data.csv';

$csv = Csv::from($file)
$csv = Csv::read($file)
->skipColumns(1);

expect($csv->toArray())->toBe([
Expand All @@ -114,7 +114,7 @@

$file = __DIR__.'/../Fixtures/data.csv';

$csv = Csv::from($file)
$csv = Csv::read($file)
->skipColumns(['Foo']);

expect($csv->toArray())->toBe([
Expand All @@ -127,7 +127,7 @@

$file = __DIR__.'/../Fixtures/data.csv';

$csv = Csv::from($file)
$csv = Csv::read($file)
->skipColumns(['Foo', 1]);

expect($csv->toArray())->toBe([
Expand All @@ -140,7 +140,7 @@

$file = __DIR__.'/../Fixtures/data.csv';

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

expect($csv->getHeaderRow())->toBe([
'Foo', 'Bar', 'Baz',
Expand All @@ -153,7 +153,7 @@
$file = 'https://docs.google.com/spreadsheets/d/e/2PACX-1vQbCA0PYQtwEDF2g4rv3-22vUpoBaNaYWFNW3wR0s0a904D-9vRfmIkNzA7VmKDArfGY81whg9tWhWp/pub?gid=103922319&single=true&output=csv';

// $file = __DIR__.'/../Fixtures/data_10krows.csv';
$csv = Csv::from($file)->mapToHeaders();
$csv = Csv::read($file)->mapToHeaders();

expect(count($csv->toArray()))->toBeGreaterThan(0);
// ray()->measure();
Expand All @@ -163,7 +163,7 @@
test('It works with a plain google spreadhset url', function () {
$url = 'https://docs.google.com/spreadsheets/d/1F4yuxvNYcBD_91MFQdgWqxXPQL1HC3PP_JOnN1Mizr0';

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

expect($csv[0])->toEqual(
[
Expand All @@ -177,7 +177,7 @@
$file = __DIR__.'/../Fixtures/data_10krows.csv';
// $file = __DIR__.'/../Fixtures/data.csv';

$csv = Csv::from($file)
$csv = Csv::read($file)
->mapToHeaders()
->filter(function ($row) {
return $row['Foo'] === 'Foo1';
Expand All @@ -196,13 +196,13 @@

$file = __DIR__.'/../Fixtures/data_10krows.csv';

$csv = Csv::from($file);
$csv = Csv::read($file);
expect($csv->count())->toBe(10000);

$csv = Csv::from($file)->mapToHeaders();
$csv = Csv::read($file)->mapToHeaders();
expect($csv->count())->toBe(9999);

$csv = Csv::from($file)
$csv = Csv::read($file)
->mapToHeaders()
->filter(function ($row) {
return $row['Foo'] === 'Foo1';
Expand All @@ -214,7 +214,7 @@

$file = __DIR__.'/../Fixtures/data_10krows.csv';

$csv = Csv::from($file)
$csv = Csv::read($file)
->mapToHeaders()
->mapToObject();

Expand All @@ -238,7 +238,7 @@ class TestRow
public $Baz;
}

$csv = Csv::from($file)
$csv = Csv::read($file)
->mapToHeaders()
->mapToObject(TestRow::class);

Expand All @@ -252,7 +252,7 @@ class TestRow

$file = __DIR__.'/../Fixtures/data.csv';

$csv = Csv::from($file)
$csv = Csv::read($file)
->mapToHeaders()
->filter(function ($row) {
expect($row)->toBeInstanceOf(stdClass::class);
Expand All @@ -274,7 +274,7 @@ class TestRow

$file = __DIR__.'/../Fixtures/data_bad_headers.csv';

$csv = Csv::from($file)
$csv = Csv::read($file)
->mapToHeaders()
->mapToObject();

Expand All @@ -290,7 +290,7 @@ class TestRow
// works at least up to 10m rows
$file = makeTestFile(100000);

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

$i = 0;

Expand Down Expand Up @@ -331,7 +331,7 @@ function makeTestFile($rows = 1000)
test('It returns json data', function () {
$file = makeTestFile(2);

$csv = Csv::from($file)
$csv = Csv::read($file)
->mapToHeaders();

expect($csv->toJson())->toBe('[{"Foo":"Foo0","Bar":"Bar0","Baz":"Baz0"}]');
Expand All @@ -343,7 +343,7 @@ function makeTestFile($rows = 1000)

$file = makeTestFile(2);

$csv = Csv::from($file)
$csv = Csv::read($file)
->mapToHeaders([
'fii',
'foo',
Expand All @@ -368,16 +368,16 @@ function makeTestFile($rows = 1000)
});

test('bla', function () {
$csv = Csv::from('https://docs.google.com/spreadsheets/d/1QDrRXsfvu4YpRvVK6DT-bj8EO3tpBl6WFCGBjSMZJgg/edit?pli=1#gid=0');
$csv = Csv::read('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]);
// 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)
$csv = Csv::read($file)
->mapToHeaders()
->skipEmptyRows();

Expand Down

0 comments on commit 3cf05f8

Please sign in to comment.