Skip to content

Commit

Permalink
added ObjectTranslator (#420)
Browse files Browse the repository at this point in the history
Object translator allows to translate any object passed to Translator into Dibi\Expression.
  • Loading branch information
milo authored and dg committed Dec 20, 2022
1 parent 78646e1 commit 124d521
Show file tree
Hide file tree
Showing 3 changed files with 153 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/Dibi/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ class Connection implements IConnection

private ?Translator $translator = null;

/** @var array<string, callable(object): Expression> */
private array $translators = [];

private HashMap $substitutes;

private int $transactionDepth = 0;
Expand Down Expand Up @@ -522,6 +525,24 @@ public function substitute(string $value): string
}


/********************* value objects translation ****************d*g**/


/** @param callable(object): Expression $translator */
public function addObjectTranslator(string $class, callable $translator): self
{
$this->translators[$class] = $translator;
return $this;
}


/** @return array<string, callable(object): Expression> */
public function getObjectTranslators(): array
{
return $this->translators;
}


/********************* shortcuts ****************d*g**/


Expand Down
13 changes: 13 additions & 0 deletions src/Dibi/Translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,19 @@ public function formatValue(mixed $value, ?string $modifier): string
}
}

if (is_object($value)
&& $modifier === null
&& !$value instanceof Literal
&& !$value instanceof Expression
) {
foreach ($this->connection->getObjectTranslators() as $class => $translator) {
if ($value instanceof $class) {
$value = $translator($value);
return $this->connection->translate(...$value->getValues());
}
}
}

// object-to-scalar procession
if ($value instanceof \BackedEnum && is_scalar($value->value)) {
$value = $value->value;
Expand Down
119 changes: 119 additions & 0 deletions tests/dibi/Connection.objectTranslator.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
<?php

/**
* @dataProvider ../databases.ini
*/

declare(strict_types=1);

use Tester\Assert;

require __DIR__ . '/bootstrap.php';

$conn = new Dibi\Connection($config + ['formatDateTime' => "'Y-m-d H:i:s.u'", 'formatDate' => "'Y-m-d'"]);


class Email
{
public $address = 'address@example.com';
}

class Time extends DateTimeImmutable
{
}


test('Without object translator', function () use ($conn) {
Assert::exception(function () use ($conn) {
$conn->translate('?', new Email);
}, Dibi\Exception::class, 'SQL translate error: Unexpected Email');
});


test('Basics', function () use ($conn) {
$conn->addObjectTranslator(Email::class, fn($object) => new Dibi\Expression('?', $object->address));
Assert::same(
reformat([
'sqlsrv' => "N'address@example.com'",
"'address@example.com'",
]),
$conn->translate('?', new Email),
);
});


test('DateTime', function () use ($conn) {
$stamp = Time::createFromFormat('Y-m-d H:i:s', '2022-11-22 12:13:14');

// Without object translator, DateTime child is translated by driver
Assert::same(
$conn->getDriver()->escapeDateTime($stamp),
$conn->translate('?', $stamp),
);


// With object translator
$conn->addObjectTranslator(Time::class, fn($object) => new Dibi\Expression('OwnTime(?)', $object->format('H:i:s')));
Assert::same(
reformat([
'sqlsrv' => "OwnTime(N'12:13:14')",
"OwnTime('12:13:14')",
]),
$conn->translate('?', $stamp),
);


// With modifier, it is still translated by driver
Assert::same(
$conn->getDriver()->escapeDateTime($stamp),
$conn->translate('%dt', $stamp),
);
Assert::same(
$conn->getDriver()->escapeDateTime($stamp),
$conn->translate('%t', $stamp),
);
Assert::same(
$conn->getDriver()->escapeDate($stamp),
$conn->translate('%d', $stamp),
);


// DateTimeImmutable as a Time parent is not affected and still translated by driver
$dt = DateTimeImmutable::createFromFormat('Y-m-d H:i:s', '2022-11-22 12:13:14');
Assert::same(
$conn->getDriver()->escapeDateTime($dt),
$conn->translate('?', $dt),
);

// But DateTime translation can be overloaded
$conn->addObjectTranslator(DateTimeInterface::class, fn() => new Dibi\Expression('OwnDateTime'));
Assert::same(
'OwnDateTime',
$conn->translate('?', $dt),
);
});


test('Complex structures', function () use ($conn) {
$conn->addObjectTranslator(Email::class, fn($object) => new Dibi\Expression('?', $object->address));
$conn->addObjectTranslator(Time::class, fn($object) => new Dibi\Expression('OwnTime(?)', $object->format('H:i:s')));
$conn->addObjectTranslator(Time::class, fn($object) => new Dibi\Expression('OwnTime(?)', $object->format('H:i:s')));

$time = Time::createFromFormat('Y-m-d H:i:s', '2022-11-22 12:13:14');
Assert::same(
reformat([
'sqlsrv' => "([a], [b], [c], [d], [e], [f], [g]) VALUES (OwnTime(N'12:13:14'), '2022-11-22', CONVERT(DATETIME2(7), '2022-11-22 12:13:14.000000'), CONVERT(DATETIME2(7), '2022-11-22 12:13:14.000000'), N'address@example.com', OwnDateTime, OwnDateTime)",
'odbc' => "([a], [b], [c], [d], [e], [f], [g]) VALUES (OwnTime('12:13:14'), #11/22/2022#, #11/22/2022 12:13:14.000000#, #11/22/2022 12:13:14.000000#, 'address@example.com', OwnDateTime, OwnDateTime)",
"([a], [b], [c], [d], [e], [f], [g]) VALUES (OwnTime('12:13:14'), '2022-11-22', '2022-11-22 12:13:14.000000', '2022-11-22 12:13:14.000000', 'address@example.com', OwnDateTime, OwnDateTime)",
]),
$conn->translate('%v', [
'a' => $time,
'b%d' => $time,
'c%t' => $time,
'd%dt' => $time,
'e' => new Email,
'f' => new DateTime,
'g' => new DateTimeImmutable,
]),
);
});

0 comments on commit 124d521

Please sign in to comment.