Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reorder conversion methods in ParamConverter and binary check for logging #58

Merged
merged 5 commits into from
May 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading