Skip to content

Commit

Permalink
Merge pull request #20 from hdogan/utf8-bom-support
Browse files Browse the repository at this point in the history
Added _bom variable to support UTF-8 BOM output
  • Loading branch information
josegonzalez committed Oct 18, 2013
2 parents 7b80daf + 18ea01d commit ce6aec7
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ public function export() {
}
```

You can also specify the delimiter, end of line, newline and escape characters using
`$_delimiter`, `$_eol`, `$_newline` and `$_enclosure`, respectively:
You can also specify the delimiter, end of line, newline, escape characters and byte order mark (BOM) sequence using
`$_delimiter`, `$_eol`, `$_newline`, `$_enclosure` and `$_bom` respectively:

```php
public function export() {
Expand All @@ -126,6 +126,7 @@ public function export() {
$_enclosure = '"';
$_newline = '\r\n';
$_eol = '~';
$_bom = true;

$this->viewClass = 'CsvView.Csv';
$this->set(compact('data', '_serialize', '_delimiter', '_enclosure', '_newline', '_eol'));
Expand All @@ -138,11 +139,14 @@ The defaults for these variables are:
* `_enclosure`: `"`
* `_newline`: `\n`
* `_eol`: `\n`
* `_bom`: false

The `_eol` variable is the one used to generate newlines in the output.
`_newline`, however, is the character that should replace the newline characters in the actual data.
It is recommended to use the string representation of the newline character to avoid rendering invalid output.

Some reader software incorrectly renders UTF-8 encoded files which do not contain byte order mark (BOM) byte sequence. The `_bom` variable is the one used to add byte order mark (BOM) byte sequence beginning of the generated CSV output stream. See [`Wikipedia article about byte order mark`](http://en.wikipedia.org/wiki/Byte_order_mark) for more information.

If you have complex model data, you can use the `$_extract` view variable to specify the individual paths for each record. This is an array of `Hash::extract()`-compatible syntax:

```php
Expand Down
12 changes: 11 additions & 1 deletion View/CsvView.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ public function render($view = null, $layout = null) {
* - '_enclosure': (default '"') CSV Enclosure for use with fputscsv()
* - '_newline': (default '\n') CSV Newline replacement for use with fputscsv()
* - '_eol': (default '\n') End-of-line character the csv
* - '_bom': (default false) Adds BOM (byte order mark) header
*
* @return void
**/
Expand All @@ -158,7 +159,8 @@ protected function _setupViewVars() {
'_enclosure',
'_newline',
'_eol',
'_null'
'_null',
'_bom'
);
foreach ($required as $viewVar) {
if (!isset($this->viewVars[$viewVar])) {
Expand Down Expand Up @@ -186,6 +188,10 @@ protected function _setupViewVars() {
$this->viewVars['_null'] = 'NULL';
}

if ($this->viewVars['_bom'] === null) {
$this->viewVars['_bom'] = false;
}

if ($this->viewVars['_extract'] !== null) {
$this->viewVars['_extract'] = (array)$this->viewVars['_extract'];
foreach ($this->viewVars['_extract'] as $i => $extract) {
Expand Down Expand Up @@ -261,6 +267,10 @@ protected function _generateRow($row = null) {
static $fp = false;
if ($fp === false) {
$fp = fopen('php://temp', 'r+');

if ($this->viewVars['_bom']) {
fputs($fp, chr(0xEF) . chr(0xBB) . chr(0xBF));
}
} else {
rewind($fp);
}
Expand Down

0 comments on commit ce6aec7

Please sign in to comment.