Skip to content

Commit fc3eff4

Browse files
committed
added CSV handler support
1 parent fa63b07 commit fc3eff4

File tree

3 files changed

+200
-147
lines changed

3 files changed

+200
-147
lines changed

src/Httpful/Bootstrap.php

+93-92
Original file line numberDiff line numberDiff line change
@@ -1,93 +1,94 @@
1-
<?php
2-
3-
namespace Httpful;
4-
5-
/**
6-
* Bootstrap class that facilitates autoloading. A naive
7-
* PSR-0 autoloader.
8-
*
9-
* @author Nate Good <me@nategood.com>
10-
*/
11-
class Bootstrap
12-
{
13-
14-
const DIR_GLUE = '/';
15-
const NS_GLUE = '\\';
16-
17-
public static $registered = false;
18-
19-
/**
20-
* Register the autoloader and any other setup needed
21-
*/
22-
public static function init()
23-
{
24-
spl_autoload_register(array('\Httpful\Bootstrap', 'autoload'));
25-
self::registerHandlers();
26-
}
27-
28-
/**
29-
* The autoload magic (PSR-0 style)
30-
*
31-
* @param string $classname
32-
*/
33-
public static function autoload($classname)
34-
{
35-
self::_autoload(dirname(dirname(__FILE__)), $classname);
36-
}
37-
38-
/**
39-
* Register the autoloader and any other setup needed
40-
*/
41-
public static function pharInit()
42-
{
43-
spl_autoload_register(array('\Httpful\Bootstrap', 'pharAutoload'));
44-
self::registerHandlers();
45-
}
46-
47-
/**
48-
* Phar specific autoloader
49-
*
50-
* @param string $classname
51-
*/
52-
public static function pharAutoload($classname)
53-
{
54-
self::_autoload('phar://httpful.phar', $classname);
55-
}
56-
57-
/**
58-
* @param string base
59-
* @param string classname
60-
*/
61-
private static function _autoload($base, $classname)
62-
{
63-
$parts = explode(self::NS_GLUE, $classname);
64-
$path = $base . self::DIR_GLUE . implode(self::DIR_GLUE, $parts) . '.php';
65-
66-
if (file_exists($path)) {
67-
require_once($path);
68-
}
69-
}
70-
/**
71-
* Register default mime handlers. Is idempotent.
72-
*/
73-
public static function registerHandlers()
74-
{
75-
if (self::$registered === true) {
76-
return;
77-
}
78-
79-
// @todo check a conf file to load from that instead of
80-
// hardcoding into the library?
81-
$handlers = array(
82-
\Httpful\Mime::JSON => new \Httpful\Handlers\JsonHandler(),
83-
\Httpful\Mime::XML => new \Httpful\Handlers\XmlHandler(),
84-
\Httpful\Mime::FORM => new \Httpful\Handlers\FormHandler(),
85-
);
86-
87-
foreach ($handlers as $mime => $handler) {
88-
Httpful::register($mime, $handler);
89-
}
90-
91-
self::$registered = true;
92-
}
1+
<?php
2+
3+
namespace Httpful;
4+
5+
/**
6+
* Bootstrap class that facilitates autoloading. A naive
7+
* PSR-0 autoloader.
8+
*
9+
* @author Nate Good <me@nategood.com>
10+
*/
11+
class Bootstrap
12+
{
13+
14+
const DIR_GLUE = '/';
15+
const NS_GLUE = '\\';
16+
17+
public static $registered = false;
18+
19+
/**
20+
* Register the autoloader and any other setup needed
21+
*/
22+
public static function init()
23+
{
24+
spl_autoload_register(array('\Httpful\Bootstrap', 'autoload'));
25+
self::registerHandlers();
26+
}
27+
28+
/**
29+
* The autoload magic (PSR-0 style)
30+
*
31+
* @param string $classname
32+
*/
33+
public static function autoload($classname)
34+
{
35+
self::_autoload(dirname(dirname(__FILE__)), $classname);
36+
}
37+
38+
/**
39+
* Register the autoloader and any other setup needed
40+
*/
41+
public static function pharInit()
42+
{
43+
spl_autoload_register(array('\Httpful\Bootstrap', 'pharAutoload'));
44+
self::registerHandlers();
45+
}
46+
47+
/**
48+
* Phar specific autoloader
49+
*
50+
* @param string $classname
51+
*/
52+
public static function pharAutoload($classname)
53+
{
54+
self::_autoload('phar://httpful.phar', $classname);
55+
}
56+
57+
/**
58+
* @param string base
59+
* @param string classname
60+
*/
61+
private static function _autoload($base, $classname)
62+
{
63+
$parts = explode(self::NS_GLUE, $classname);
64+
$path = $base . self::DIR_GLUE . implode(self::DIR_GLUE, $parts) . '.php';
65+
66+
if (file_exists($path)) {
67+
require_once($path);
68+
}
69+
}
70+
/**
71+
* Register default mime handlers. Is idempotent.
72+
*/
73+
public static function registerHandlers()
74+
{
75+
if (self::$registered === true) {
76+
return;
77+
}
78+
79+
// @todo check a conf file to load from that instead of
80+
// hardcoding into the library?
81+
$handlers = array(
82+
\Httpful\Mime::JSON => new \Httpful\Handlers\JsonHandler(),
83+
\Httpful\Mime::XML => new \Httpful\Handlers\XmlHandler(),
84+
\Httpful\Mime::FORM => new \Httpful\Handlers\FormHandler(),
85+
\Httpful\Mime::CSV => new \Httpful\Handlers\CsvHandler(),
86+
);
87+
88+
foreach ($handlers as $mime => $handler) {
89+
Httpful::register($mime, $handler);
90+
}
91+
92+
self::$registered = true;
93+
}
9394
}

src/Httpful/Handlers/CsvHandler.php

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
/**
3+
* Mime Type: text/csv
4+
* @author Raja Kapur <rajak@twistedthrottle.com>
5+
*/
6+
7+
namespace Httpful\Handlers;
8+
9+
class CsvHandler extends MimeHandlerAdapter
10+
{
11+
/**
12+
* @param string $body
13+
* @return mixed
14+
*/
15+
public function parse($body)
16+
{
17+
if (empty($body))
18+
return null;
19+
20+
$parsed = array();
21+
$fp = fopen('data://text/plain;base64,'.base64_encode($body), 'r');
22+
while (($r = fgetcsv($fp)) !== FALSE) {
23+
$parsed[] = $r;
24+
}
25+
26+
if (is_null($parsed))
27+
throw new \Exception("Unable to parse response as CSV");
28+
return $parsed;
29+
}
30+
31+
/**
32+
* @param mixed $payload
33+
* @return string
34+
*/
35+
public function serialize($payload)
36+
{
37+
$fp = fopen('php://temp/maxmemory:'. (12*1024*1024), 'r+');
38+
$i = 0;
39+
foreach ($payload as $fields) {
40+
if($i++ == 0) {
41+
fputcsv($fp, array_keys($fields));
42+
}
43+
fputcsv($fp, $fields);
44+
}
45+
rewind($fp);
46+
$data = stream_get_contents($fp);
47+
fclose($fp);
48+
return $data;
49+
}
50+
}

src/Httpful/Mime.php

+57-55
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,58 @@
1-
<?php
2-
3-
namespace Httpful;
4-
5-
/**
6-
* Class to organize the Mime stuff a bit more
7-
* @author Nate Good <me@nategood.com>
8-
*/
9-
class Mime
10-
{
11-
const JSON = 'application/json';
12-
const XML = 'application/xml';
13-
const XHTML = 'application/html+xml';
14-
const FORM = 'application/x-www-form-urlencoded';
15-
const PLAIN = 'text/plain';
16-
const JS = 'text/javascript';
17-
const HTML = 'text/html';
18-
const YAML = 'application/x-yaml';
19-
20-
/**
21-
* Map short name for a mime type
22-
* to a full proper mime type
23-
*/
24-
public static $mimes = array(
25-
'json' => self::JSON,
26-
'xml' => self::XML,
27-
'form' => self::FORM,
28-
'plain' => self::PLAIN,
29-
'text' => self::PLAIN,
30-
'html' => self::HTML,
31-
'xhtml' => self::XHTML,
32-
'js' => self::JS,
33-
'javascript'=> self::JS,
34-
'yaml' => self::YAML,
35-
);
36-
37-
/**
38-
* Get the full Mime Type name from a "short name".
39-
* Returns the short if no mapping was found.
40-
* @return string full mime type (e.g. application/json)
41-
* @param string common name for mime type (e.g. json)
42-
*/
43-
public static function getFullMime($short_name)
44-
{
45-
return array_key_exists($short_name, self::$mimes) ? self::$mimes[$short_name] : $short_name;
46-
}
47-
48-
/**
49-
* @return bool
50-
* @param string $short_name
51-
*/
52-
public static function supportsMimeType($short_name)
53-
{
54-
return array_key_exists($short_name, self::$mimes);
55-
}
1+
<?php
2+
3+
namespace Httpful;
4+
5+
/**
6+
* Class to organize the Mime stuff a bit more
7+
* @author Nate Good <me@nategood.com>
8+
*/
9+
class Mime
10+
{
11+
const JSON = 'application/json';
12+
const XML = 'application/xml';
13+
const XHTML = 'application/html+xml';
14+
const FORM = 'application/x-www-form-urlencoded';
15+
const PLAIN = 'text/plain';
16+
const JS = 'text/javascript';
17+
const HTML = 'text/html';
18+
const YAML = 'application/x-yaml';
19+
const CSV = 'text/csv';
20+
21+
/**
22+
* Map short name for a mime type
23+
* to a full proper mime type
24+
*/
25+
public static $mimes = array(
26+
'json' => self::JSON,
27+
'xml' => self::XML,
28+
'form' => self::FORM,
29+
'plain' => self::PLAIN,
30+
'text' => self::PLAIN,
31+
'html' => self::HTML,
32+
'xhtml' => self::XHTML,
33+
'js' => self::JS,
34+
'javascript'=> self::JS,
35+
'yaml' => self::YAML,
36+
'csv' => self::CSV,
37+
);
38+
39+
/**
40+
* Get the full Mime Type name from a "short name".
41+
* Returns the short if no mapping was found.
42+
* @return string full mime type (e.g. application/json)
43+
* @param string common name for mime type (e.g. json)
44+
*/
45+
public static function getFullMime($short_name)
46+
{
47+
return array_key_exists($short_name, self::$mimes) ? self::$mimes[$short_name] : $short_name;
48+
}
49+
50+
/**
51+
* @return bool
52+
* @param string $short_name
53+
*/
54+
public static function supportsMimeType($short_name)
55+
{
56+
return array_key_exists($short_name, self::$mimes);
57+
}
5658
}

0 commit comments

Comments
 (0)