Skip to content
Draft
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
18 changes: 15 additions & 3 deletions src/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -1222,11 +1222,11 @@ public function getCacheSize(): int
* An internal wrapper to dbsafeString, used to process a complete array of parameters
* as used by prepared statements.
*
* @param array<array-key, BackedEnum|EscapeableParameterInterface|scalar|null> $params
* @param array<array-key, BackedEnum|EscapeableParameterInterface|scalar|Stringable|null> $params
* @param list<bool>|false $escapes An array of boolean for each param, used to block the escaping of html-special chars.
* If not passed, all params will be cleaned.
*
* @return list<scalar|null>
* @return list<scalar|Stringable|null>
*
* @see Db::dbsafeString($string, $htmlSpecialChars = true)
*/
Expand All @@ -1246,6 +1246,10 @@ private function dbsafeParams(array $params, array | false $escapes = []): array
continue;
}

if ($param instanceof EscapeableParameterInterface) {
continue;
}

if ($escapes !== false) {
if (isset($escapes[$key])) {
$param = $this->dbsafeString($param, $escapes[$key], false);
Expand All @@ -1263,7 +1267,11 @@ private function dbsafeParams(array $params, array | false $escapes = []): array
/**
* Makes a string db-safe.
*
* @return ($input is float ? float : ($input is int ? int : ($input is bool ? int<0,1> : ($input is null ? null : ($input is scalar ? string : mixed)))))
* @template T
*
* @param T $input
*
* @return ($input is float ? float : ($input is int ? int : ($input is bool ? int<0,1> : ($input is null ? null : ($input is scalar ? string : ($input is Stringable ? string : T))))))
* @deprecated we need to get rid of this
*/
public function dbsafeString(mixed $input, bool $htmlSpecialChars = true, bool $addSlashes = true): mixed
Expand All @@ -1285,6 +1293,10 @@ public function dbsafeString(mixed $input, bool $htmlSpecialChars = true, bool $
return $input;
}

if ($input instanceof Stringable) {
return (string) $input;
}

// escape special chars
if (is_scalar($input) && $htmlSpecialChars) {
$input = html_entity_decode((string) $input, ENT_COMPAT, 'UTF-8');
Expand Down
7 changes: 4 additions & 3 deletions src/ConnectionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ interface ConnectionInterface extends DoctrineConnectionInterface
* @throws QueryException
* @return array<int, array<string, mixed>>
*
* @see fetchAllAssociative
* @deprecated Use {@see self::fetchAllAssociative()} instead.
*/
public function getPArray(string $query, array $params = [], ?int $start = null, ?int $end = null, bool $cache = true, array $escapes = []): array;

Expand All @@ -61,7 +61,7 @@ public function getPArray(string $query, array $params = [], ?int $start = null,
* @throws QueryException
* @return array<string, mixed>
*
* @see fetchAssociative
* @deprecated Use {@see self::fetchAssociative()} instead.
*/
public function getPRow(string $query, array $params = [], int $number = 0, bool $cache = true, array $escapes = []): array;

Expand Down Expand Up @@ -108,7 +108,8 @@ public function getGenerator(string $query, array $params = [], int $chunkSize =
* @param list<bool> $escapes An array of booleans for each param, used to block the escaping of html-special chars.
* If not passed, all params will be cleaned.
* @throws QueryException
* @see executeStatement
*
* @deprecated Use {@see self::executeStatement()} instead.
*/
public function _pQuery(string $query, array $params = [], array $escapes = []): bool;

Expand Down
6 changes: 6 additions & 0 deletions src/EscapeableParameterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,14 @@

namespace Artemeon\Database;

use Stringable;

interface EscapeableParameterInterface
{
public function isEscape(): bool;

/**
* @return scalar|Stringable|null
*/
public function getValue(): mixed;
}
Loading