-
Notifications
You must be signed in to change notification settings - Fork 10
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 #5 from zoilomora/encode-to-utf8
Encode to UTF-8
- Loading branch information
Showing
8 changed files
with
213 additions
and
7 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
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,120 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace ZoiloMora\Doctrine\DBAL\Driver\MicrosoftAccess; | ||
|
||
use Doctrine\DBAL\ParameterType; | ||
|
||
final class Statement extends \Doctrine\DBAL\Driver\PDO\Statement | ||
{ | ||
private const FROM_ENCODING = 'Windows-1252'; | ||
|
||
private ?string $charset; | ||
|
||
protected function __construct(?string $charset = null) | ||
{ | ||
$this->charset = $charset; | ||
} | ||
|
||
public function bindParam($param, &$variable, $type = ParameterType::STRING, $length = null, $driverOptions = null) | ||
{ | ||
switch ($type) { | ||
case ParameterType::LARGE_OBJECT: | ||
case ParameterType::BINARY: | ||
if ($driverOptions === null) { | ||
$driverOptions = \PDO::SQLSRV_ENCODING_BINARY; | ||
} | ||
|
||
break; | ||
|
||
case ParameterType::ASCII: | ||
$type = ParameterType::STRING; | ||
$length = 0; | ||
$driverOptions = \PDO::SQLSRV_ENCODING_SYSTEM; | ||
break; | ||
} | ||
|
||
return parent::bindParam($param, $variable, $type, $length, $driverOptions); | ||
} | ||
|
||
public function bindValue($param, $value, $type = ParameterType::STRING) | ||
{ | ||
return $this->bindParam($param, $value, $type); | ||
} | ||
|
||
public function fetchOne() | ||
{ | ||
return $this->convertStringEncoding( | ||
parent::fetchOne() | ||
); | ||
} | ||
|
||
public function fetchNumeric() | ||
{ | ||
return $this->convertArrayEncoding( | ||
parent::fetchNumeric() | ||
); | ||
} | ||
|
||
public function fetchAssociative() | ||
{ | ||
return $this->convertArrayEncoding( | ||
parent::fetchAssociative() | ||
); | ||
} | ||
|
||
public function fetchAllNumeric(): array | ||
{ | ||
return $this->convertCollectionEncoding( | ||
parent::fetchAllNumeric() | ||
); | ||
} | ||
|
||
public function fetchFirstColumn(): array | ||
{ | ||
return $this->convertArrayEncoding( | ||
parent::fetchFirstColumn() | ||
); | ||
} | ||
|
||
public function fetchAllAssociative(): array | ||
{ | ||
return $this->convertCollectionEncoding( | ||
parent::fetchAllAssociative() | ||
); | ||
} | ||
|
||
private function convertCollectionEncoding(array $items): array | ||
{ | ||
\array_walk( | ||
$items, | ||
function (&$item) { | ||
$item = $this->convertArrayEncoding($item); | ||
} | ||
); | ||
|
||
return $items; | ||
} | ||
|
||
private function convertArrayEncoding(array $items): array | ||
{ | ||
foreach ($items as $key => $value) { | ||
$items[$key] = $this->convertStringEncoding($items[$key]); | ||
} | ||
|
||
return $items; | ||
} | ||
|
||
private function convertStringEncoding(?string $value): ?string | ||
{ | ||
if (null === $this->charset) { | ||
return $value; | ||
} | ||
|
||
if (null === $value) { | ||
return null; | ||
} | ||
|
||
return \mb_convert_encoding($value, $this->charset, self::FROM_ENCODING); | ||
} | ||
} |
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
Binary file not shown.
Binary file not shown.
46 changes: 46 additions & 0 deletions
46
tests/Doctrine/DBAL/Driver/MicrosoftAccess/StatementTest.php
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 | ||
declare(strict_types=1); | ||
|
||
namespace ZoiloMora\Tests\Doctrine\DBAL\Driver\MicrosoftAccess; | ||
|
||
use Doctrine\DBAL\Connection; | ||
use ZoiloMora\Tests\BaseTest; | ||
|
||
class StatementTest extends BaseTest | ||
{ | ||
private const TABLE_NAME = 'test'; | ||
|
||
protected function filename(): string | ||
{ | ||
return 'Iurie-popovt-test.accdb'; | ||
} | ||
|
||
protected function dsn(): string | ||
{ | ||
return 'Iurie-popovt-test'; | ||
} | ||
|
||
protected function driverOptions(): array | ||
{ | ||
return [ | ||
'charset' => 'UTF-8', | ||
]; | ||
} | ||
|
||
/** | ||
* @test | ||
* @dataProvider connections | ||
*/ | ||
public function given_a_french_database_when_has_driver_options_then_it_appears_in_utf8(Connection $connection) | ||
{ | ||
$result = $connection | ||
->createQueryBuilder() | ||
->select('note') | ||
->from(self::TABLE_NAME) | ||
->execute(); | ||
|
||
$item = $result->fetchAllAssociative()[0]; | ||
|
||
$this->assertSame('Matériel Prêt un mail est envoyé', $item['note']); | ||
} | ||
} |