2017-25 © Pedro Peláez
 

library csv

OOP wrapper for PHP's csv functions, with column mapping for easy code support.

image

okneloper/csv

OOP wrapper for PHP's csv functions, with column mapping for easy code support.

  • Tuesday, April 25, 2017
  • by okneloper
  • Repository
  • 1 Watchers
  • 0 Stars
  • 508 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 10 Versions
  • 10 % Grown

The README.md

csv

OOP wrapper for PHP's csv functions, with column mapping for easy code maintenance., (*1)

Easy to use and maintain the code using it

The code can be easily adopted to changing requirements and input file formats., (*2)

Consider CSV data of the world's heaviest animals having position, animal and weight in 1 line as column headings., (*3)

<?php
use \Okneloper\Csv\Stream\Input\FileStream;
use \Okneloper\Csv\CsvReader;

// read CSV data from a file
$dataSource = new FileStream($file);

// creata a reader
$csv = new CsvReader($dataSource);

// read rows one by one
while ($row = $csv->read()) {
    echo $row->position; // value for the row in column 0 with header 'position'
    echo $row->animal;   // value for the row in column 1 with header 'animal'
    echo $row->weight;   // value for the row in column 2 with header 'weight'
    echo $row["weight"]; // also supports array syntax 
}

Usage

Use a CSV file as a data source, (*4)

    $dataSource = new \Okneloper\Csv\Stream\Input\FileStream($file);

Or alternatively use a string containing CSV data, (*5)

    $dataSource = new \Okneloper\Csv\Stream\StringStream("position,animal,weight\n1,Blue whale,180 tonnes\n2,African Elephant,6350 kg\n3,Brown Bear,1 ton");

Create a reader using the data source, (*6)

    $csv = new \Okneloper\Csv\CsvReader($dataSource);

Read the data, (*7)

    while ($row = $csv->read()) {
        print_r($row->toArray());
    }

Mapped data can be accessed as array elements:, (*8)

echo $row['position'];

or object properties:, (*9)

echo $row->position;

Column mapping

Mapping to header

By default, the data is mapped to the header row., (*10)

$csv = new \Okneloper\Csv\CsvReader($dataSource);

Outputs, (*11)

Array
(
    [position] => 1
    [animal] => Blue whale
    [weight] => 180 tonnes
)
...

Custom mapping

If you get a new CSV to process every once in a regularly and the column order or names may change, custom mapping helps to deal with it:, (*12)

$csv = new \Okneloper\Csv\CsvReader($dataSource, true, ['Nr', 'Who', 'HowMuch']);
Array
(
    [Nr] => 1
    [Who] => Blue whale
    [HowMuch] => 180 tonnes
)
...



### No header ### ```php $csv = new \Okneloper\Csv\CsvReader($dataSource, false, ['Nr', 'Who', 'HowMuch']);

Array ( [Nr] => position [Who] => animal [HowMuch] => weight ) Array ( [Nr] => 1 [Who] => Blue whale [HowMuch] => 180 tonnes ) ..., (*13)


### No header, no mapping ### ```php $csv = new \Okneloper\Csv\CsvReader($dataSource, false, null);
Array
(
    [0] => position
    [1] => animal
    [2] => weight
)
Array
(
    [0] => 1
    [1] => Blue whale
    [2] => 180 tonnes
)
...

The Versions

25/04 2017

dev-master

9999999-dev https://github.com/okneloper/csv

OOP wrapper for PHP's csv functions, with column mapping for easy code support.

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

by Aleksey Lavrinenko

csv csvreader

25/04 2017

v0.4.3

0.4.3.0 https://github.com/okneloper/csv

OOP wrapper for PHP's csv functions, with column mapping for easy code support.

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

by Aleksey Lavrinenko

csv csvreader

19/02 2017

v0.4.2

0.4.2.0 https://github.com/okneloper/csv

OOP wrapper for PHP's csv functions, with column mapping for easy code support.

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

by Aleksey Lavrinenko

csv csvreader

18/02 2017

v0.4.1

0.4.1.0 https://github.com/okneloper/csv

OOP wrapper for PHP's csv functions, with column mapping for easy code support.

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

by Aleksey Lavrinenko

csv csvreader

18/02 2017

v0.4.0

0.4.0.0 https://github.com/okneloper/csv

OOP wrapper for PHP's csv functions, with column mapping for easy code support.

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

by Aleksey Lavrinenko

csv csvreader

12/12 2016

v0.3.2

0.3.2.0 https://github.com/okneloper/csv

OOP wrapper for PHP's csv functions, with column mapping for easy code support.

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

by Aleksey Lavrinenko

csv csvreader

11/05 2016

v0.3.1

0.3.1.0 https://github.com/okneloper/csv

OOP wrapper for PHP's csv functions, with column mapping for easy code support.

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

by Aleksey Lavrinenko

csv csvreader

07/02 2016

v0.3.0

0.3.0.0 https://github.com/okneloper/csv

OOP wrapper for PHP's csv functions, with column mapping for easy code support.

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

by Aleksey Lavrinenko

csv csvreader

09/11 2015

v0.2.0

0.2.0.0 https://github.com/okneloper/csv

OOP wrapper for PHP's csv functions, with column mapping for easy code support.

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

by Aleksey Lavrinenko

csv csvreader

25/10 2015

v0.1.0

0.1.0.0 https://github.com/okneloper/csv

OOP wrapper for PHP's csv functions, with column mapping for easy code support.

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

by Aleksey Lavrinenko

csv csvreader