Skip to content

Commit

Permalink
Modify the escape parameter behaviour (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
ajgarlag committed Apr 8, 2016
1 parent bd30e66 commit efe5007
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 24 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
### Changed
- Remove the abstract declaration of `Ajgl\Csv\Rfc\CsvRfcUtils`
- Declare the `Ajgl\Csv\Rfc\CsvRfcUtils` constructor as private
- In writing context, the escape char should be a backslash (`\`), any other value will be ignored.

## 0.1.0 - 2016-02-25
### Added
Expand Down
32 changes: 26 additions & 6 deletions src/CsvRfcUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ private function __construct()
* @param string $escape
* @param string $eol
*/
public static function fPutCsv($handle, array $fields, $delimiter = ',', $enclosure = '"', $escape = '"', $eol = null)
public static function fPutCsv($handle, array $fields, $delimiter = ',', $enclosure = '"', $escape = '\\', $eol = null)
{
self::checkCsvEscape($enclosure, $escape);
self::checkPutCsvEscape($escape);

$eol = self::resolveEol($eol);
if ($eol !== self::EOL_WRITE_DEFAULT || self::hasAnyValueWithEscapeFollowedByEnclosure($fields, $enclosure)) {
Expand Down Expand Up @@ -87,7 +87,7 @@ private static function resolveEol($eol)
*/
public static function fGetCsv($handle, $length = 0, $delimiter = ',', $enclosure = '"', $escape = '"')
{
self::checkCsvEscape($enclosure, $escape);
self::checkGetCsvEscape($enclosure, $escape);

return \fgetcsv($handle, $length, $delimiter, $enclosure, $enclosure);
}
Expand All @@ -104,7 +104,7 @@ public static function fGetCsv($handle, $length = 0, $delimiter = ',', $enclosur
*/
public static function strGetCsv($input, $delimiter = ',', $enclosure = '"', $escape = '"')
{
self::checkCsvEscape($enclosure, $escape);
self::checkGetCsvEscape($enclosure, $escape);

return \str_getcsv($input, $delimiter, $enclosure, $enclosure);
}
Expand Down Expand Up @@ -155,18 +155,38 @@ public static function fixEnclosureEscape($enclosure, $line)
return \str_replace('\\'.$enclosure, '\\'.$enclosure.$enclosure, $line);
}

/**
* Emits a warning if the escape char is not the default backslash or null.
*
* @param string $escape
*/
public static function checkPutCsvEscape($escape)
{
if ($escape !== '\\' && $escape !== null) {
trigger_error(
sprintf(
"In writing mode, the escape char must be a backslash '\\'. "
."The given escape char '%s' will be ignored.",
$escape
),
E_USER_WARNING
);
}
}

/**
* Emits a warning if the enclosure char and escape char are different.
*
* @param string $enclosure
* @param string $escape
*/
public static function checkCsvEscape($enclosure, $escape)
public static function checkGetCsvEscape($enclosure, $escape)
{
if ($enclosure !== $escape) {
trigger_error(
sprintf(
"The escape and enclosure chars must be equals. The given escape char '%s' will be ignored.",
'In reading mode, the escape and enclosure chars must be equals. '
."The given escape char '%s' will be ignored.",
$escape
),
E_USER_WARNING
Expand Down
17 changes: 4 additions & 13 deletions src/Spl/SplFileObjectTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,31 +28,22 @@ protected function fixCsvControl()
$this->setCsvControl($delimiter, $enclosure);
}

/**
* @param string $enclosure
* @param string $escape
*/
protected function checkEscapeChar($enclosure, $escape)
{
CsvRfcUtils::checkCsvEscape($enclosure, $escape);
}

public function fgetcsv($delimiter = ',', $enclosure = '"', $escape = '"')
{
$this->checkEscapeChar($enclosure, $escape);
CsvRfcUtils::checkGetCsvEscape($enclosure, $escape);

return parent::fgetcsv($delimiter, $enclosure, $enclosure);
}

public function fputcsv($fields, $delimiter = ',', $enclosure = '"', $escape = '"')
public function fputcsv($fields, $delimiter = ',', $enclosure = '"', $escape = '\\')
{
$this->checkEscapeChar($enclosure, $escape);
CsvRfcUtils::checkPutCsvEscape($escape);
$this->fwrite(CsvRfcUtils::strPutCsv($fields, $delimiter, $enclosure));
}

public function setCsvControl($delimiter = ',', $enclosure = '"', $escape = '"')
{
$this->checkEscapeChar($enclosure, $escape);
CsvRfcUtils::checkGetCsvEscape($enclosure, $escape);
parent::setCsvControl($delimiter, $enclosure, $enclosure);
}
}
2 changes: 1 addition & 1 deletion src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
* @param string $enclosure
* @param string $escape
*/
function fputcsv($handle, array $fields, $delimiter = ',', $enclosure = '"', $escape = '"')
function fputcsv($handle, array $fields, $delimiter = ',', $enclosure = '"', $escape = '\\')
{
CsvRfcUtils::fPutCsv($handle, $fields, $delimiter, $enclosure, $escape);
}
Expand Down
6 changes: 3 additions & 3 deletions tests/CsvRfcUtilsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,15 +107,15 @@ public function testFPutCsv()
);

$fp = fopen('php://temp', 'w+');
CsvRfcUtils::fPutCsv($fp, $fields, ';', "'", "'");
CsvRfcUtils::fPutCsv($fp, $fields, ';', "'");
rewind($fp);
$this->assertEquals(
"Hello,World!;'Hello;World!';'Hello\\\"World\\\"!';'Hello\''World\''!';'Hello\nWorld!'\n",
fread($fp, 4096)
);

$fp = fopen('php://temp', 'w+');
CsvRfcUtils::fPutCsv($fp, $fields, ';', "'", "'", CsvRfcUtils::EOL_WRITE_RFC);
CsvRfcUtils::fPutCsv($fp, $fields, ';', "'", null, CsvRfcUtils::EOL_WRITE_RFC);
rewind($fp);
$this->assertEquals(
"Hello,World!;'Hello;World!';'Hello\\\"World\\\"!';'Hello\''World\''!';'Hello\nWorld!'\r\n",
Expand Down Expand Up @@ -155,7 +155,7 @@ public function testFPutCsvWithRfc()
);

$fp = fopen('php://temp', 'w+');
CsvRfcUtils::fPutCsv($fp, $fields, ';', "'", "'");
CsvRfcUtils::fPutCsv($fp, $fields, ';', "'");
rewind($fp);
$this->assertEquals(
"Hello,World!;'Hello;World!';'Hello\\\"World\\\"!';'Hello\''World\''!';'Hello\nWorld!'\r\n",
Expand Down
2 changes: 1 addition & 1 deletion tests/functionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public function test_fputcsv()
);

$fp = fopen('php://temp', 'w+');
Rfc\fputcsv($fp, $fields, ';', "'", "'");
Rfc\fputcsv($fp, $fields, ';', "'");
rewind($fp);
$this->assertEquals(
"Hello,World!;'Hello;World!';'Hello\\\"World\\\"!';'Hello\''World\''!';'Hello\nWorld!'\n",
Expand Down

0 comments on commit efe5007

Please sign in to comment.