Skip to content

Commit

Permalink
Merge pull request #58 from ray-di/fix-57
Browse files Browse the repository at this point in the history
Reorder conversion methods in ParamConverter and binary check for logging
  • Loading branch information
koriym authored May 11, 2024
2 parents 9f79cab + 1a06081 commit 718bcf2
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"require": {
"php": "^8.1",
"ext-pdo": "*",
"ext-mbstring": "*",
"aura/sql": "^4.0 || ^5.0",
"doctrine/annotations": "^1.12 || ^2.0",
"guzzlehttp/guzzle": "^6.3 || ^7.2",
Expand Down
12 changes: 12 additions & 0 deletions src/MediaQueryLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@

use Stringable;

use function base64_encode;
use function implode;
use function is_string;
use function json_encode;
use function mb_check_encoding;
use function sprintf;

use const JSON_THROW_ON_ERROR;
Expand All @@ -27,6 +30,15 @@ public function start(): void
*/
public function log(string $queryId, array $values): void
{
/** @psalm-suppress MixedAssignment */
foreach ($values as &$value) {
if (! is_string($value) || mb_check_encoding($value, 'UTF-8')) {
continue;
}

$value = base64_encode($value); // Encode non-UTF-8 strings in base64 to handle binary data.
}

$this->logs[] = sprintf('query: %s(%s)', $queryId, json_encode($values, JSON_THROW_ON_ERROR));
}

Expand Down
8 changes: 4 additions & 4 deletions src/ParamConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ public function __invoke(array &$values): void
continue;
}

if (method_exists($value, '__toString')) {
$value = (string) $value;
if ($value instanceof ToScalarInterface) {
$value = $value->toScalar();
continue;
}

if ($value instanceof ToScalarInterface) {
$value = $value->toScalar();
if (method_exists($value, '__toString')) {
$value = (string) $value;
continue;
}

Expand Down

0 comments on commit 718bcf2

Please sign in to comment.