Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
463ce74
commit a437b4e
Showing
11 changed files
with
360 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
tests/ export-ignore | ||
phpunit.xml export-ignore | ||
.travis.yml export-ignore |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
composer.lock | ||
public | ||
vendor | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
before_script: | ||
- composer self-update | ||
- composer install | ||
language: php | ||
branches: | ||
only: | ||
- master | ||
php: | ||
- 5.6 | ||
- 7.0 | ||
script: | ||
- ./vendor/bin/phpunit | ||
notifications: | ||
email: false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
New BSD License | ||
|
||
Copyright (c) 2009-2016, NOLA Interactive, LLC. | ||
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. | ||
* Neither the name of NOLA Interactive, LLC, nor the names of its | ||
contributors may be used to endorse or promote products derived | ||
from this software without specific prior written permission. | ||
|
||
THIS SOFTWARE IS PROVIDED BY NOLA INTERACTIVE, LLC, ''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 NOLA INTERACTIVE, LLC 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. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
# pop-csv | ||
Pop CSV Component | ||
pop-csv | ||
======= | ||
|
||
Version 3, in development |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
{ | ||
"name": "popphp/pop-csv", | ||
"description": "Pop CSV Component for Pop PHP Framework", | ||
"keywords": [ | ||
"csv", | ||
"php", | ||
"pop", | ||
"pop php" | ||
], | ||
"homepage": "http://www.popphp.org/", | ||
"license": "New BSD", | ||
"authors": [ | ||
{ | ||
"name": "Nick Sagona", | ||
"email": "dev@nolainteractive.com", | ||
"homepage": "http://www.nolainteractive.com/" | ||
} | ||
], | ||
"require": { | ||
"php": ">=7.0.0" | ||
}, | ||
"require-dev": { | ||
"phpunit/phpunit": "5.5.*" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"Pop\\Csv\\": "src/" | ||
} | ||
}, | ||
"autoload-dev": { | ||
"psr-4": { | ||
"Pop\\Csv\\Test\\": "tests/" | ||
} | ||
}, | ||
"extra": { | ||
"branch-alias": { | ||
"dev-master": "3.0.x-dev" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<phpunit bootstrap="vendor/autoload.php"> | ||
<testsuites> | ||
<testsuite name="Pop Data Component PHPUnit Test"> | ||
<directory>tests</directory> | ||
</testsuite> | ||
</testsuites> | ||
<filter> | ||
<whitelist addUncoveredFilesFromWhitelist="true"> | ||
<directory suffix=".php">src</directory> | ||
</whitelist> | ||
</filter> | ||
<logging> | ||
<log type="coverage-html" target="/tmp/pop-data-cc" charset="UTF-8" | ||
yui="true" highlight="false" showUncoveredFiles="true" | ||
lowUpperBound="35" highLowerBound="70" /> | ||
</logging> | ||
</phpunit> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,184 @@ | ||
<?php | ||
/** | ||
* Pop PHP Framework (http://www.popphp.org/) | ||
* | ||
* @link https://github.com/popphp/popphp-framework | ||
* @author Nick Sagona, III <dev@nolainteractive.com> | ||
* @copyright Copyright (c) 2009-2016 NOLA Interactive, LLC. (http://www.nolainteractive.com) | ||
* @license http://www.popphp.org/license New BSD License | ||
*/ | ||
|
||
/** | ||
* @namespace | ||
*/ | ||
namespace Pop\Csv; | ||
|
||
/** | ||
* CSV class | ||
* | ||
* @category Pop | ||
* @package Pop_Data | ||
* @author Nick Sagona, III <dev@nolainteractive.com> | ||
* @copyright Copyright (c) 2009-2016 NOLA Interactive, LLC. (http://www.nolainteractive.com) | ||
* @license http://www.popphp.org/license New BSD License | ||
* @version 3.0.0 | ||
*/ | ||
class Csv | ||
{ | ||
|
||
/** | ||
* Parse the CSV string into a PHP array | ||
* | ||
* @param string $string | ||
* @param array $options | ||
* @return array | ||
*/ | ||
public static function unserialize($string, array $options = []) | ||
{ | ||
$delimiter = (isset($options['delimiter'])) ? $options['delimiter'] : ','; | ||
$enclosure = (isset($options['enclosure'])) ? $options['enclosure'] : '"'; | ||
$escape = (isset($options['escape'])) ? $options['escape'] : "\\"; | ||
$fields = (isset($options['fields'])) ? (bool)$options['fields'] : true; | ||
$lines = preg_split("/((\r?\n)|(\r\n?))/", $string); | ||
$data = []; | ||
$fieldKeys = []; | ||
|
||
foreach ($lines as $i => $line) { | ||
$line = trim($line); | ||
if (!empty($line)) { | ||
if (($i == 0) && ($fields)) { | ||
$fieldNames = str_getcsv($line, $delimiter, $enclosure, $escape); | ||
foreach ($fieldNames as $name) { | ||
$fieldKeys[] = trim($name); | ||
} | ||
} else { | ||
$values = str_getcsv($line, $delimiter, $enclosure, $escape); | ||
foreach ($values as $key => $value) { | ||
$values[$key] = stripslashes(trim($value)); | ||
} | ||
if ((count($fieldKeys) > 0) && (count($fieldKeys) == count($values))) { | ||
$data[] = array_combine($fieldKeys, $values); | ||
} else { | ||
$data[] = $values; | ||
} | ||
} | ||
} | ||
} | ||
|
||
return $data; | ||
} | ||
|
||
/** | ||
* Convert the data into CSV format. | ||
* | ||
* @param mixed $data | ||
* @param array $options | ||
* @return string | ||
*/ | ||
public static function serialize($data, array $options = []) | ||
{ | ||
$keys = array_keys($data); | ||
$isAssoc = false; | ||
|
||
foreach ($keys as $key) { | ||
if (!is_numeric($key)) { | ||
$isAssoc = true; | ||
} | ||
} | ||
|
||
if ($isAssoc) { | ||
$newData = []; | ||
foreach ($data as $key => $value) { | ||
$newData = array_merge($newData, $value); | ||
} | ||
$data = $newData; | ||
} | ||
|
||
if (isset($options['omit'])) { | ||
$omit = (!is_array($options['omit'])) ? [$options['omit']] : $options['omit']; | ||
} else { | ||
$omit = []; | ||
} | ||
$delimiter = (isset($options['delimiter'])) ? $options['delimiter'] : ','; | ||
$enclosure = (isset($options['enclosure'])) ? $options['enclosure'] : '"'; | ||
$escape = (isset($options['escape'])) ? $options['escape'] : "\\"; | ||
$fields = (isset($options['fields'])) ? (bool)$options['fields'] : true; | ||
$csv = ''; | ||
|
||
if (is_array($data) && isset($data[0]) && (is_array($data[0]) || ($data[0] instanceof \ArrayObject)) && ($fields)) { | ||
$csv .= self::getFieldHeaders((array)$data[0], $delimiter, $omit); | ||
} | ||
|
||
// Initialize and clean the field values. | ||
foreach ($data as $value) { | ||
$csv .= self::serializeRow((array)$value, $omit, $delimiter, $enclosure, $escape); | ||
} | ||
|
||
return $csv; | ||
} | ||
|
||
/** | ||
* Serialize single row of data; | ||
* | ||
* @param array $value | ||
* @param array $omit | ||
* @param string $delimiter | ||
* @param string $enclosure | ||
* @param string $escape | ||
* @return string | ||
*/ | ||
public static function serializeRow(array $value, array $omit = [], $delimiter = ',', $enclosure = '"', $escape = "\\") | ||
{ | ||
$rowAry = []; | ||
foreach ($value as $key => $val) { | ||
if (!in_array($key, $omit)) { | ||
$val = str_replace(["\n", "\r"], [" ", " "], $val); | ||
if (strpos($val, $delimiter) !== false) { | ||
if (strpos($val, $enclosure) !== false) { | ||
$val = str_replace($enclosure, $escape . $enclosure, $val); | ||
} | ||
$val = $enclosure . $val . $enclosure; | ||
} | ||
$rowAry[] = $val; | ||
} | ||
} | ||
return implode($delimiter, $rowAry) . "\n"; | ||
} | ||
|
||
/** | ||
* Get field headers | ||
* | ||
* @param mixed $data | ||
* @param string $delimiter | ||
* @param array $omit | ||
* @return string | ||
*/ | ||
public static function getFieldHeaders($data, $delimiter = ',', array $omit = []) | ||
{ | ||
$headers = array_keys($data); | ||
$headersAry = []; | ||
foreach ($headers as $header) { | ||
if (!in_array($header, $omit)) { | ||
$headersAry[] = $header; | ||
} | ||
} | ||
return implode($delimiter, $headersAry) . PHP_EOL; | ||
} | ||
|
||
/** | ||
* Determine if the string is valid CSV | ||
* | ||
* @param string $string | ||
* @return boolean | ||
*/ | ||
public static function isValid($string) | ||
{ | ||
$lines = preg_split("/((\r?\n)|(\r\n?))/", $string); | ||
$fields = []; | ||
if (isset($lines[0])) { | ||
$fields = str_getcsv($lines[0]); | ||
} | ||
return (count($fields) > 0); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
/** | ||
* Pop PHP Framework (http://www.popphp.org/) | ||
* | ||
* @link https://github.com/popphp/popphp-framework | ||
* @author Nick Sagona, III <dev@nolainteractive.com> | ||
* @copyright Copyright (c) 2009-2016 NOLA Interactive, LLC. (http://www.nolainteractive.com) | ||
* @license http://www.popphp.org/license New BSD License | ||
*/ | ||
|
||
/** | ||
* @namespace | ||
*/ | ||
namespace Pop\Csv; | ||
|
||
/** | ||
* Csv exception class | ||
* | ||
* @category Pop | ||
* @package Pop_Csv | ||
* @author Nick Sagona, III <dev@nolainteractive.com> | ||
* @copyright Copyright (c) 2009-2016 NOLA Interactive, LLC. (http://www.nolainteractive.com) | ||
* @license http://www.popphp.org/license New BSD License | ||
* @version 3.0.0 | ||
*/ | ||
class Exception extends \Exception {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
namespace Pop\Data\Test; | ||
|
||
use Pop\Data\Type\Csv; | ||
|
||
class CsvTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
|
||
public function testUnserializeAndSerialize() | ||
{ | ||
$data = Csv::unserialize(file_get_contents(__DIR__ . '/tmp/data.csv')); | ||
$this->assertEquals('testuser1', $data[0]['username']); | ||
$string = Csv::serialize($data); | ||
$this->assertContains('testuser1,testuser1@test.com', $string); | ||
} | ||
|
||
public function testSerializeWithAssociativeArray() | ||
{ | ||
|
||
$data = [ | ||
'my_table' => [ | ||
[ | ||
'first_name' => 'Bob', | ||
'last_name' => 'Smith' | ||
], | ||
[ | ||
'first_name' => 'Jane', | ||
'last_name' => 'Smith' | ||
] | ||
] | ||
]; | ||
$string = Csv::serialize($data); | ||
$this->assertContains('Bob,Smith', $string); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
id,username,email | ||
1,testuser1,testuser1@test.com | ||
2,testuser2,testuser2@test.com |