Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F98285672
CsvHandler.php
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Sat, Jan 11, 18:58
Size
1 KB
Mime Type
text/x-php
Expires
Mon, Jan 13, 18:58 (1 d, 23 h)
Engine
blob
Format
Raw Data
Handle
23550172
Attached To
rPH Phabricator
CsvHandler.php
View Options
<?php
/**
* Mime Type: text/csv
* @author Raja Kapur <rajak@twistedthrottle.com>
*/
namespace
Httpful\Handlers
;
class
CsvHandler
extends
MimeHandlerAdapter
{
/**
* @param string $body
* @return mixed
*/
public
function
parse
(
$body
)
{
if
(
empty
(
$body
))
return
null
;
$parsed
=
array
();
$fp
=
fopen
(
'data://text/plain;base64,'
.
base64_encode
(
$body
),
'r'
);
while
((
$r
=
fgetcsv
(
$fp
))
!==
FALSE
)
{
$parsed
[]
=
$r
;
}
if
(
empty
(
$parsed
))
throw
new
\Exception
(
"Unable to parse response as CSV"
);
return
$parsed
;
}
/**
* @param mixed $payload
* @return string
*/
public
function
serialize
(
$payload
)
{
$fp
=
fopen
(
'php://temp/maxmemory:'
.
(
6
*
1024
*
1024
),
'r+'
);
$i
=
0
;
foreach
(
$payload
as
$fields
)
{
if
(
$i
++
==
0
)
{
fputcsv
(
$fp
,
array_keys
(
$fields
));
}
fputcsv
(
$fp
,
$fields
);
}
rewind
(
$fp
);
$data
=
stream_get_contents
(
$fp
);
fclose
(
$fp
);
return
$data
;
}
}
Event Timeline
Log In to Comment