This commit is contained in:
Daniil Gentili 2024-03-26 18:39:55 +01:00
parent 6c4c2d44c8
commit b222c6f67d
14 changed files with 76 additions and 4 deletions

View File

@ -45,21 +45,35 @@ abstract class DbArray implements Countable, ArrayAccess, Traversable, IteratorA
return $this->get($key) !== null;
}
/**
* @param TKey $offset
* @return TValue
*/
final public function offsetGet(mixed $offset): mixed
{
return $this->get($offset);
}
/**
* @param TKey $offset
*/
final public function offsetExists(mixed $offset): bool
{
return $this->isset($offset);
}
/**
* @param TKey $offset
* @param TValue $value
*/
final public function offsetSet(mixed $offset, mixed $value): void
{
$this->set($offset, $value);
}
/**
* @param TKey $offset
*/
final public function offsetUnset(mixed $offset): void
{
$this->unset($offset);

View File

@ -32,7 +32,7 @@ use function Amp\Future\await;
* @template TKey as array-key
* @template TValue
*
* @consistent-constructor
* @psalm-consistent-constructor
*
* @extends DbArray<TKey, TValue>
*
@ -40,8 +40,13 @@ use function Amp\Future\await;
*/
abstract class DriverArray extends DbArray
{
protected function __construct(protected readonly FieldConfig $config, protected readonly Serializer $serializer)
{
/**
* @param Serializer<TValue> $serializer
*/
protected function __construct(
protected readonly FieldConfig $config,
protected readonly Serializer $serializer
) {
}
public static function getInstance(FieldConfig $config, DbArray|null $previous): DbArray

View File

@ -33,6 +33,9 @@ use danog\AsyncOrm\Serializer;
*/
abstract class SqlArray extends DriverArray
{
/**
* @param Serializer<TValue> $serializer
*/
protected function __construct(
FieldConfig $config,
Serializer $serializer,

View File

@ -51,6 +51,9 @@ final class MysqlArray extends SqlArray
// We're forced to use quoting (just like PDO does internally when using prepares) because native MySQL prepares are extremely slow.
protected PDO $pdo;
/**
* @param Serializer<TValue> $serializer
*/
public function __construct(FieldConfig $config, Serializer $serializer)
{
$settings = $config->settings;

View File

@ -22,6 +22,7 @@ use danog\AsyncOrm\DbArray;
use danog\AsyncOrm\Driver\MemoryArray;
use danog\AsyncOrm\FieldConfig;
use danog\AsyncOrm\Internal\Containers\ObjectContainer;
use danog\AsyncOrm\Settings\DriverSettings;
use Traversable;
/**
@ -56,6 +57,7 @@ final class ObjectArray extends DbArray
$previous->cache->flushCache();
return $previous->cache->inner;
}
\assert($config->settings instanceof DriverSettings);
$previous->cache->startCacheCleanupLoop($config->settings->cacheTtl);
return $previous;
}

View File

@ -43,6 +43,9 @@ class PostgresArray extends SqlArray
private static array $connections = [];
private static ?LocalKeyedMutex $mutex = null;
/**
* @param Serializer<TValue> $serializer
*/
public function __construct(FieldConfig $config, Serializer $serializer)
{
self::$mutex ??= new LocalKeyedMutex;

View File

@ -49,6 +49,9 @@ final class RedisArray extends DriverArray
private readonly RedisClient $db;
/**
* @param Serializer<TValue> $serializer
*/
public function __construct(FieldConfig $config, Serializer $serializer)
{
if ($serializer instanceof Passthrough && $config->valueType === ValueType::INT) {
@ -118,6 +121,7 @@ final class RedisArray extends DriverArray
$value = $this->db->get($this->rKey($key));
if ($value !== null) {
/** @var TValue */
$value = $this->serializer->deserialize($value);
}

View File

@ -20,9 +20,17 @@ namespace danog\AsyncOrm;
/**
* Serializer interface.
*
* @template TValue
*/
interface Serializer
{
/**
* @param TValue $value
*/
public function serialize(mixed $value): mixed;
/**
* @return TValue
*/
public function deserialize(mixed $value): mixed;
}

View File

@ -21,9 +21,17 @@ namespace danog\AsyncOrm\Serializer;
use Amp\Postgres\PostgresByteA;
use danog\AsyncOrm\Serializer;
/** @internal BYTEA serializer */
/**
* @internal BYTEA serializer
*
* @template TValue
* @implements Serializer<TValue>
*/
final class ByteaSerializer implements Serializer
{
/**
* @param Serializer<TValue> $inner
*/
public function __construct(
private readonly Serializer $inner
) {

View File

@ -24,6 +24,9 @@ use danog\AsyncOrm\Serializer;
* Igbinary serializer.
*
* @api
*
* @template TValue
* @implements Serializer<TValue>
*/
final class Igbinary implements Serializer
{
@ -34,6 +37,7 @@ final class Igbinary implements Serializer
public function deserialize(mixed $value): mixed
{
\assert(\is_string($value));
/** @var TValue */
return \igbinary_unserialize($value);
}
}

View File

@ -24,6 +24,8 @@ use danog\AsyncOrm\Serializer;
* Integer casting serializer.
*
* @api
*
* @implements Serializer<int>
*/
final class IntString implements Serializer
{

View File

@ -24,6 +24,9 @@ use danog\AsyncOrm\Serializer;
* JSON serializer.
*
* @api
*
* @template TValue
* @implements Serializer<TValue>
*/
final class Json implements Serializer
{
@ -34,6 +37,7 @@ final class Json implements Serializer
public function deserialize(mixed $value): mixed
{
\assert(\is_string($value));
/** @var TValue */
return \json_decode($value, true, flags: JSON_THROW_ON_ERROR);
}
}

View File

@ -24,6 +24,9 @@ use danog\AsyncOrm\Serializer;
* Native serializer.
*
* @api
*
* @template TValue
* @implements Serializer<TValue>
*/
final class Native implements Serializer
{
@ -34,6 +37,7 @@ final class Native implements Serializer
public function deserialize(mixed $value): mixed
{
\assert(\is_string($value));
/** @var TValue */
return \unserialize($value);
}
}

View File

@ -24,13 +24,21 @@ use danog\AsyncOrm\Serializer;
* Passthrough serializer.
*
* @api
*
* @template TValue
* @implements Serializer<TValue>
*/
final class Passthrough implements Serializer
{
/**
* @param TValue $value
* @return TValue
*/
public function serialize(mixed $value): mixed
{
return $value;
}
/** @param TValue $value */
public function deserialize(mixed $value): mixed
{
return $value;