From 4170aca3bedd0fb483d82f890f891adea97d21ff Mon Sep 17 00:00:00 2001 From: Simon Vieille Date: Fri, 21 Feb 2020 15:46:48 +0100 Subject: [PATCH] init --- LICENSE | 24 ++++++++++ README.md | 120 +++++++++++++++++++++++++++++++++++++++++++++++++- composer.json | 16 +++++++ phpunit.xml | 17 +++++++ 4 files changed, 176 insertions(+), 1 deletion(-) create mode 100644 LICENSE create mode 100644 composer.json create mode 100644 phpunit.xml diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..80445fb --- /dev/null +++ b/LICENSE @@ -0,0 +1,24 @@ +Copyright (c) 2014 +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + +* Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + +* Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE +FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + diff --git a/README.md b/README.md index 96a5161..0f80ce1 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,120 @@ -# droneio-test +CSV parser/generator +==================== +![](https://phpci.gitnet.fr/build-status/image/2?branch=master&label=PHPCensor&style=flat-square) + +A simple PHP library to: + +* parse a CSV file +* parse a stream as CSV datas +* generate CSV files. + +PHP >= 7.1 required. + +## Composer installation + +``` +$ composer require deblan/csv "~3" +``` + +Or in your composer.json: + +``` +{ + "require": { + "deblan/csv": "~3" + } +} +``` + +## Usages + +### Generator + +```php +use Deblan\Csv\Csv; + +$csv = new Csv(); + +// Defines the delimiter (default is ;) +$csv->setDelimiter(";"); + +// Defines the enclosure (default is ") +$csv->setEnclosure('"'); + +// Defines the end of line (default is \n) +$csv->setEndOfLine("\n"); + +// Defines the charset (default is UTF-8) +$csv->setCharset("UTF-8"); + +// Add a new line at the end +$csv->addData(['Foo', '$1000')); + +// Add a new line at the end +$csv->appendData(['Bar', '$600']); + +// Add a new line at the beginning +$csv->prependData(['Boo', '$3000']); + +// Defines all the datas +$csv->setDatas([[...], [...]]); + +// Defines the header +$csv->setHeaders(['Product', 'Price']); + +// Rendering +$result = $csv->render(); + +// Rendering to a file +$result = $csv->render('products.csv'); + +// Appending to a file +$result = $csv->render('products.csv', FILE_APPEND); +``` + +### Parse a file + +```php +use Deblan\Csv\CsvParser; + +$csv = new CsvParser(); + +// Defines the delimiter (default is ;) +$csv->setDelimiter(";"); + +// Defines the enclosure (default is ") +$csv->setEnclosure('"'); + +// Defines the end of line (default is \n) +$csv->setEndOfLine("\n"); + +// Headers? +$csv->setHasHeaders(true); + +// Parse a file +$csv->parseFile('products.csv'); + +// Parse a string +$csv->parseString($myString); + +// Headers and datas +$headers = $csv->getHeaders(); +$products = $csv->getDatas(); +``` + +### Parse a stram + +```php +use Deblan\Csv\CsvStreamParser; + +// CsvStreamParser is a CsvParser +$csv = new CsvStreamParser(); + +// Parse a stream +$csv->parseStream(fopen('products.csv', 'r')); + +while ($data = $csv->getData()) { + // ... +} +``` diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..6f2caa8 --- /dev/null +++ b/composer.json @@ -0,0 +1,16 @@ +{ + "name": "deblan/csv", + "description": "A simple PHP library to parse and generate CSV files.", + "license": "BSD-2-Clause", + "authors": [ + { + "name": "Simon Vieille", + "email": "simon@deblan.fr" + } + ], + "autoload": { + "psr-4": { + "Deblan\\Csv\\": "src/Deblan/Csv/" + } + } +} diff --git a/phpunit.xml b/phpunit.xml new file mode 100644 index 0000000..2fd344a --- /dev/null +++ b/phpunit.xml @@ -0,0 +1,17 @@ + + + + + tests/ + + +