forked from cmoore4/phalcon-rest
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented CSVResponse. Updated readme to reflect.
- Loading branch information
Sean Moore
committed
Jun 7, 2013
1 parent
8c064ba
commit 1a2b187
Showing
5 changed files
with
89 additions
and
52 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
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
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,46 @@ | ||
<?php | ||
namespace PhalconRest\Responses; | ||
|
||
class CSVResponse extends Response{ | ||
|
||
protected $headers = true; | ||
|
||
public function __construct($di){ | ||
parent::__construct($di); | ||
} | ||
|
||
public function send($records){ | ||
|
||
$response = $this->di->get('response'); | ||
// Headers for a CSV | ||
$response->setHeader('Content-type', 'application/csv'); | ||
|
||
// By default, filename is just a timestamp. You should probably change this. | ||
$response->setHeader('Content-Disposition', 'attachment; filename="'.time().'.csv"'); | ||
$response->setHeader('Pragma', 'no-cache'); | ||
$response->setHeader('Expires', '0'); | ||
|
||
// We write directly to out, which means we don't ever save this file to disk. | ||
$handle = fopen('php://output', 'w'); | ||
|
||
// The keys of the first result record will be the first line of the CSV (headers) | ||
if($this->headers){ | ||
fputcsv($handle, array_keys($records[0])); | ||
} | ||
|
||
// Write each record as a csv line. | ||
foreach($records as $line){ | ||
fputcsv($handle, $line); | ||
} | ||
|
||
fclose($handle); | ||
|
||
return $this; | ||
} | ||
|
||
public function useHeaderRow($headers){ | ||
$this->headers = (bool) $headers; | ||
return $this; | ||
} | ||
|
||
} |
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
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