-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #43 from keboola/zajca-bom
utf8 bom detection
- Loading branch information
Showing
10 changed files
with
102 additions
and
4 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,37 @@ | ||
<?php | ||
|
||
namespace Keboola\Csv; | ||
|
||
class UTF8BOMHelper | ||
{ | ||
/** | ||
* @param array $header | ||
* @return array | ||
*/ | ||
public static function detectAndRemoveBOM($header) | ||
{ | ||
if (!is_array($header)) { | ||
return $header; | ||
} | ||
$utf32BigEndianBom = chr(0x00) . chr(0x00) . chr(0xFE) . chr(0xFF); | ||
$utf32LittleEndianBom = chr(0xFF) . chr(0xFE) . chr(0x00) . chr(0x00); | ||
$utf16BigEndianBom = chr(0xFE) . chr(0xFF); | ||
$utf16LittleEndianBom = chr(0xFF) . chr(0xFE); | ||
$utf8Bom = chr(0xEF) . chr(0xBB) . chr(0xBF); | ||
|
||
foreach ([ | ||
$utf32BigEndianBom, | ||
$utf32LittleEndianBom, | ||
$utf16BigEndianBom, | ||
$utf16LittleEndianBom, | ||
$utf8Bom, | ||
] as $bomString) { | ||
if (strpos($header[0], $bomString) === 0) { | ||
$header[0] = trim(substr($header[0], strlen($bomString)), '"'); | ||
break; | ||
} | ||
} | ||
|
||
return $header; | ||
} | ||
} |
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,34 @@ | ||
<?php | ||
|
||
namespace Keboola\Csv\Tests; | ||
|
||
use Keboola\Csv\CsvReader; | ||
use Keboola\Csv\UTF8BOMHelper; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class UTF8BOMHelperTest extends TestCase | ||
{ | ||
/** | ||
* @dataProvider bomProvider | ||
* @param string $bomFile | ||
*/ | ||
public function testDetectAndRemoveBOM($bomFile) | ||
{ | ||
$file = __DIR__ . '/data/bom/' . $bomFile . '.csv'; | ||
$reader = new CsvReader($file); | ||
$firstLine = $reader->current(); | ||
$this->assertNotSame(['id', 'name'], $firstLine); | ||
$this->assertSame(['id', 'name'], UTF8BOMHelper::detectAndRemoveBOM($firstLine)); | ||
} | ||
|
||
public function bomProvider() | ||
{ | ||
return [ | ||
['utf32BigEndianBom'], | ||
['utf32LittleEndianBom'], | ||
['utf16BigEndianBom'], | ||
['utf16LittleEndianBom'], | ||
['utf8Bom'], | ||
]; | ||
} | ||
} |
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 @@ | ||
��"id","name" |
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 @@ | ||
��"id","name" |
Binary file not shown.
Binary file not shown.
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 @@ | ||
"id","name" |