2017-25 © Pedro Peláez
 

library pop-csv

Pop CSV Component for Pop PHP Framework

image

popphp/pop-csv

Pop CSV Component for Pop PHP Framework

  • Monday, January 29, 2018
  • by nicksagona
  • Repository
  • 1 Watchers
  • 1 Stars
  • 479 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 4 Versions
  • 11 % Grown

The README.md

pop-csv

Build Status Coverage Status, (*1)

Join the chat at https://discord.gg/TZjgT74U7E, (*2)

Overview

pop-csv provides a streamlined way to work with PHP data and the CSV format., (*3)

It is a component of the Pop PHP Framework., (*4)

Install

Install pop-csv using Composer., (*5)

composer require popphp/pop-csv

Or, require it in your composer.json file, (*6)

"require": {
    "popphp/pop-csv" : "^4.2.0"
}

Top, (*7)

Quickstart

Create a CSV string

$phpData = [
    [
        'first_name' => 'Bob',
        'last_name'  => 'Smith'
    ],
    [
        'first_name' => 'Jane',
        'last_name'  => 'Smith'
    ]
];

$data      = new Pop\Csv\Csv($phpData);
$csvString = $data->serialize();

The $csvString variable now contains:, (*8)

first_name,last_name
Bob,Smith
Jane,Smith

Create data from a CSV string

You can either pass the data object a direct string of serialized data or a file containing a string of serialized data. It will detect which one it is and parse it accordingly., (*9)

$csv     = new Pop\Csv\Csv($csvString);
$phpData = $csv->unserialize();

Top, (*10)

Options

Where serializing or unserializing CSV data, there are a set of options available to tailor the process:, (*11)

$options = [
    'exclude'   => ['id'],    // An array of fields to exclude
    'include'   => ['email'], // An array of fields to explicitly include, omitting all others
    'delimiter' => ',',       // Delimiter defaults to ',' - could be "\t" or something else
    'enclosure' => '"',       // Default string enclosure, i.e. "my data","other data"
    'escape'    => '"',       // String character to escape in the data, i.e. "my ""data"" here"
    'fields'    => true,      // Include the field names in the first row 
    'newline'   => true,      // Allow newlines in a data cell. Set as false to trim them
    'limit'     => 0,         // Character limit of a data cell. 0 means no limit
    'map'       => [],        // Array key of a single array value to map to the data cell value
    'columns'   => [],        // Array key of a multidimensional array value to map and join into the data cell value
];

Map/Columns Example, (*12)

$users = [
    'id'       => 1,
    'username' => 'testuser',
    'country'  => [
        'name' => 'United States',
        'code' => 'US'
    ],
    'roles'    => [
        ['id' => 1, 'name' => 'Admin'],
        ['id' => 2, 'name' => 'Staff'],
    ]
];
$options = [
    'map' => [
        'country' => 'code'
    ],
    'columns' => [
        'roles' => 'name'
    ]
]

Pass the options array to constructor method:, (*13)

$data      = new Pop\Csv\Csv($users, $options);
$csvString = $data->serialize();
echo $csvString;

The above will output the following CSV data:, (*14)

id,username,country,roles
1,testuser,US,"Admin,Staff"

Top, (*15)

Output CSV

Write to File

$phpData = [
    [
        'first_name' => 'Bob',
        'last_name'  => 'Smith'
    ],
    [
        'first_name' => 'Jane',
        'last_name'  => 'Smith'
    ]
];

$data = new Pop\Csv\Csv($phpData);
$data->writeToFile('/path/to/file.csv');

Output to HTTP

$phpData = [
    [
        'first_name' => 'Bob',
        'last_name'  => 'Smith'
    ],
    [
        'first_name' => 'Jane',
        'last_name'  => 'Smith'
    ]
];

$data = new Pop\Csv\Csv($phpData);
$data->outputToHttp('my-file.csv');
Force download of file

Pass a true boolean as the second parameter, which forces attachment for the Content-Disposition header., (*16)

$data->outputToHttp('my-file.csv', true);
Additional HTTP headers

Additional HTTP headers can be passed to the third parameter:, (*17)

$data->outputToHttp('my-file.csv', false, ['X-Header' => 'some-header-value']);

Top, (*18)

Append Data

In the case of working with large data sets, you can append CSV data to an existing file on disk. This prevents loading large amounts of data into memory that may exceed the PHP environment's limits., (*19)

us Pop\Csv\Csv;

$phpData = [
    [
        'first_name' => 'Bob',
        'last_name'  => 'Smith'
    ],
    [
        'first_name' => 'Jane',
        'last_name'  => 'Smith'
    ]
];

Csv::appendDataToFile('my-file.csv', $data);

Top, (*20)

The Versions

29/01 2018

dev-master

9999999-dev http://www.popphp.org/

Pop CSV Component for Pop PHP Framework

  Sources   Download

BSD-3-Clause New BSD

The Requires

  • php >=5.6.0

 

The Development Requires

csv php pop pop php

29/01 2018

dev-v3-dev

dev-v3-dev http://www.popphp.org/

Pop CSV Component for Pop PHP Framework

  Sources   Download

BSD-3-Clause New BSD

The Requires

  • php >=5.6.0

 

The Development Requires

csv php pop pop php

29/01 2018

3.0.1

3.0.1.0 http://www.popphp.org/

Pop CSV Component for Pop PHP Framework

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.6.0

 

The Development Requires

csv php pop pop php

22/02 2017

3.0.0

3.0.0.0 http://www.popphp.org/

Pop CSV Component for Pop PHP Framework

  Sources   Download

New BSD

The Requires

  • php >=5.6.0

 

The Development Requires

csv php pop pop php