Skip to content

Commit 9f0fd13

Browse files
committed
feat: support contextual casters and serializers
1 parent 3ecc604 commit 9f0fd13

File tree

72 files changed

+1065
-389
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

72 files changed

+1065
-389
lines changed

mago.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
php-version = "8.4.0"
1+
php-version = "8.5.0"
22

33
[source]
44
paths = ["src", "packages", "tests"]

packages/container/src/GenericContainer.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -177,11 +177,7 @@ public function get(string $className, null|string|UnitEnum $tag = null, mixed .
177177
{
178178
$this->resolveChain();
179179

180-
$dependency = $this->resolve(
181-
className: $className,
182-
tag: $tag,
183-
params: $params,
184-
);
180+
$dependency = $this->resolve($className, $tag, ...$params);
185181

186182
$this->stopChain();
187183

packages/database/src/Casters/EncryptedCaster.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ public function __construct(
1515
private Encrypter $encrypter,
1616
) {}
1717

18+
public static function for(): false
19+
{
20+
return false;
21+
}
22+
1823
public function cast(mixed $input): ?string
1924
{
2025
if ($input === null) {

packages/database/src/Casters/PrimaryKeyCaster.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,18 @@
44

55
namespace Tempest\Database\Casters;
66

7+
use Tempest\Core\Priority;
78
use Tempest\Database\PrimaryKey;
89
use Tempest\Mapper\Caster;
910

11+
#[Priority(Priority::HIGHEST)]
1012
final readonly class PrimaryKeyCaster implements Caster
1113
{
14+
public static function for(): string
15+
{
16+
return PrimaryKey::class;
17+
}
18+
1219
public function cast(mixed $input): PrimaryKey
1320
{
1421
if ($input instanceof PrimaryKey) {

packages/database/src/DatabaseInitializer.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ className: Connection::class,
4141
$connection = $container->get(Connection::class, $tag);
4242

4343
return new GenericDatabase(
44-
$connection,
45-
new GenericTransactionManager($connection),
46-
$container->get(SerializerFactory::class),
44+
connection: $connection,
45+
transactionManager: new GenericTransactionManager($connection),
46+
serializerFactory: new SerializerFactory($container),
4747
);
4848
}
4949
}

packages/database/src/GenericDatabase.php

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Tempest\Database\Connection\Connection;
1313
use Tempest\Database\Exceptions\QueryWasInvalid;
1414
use Tempest\Database\Transactions\TransactionManager;
15+
use Tempest\Mapper\Context;
1516
use Tempest\Mapper\SerializerFactory;
1617
use Throwable;
1718
use UnitEnum;
@@ -29,6 +30,14 @@ final class GenericDatabase implements Database
2930
get => $this->connection->config->tag;
3031
}
3132

33+
private string $context {
34+
get => match ($this->dialect) {
35+
DatabaseDialect::POSTGRESQL => Context::DATABASE_POSTGRESQL,
36+
DatabaseDialect::MYSQL => Context::DATABASE_MYSQL,
37+
DatabaseDialect::SQLITE => Context::DATABASE_SQLITE,
38+
};
39+
}
40+
3241
public function __construct(
3342
private(set) readonly Connection $connection,
3443
private(set) readonly TransactionManager $transactionManager,
@@ -128,17 +137,9 @@ private function resolveBindings(Query $query): array
128137
$bindings = [];
129138

130139
foreach ($query->bindings as $key => $value) {
131-
// Database handle booleans differently. We might need a database-aware serializer at some point.
132-
if (is_bool($value)) {
133-
$value = match ($this->dialect) {
134-
DatabaseDialect::POSTGRESQL => $value ? 'true' : 'false',
135-
default => $value ? '1' : '0',
136-
};
137-
}
138-
139140
if ($value instanceof Query) {
140141
$value = $value->execute();
141-
} elseif ($serializer = $this->serializerFactory->forValue($value)) {
142+
} elseif ($serializer = $this->serializerFactory->forValue($value, $this->context)) {
142143
$value = $serializer->serialize($value);
143144
}
144145

packages/database/src/PrimaryKey.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@
55
namespace Tempest\Database;
66

77
use Stringable;
8-
use Tempest\Database\Casters\PrimaryKeyCaster;
9-
use Tempest\Mapper\CastWith;
108

11-
#[CastWith(PrimaryKeyCaster::class)]
129
final readonly class PrimaryKey implements Stringable
1310
{
1411
public string|int $value;

packages/database/src/Serializers/EncryptedSerializer.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ public function __construct(
1414
private Encrypter $encrypter,
1515
) {}
1616

17+
public static function for(): false
18+
{
19+
return false;
20+
}
21+
1722
public function serialize(mixed $input): array|string
1823
{
1924
if (! is_string($input)) {

packages/database/src/Serializers/HashedSerializer.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ public function __construct(
1111
private PasswordHasher $passwordHasher,
1212
) {}
1313

14+
public static function for(): false
15+
{
16+
return false;
17+
}
18+
1419
public function serialize(mixed $input): string
1520
{
1621
if (! is_string($input)) {
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tempest\Database\Serializers;
6+
7+
use Tempest\Mapper\Context;
8+
use Tempest\Mapper\Exceptions\ValueCouldNotBeSerialized;
9+
use Tempest\Mapper\Serializer;
10+
11+
#[Context(Context::DATABASE_MYSQL)]
12+
final class MysqlBooleanSerializer implements Serializer
13+
{
14+
public static function for(): string
15+
{
16+
return 'bool';
17+
}
18+
19+
public function serialize(mixed $input): string
20+
{
21+
if (! is_bool($input)) {
22+
throw new ValueCouldNotBeSerialized('boolean');
23+
}
24+
25+
return $input ? '1' : '0';
26+
}
27+
}

0 commit comments

Comments
 (0)