PHP code example of luispastendev / csv-generator

1. Go to this page and download the library: Download luispastendev/csv-generator library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

luispastendev / csv-generator example snippets


use CSVGenerator\CSVGenerator;

// Especificamos la ruta de donde se generara el archivo con ext csv.
// si el archivo ya existe se creara uno nuevo
$file = __DIR__ . '/test.csv';

// clasico
$csv_generator = new CSVGenerator;
$csv_generator->create($file, ['id', 'name', 'company']);
$csv_generator->add([1, 'luis', 'company1']);
$csv_generator->add([2, 'foo', 'company2']);

// chaining functions
(new CSVGenerator)->create($file, ['id', 'name', 'company'])
    ->add([
        [1, 'luis', 'company1'],
        [2, 'foo', 'company2']
    ]);

$file = __DIR__ . '/test.csv';

$generator = new CSVGenerator($file);

// paso a paso
$generator->add([1, 'luis', 'company1']);
$generator->add([2, 'foo', 'company2']);

// en lote
$generator->add([
    [1, 'luis', 'company1'],
    [2, 'foo', 'company2'],
    [3, 'bar', 'company3']
);

$path = __DIR__ . '/test.csv';

(new CSVGenerator)->create($path, [ 
    [1, 'luis', 'company1'],
    // ...
]);

$path = __DIR__ . '/ya-existo.csv';

$generator = new CSVGenerator($path);
$generator->add([1,'foo','bar']);

// o tambien ... 

(new CSVGenerator)->setFile($path)->add([
    [1,'foo','bar'],
    //...
]);

$generator->setFile($path)->add([10, 'php', 'fff']);
$info = $generator->getFileInfo();

// [
//    'filename'  => 'ya-existo.csv',
//    'basename'  => 'ya-existo',
//    'extension' => 'csv',
//    'path'      => /path/dir/
// ]

/**
 * Se encarga de agregar data a un archivo existente regresa
 * falso si ocurrio algun problema
 *
 * @param array $rows 
 * 
 * @return bool
 */

// rows puede ser un array unidimensional o bidi ej: [..] o [[...], [...]]
$obj->add(array $rows);

/**
 * Se encarga de crear un archivo y agregar datos
 *
 * @param string $path
 * @param array $rows
 * @return self
 */

// rows puede ser un array unidimensional o bidi ej: [..] o [[...], [...]]
$obj->create(string $pathfile, array $rows);

/**
 * Establece un archivo para trabajar sobre el.
 *
 * @param string $path
 * @return self
 */

$obj->setFile(string $path);

/**
 * Regresa información del archivo nuevo o existente.
 *
 * @return array
 */

$obj->getFileInfo(string $path);