Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add function str_putcsv #4

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"type": "library",
"license": "MIT",
"autoload": {
"files": ["src/functions_include.php"],
"files": ["src/functions_include.php", "src/polyfill-php71.php"],
"psr-4": {
"ipl\\Stdlib\\": "src"
}
Expand Down
44 changes: 44 additions & 0 deletions src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,47 @@ function arrayval($subject)
get_php_type($subject)
));
}

/**
* Format the given input array as CSV string and return it
*
* The input array is always expected to be an array of rows.
* The keys of the first row will be automatically used as the header row.
*
* @param iterable $data
* @param string $delimiter Field delimiter
* @param string $enclosure Field enclosure
* @param string $escape Escape character
*
* @return string
*
* @throws \InvalidArgumentException
*/
function str_putcsv($data, $delimiter = ',', $enclosure = '"', $escape = '\\')
{
$fp = fopen('php://temp', 'r+b');

if (! is_iterable($data)) {
throw new \InvalidArgumentException(sprintf(
'str_putcsv expects arrays or instances of Traversable. Got %s instead.',
get_php_type($data)
));
}

foreach ($data as $row) {
fputcsv($fp, array_keys($row), $delimiter, $enclosure, $escape);

break;
}

foreach ($data as $row) {
fputcsv($fp, $row, $delimiter, $enclosure, $escape);
}

rewind($fp);
$csv = stream_get_contents($fp);
fclose($fp);
$csv = rtrim($csv, "\n"); // fputcsv adds a newline

return $csv;
}
15 changes: 15 additions & 0 deletions src/polyfill-php71.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

if (PHP_VERSION_ID < 70100 && ! function_exists('\is_iterable')) {
/**
* Verify that the contents of a variable is an iterable value
*
* @param mixed $var The value to check
*
* @return bool Returns true if var is iterable, false otherwise
*/
function is_iterable($var)
{
return is_array($var) || $var instanceof \Traversable;
}
}
27 changes: 27 additions & 0 deletions tests/php/FunctionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,31 @@ public function testArrayvalException()
{
Stdlib\arrayval(null);
}

public function testStrPutcsv()
{
$data = [
[
'Name' => 'John Doe',
'Age' => 45
],
[
'Name' => 'Richard Roe',
'Age' => 38
],
[
'Name' => 'Jane Roe',
'Age' => 27
]
];

$csv = <<<'CSV'
Name,Age
"John Doe",45
"Richard Roe",38
"Jane Roe",27
CSV;

$this->assertSame($csv, Stdlib\str_putcsv($data));
}
}