Skip to content

Commit

Permalink
no issue - allow converter fromSql() method $type parameter to be a u…
Browse files Browse the repository at this point in the history
…ser-given callback
  • Loading branch information
pounard committed May 13, 2024
1 parent 6a66199 commit e4a3eeb
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Next

* [feature] ⭐️ Allow `Converter::fromSql()` `$type` parameter to be a user-given callback.
* [fix] Cleanup PDO DSN options prior connecting in bridge factory.

## 1.6.1
Expand Down
5 changes: 4 additions & 1 deletion src/Converter/Converter.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,14 @@ public function toExpression(mixed $value, ?string $type = null): Expression
* @throws ValueConversionError
* In case of value conversion error.
*/
public function fromSql(string $type, null|int|float|string $value): mixed
public function fromSql(callable|string $type, null|int|float|string $value): mixed
{
if (null === $value) {
return null;
}
if (\is_callable($type)) {
return $type($value);
}

if (\str_ends_with($type, '[]')) {
return $this->parseArrayRecursion(\substr($type, 0, -2), ArrayRowParser::parseArray($value));
Expand Down
9 changes: 9 additions & 0 deletions tests/Converter/ConverterUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,15 @@ public function testToExpressionNullTypeFallback(): void
self::assertNull($value->getType());
}

public function testFromSqlCallback(): void
{
$converter = new Converter();

$callback = fn (mixed $value) => "13" === $value ? 7 : throw new \Exception();

self::assertSame(7, $converter->fromSql($callback, "13"));
}

public function testFromSqlBool(): void
{
$converter = new Converter();
Expand Down

0 comments on commit e4a3eeb

Please sign in to comment.