mirror of
https://github.com/ConnectedHumber/Air-Quality-Web
synced 2024-10-31 03:23:01 +00:00
31 lines
623 B
PHP
31 lines
623 B
PHP
<?php
|
|
|
|
namespace SBRL;
|
|
|
|
/**
|
|
* Serialises an object to a variety of formats.
|
|
*/
|
|
class ResponseEncoder
|
|
{
|
|
/**
|
|
* Encodes an array of associative arrays to CSV.
|
|
* @param array $data The data to encode.
|
|
* @return string The data encoded to csv.
|
|
*/
|
|
public static function encode_csv($data) {
|
|
if(empty($data))
|
|
return "";
|
|
|
|
$result = fopen('php://temp/maxmemory:'. (5*1024*1024), 'r+');
|
|
fputcsv($result, array_keys($data[0]));
|
|
foreach($data as $row)
|
|
fputcsv($result, array_values($row));
|
|
rewind($result);
|
|
|
|
$response = \stream_get_contents($result);
|
|
|
|
fclose($result);
|
|
|
|
return $response;
|
|
}
|
|
}
|