mirror of
https://github.com/danog/AsyncOrm.git
synced 2024-11-29 20:29:48 +01:00
Add support for more types
This commit is contained in:
parent
0923a7d544
commit
f34630aaae
@ -25,6 +25,8 @@ use AssertionError;
|
||||
use danog\AsyncOrm\Driver\Mysql;
|
||||
use danog\AsyncOrm\Driver\SqlArray;
|
||||
use danog\AsyncOrm\FieldConfig;
|
||||
use danog\AsyncOrm\Internal\Serializer\BoolInt;
|
||||
use danog\AsyncOrm\Internal\Serializer\BoolString;
|
||||
use danog\AsyncOrm\Internal\Serializer\Passthrough;
|
||||
use danog\AsyncOrm\KeyType;
|
||||
use danog\AsyncOrm\Serializer;
|
||||
@ -60,6 +62,7 @@ final class MysqlArray extends SqlArray
|
||||
/** @var Serializer<TValue> */
|
||||
$serializer = match ($config->valueType) {
|
||||
ValueType::SCALAR, ValueType::OBJECT => $serializer,
|
||||
ValueType::BOOL => new BoolInt,
|
||||
default => new Passthrough
|
||||
};
|
||||
$settings = $config->settings;
|
||||
|
@ -22,6 +22,8 @@ use Amp\Postgres\PostgresConnectionPool;
|
||||
use Amp\Sync\LocalKeyedMutex;
|
||||
use danog\AsyncOrm\Driver\SqlArray;
|
||||
use danog\AsyncOrm\FieldConfig;
|
||||
use danog\AsyncOrm\Internal\Serializer\BoolInt;
|
||||
use danog\AsyncOrm\Internal\Serializer\BoolString;
|
||||
use danog\AsyncOrm\Internal\Serializer\ByteaSerializer;
|
||||
use danog\AsyncOrm\Internal\Serializer\Passthrough;
|
||||
use danog\AsyncOrm\KeyType;
|
||||
@ -92,7 +94,7 @@ class PostgresArray extends SqlArray
|
||||
ValueType::STRING => "VARCHAR(255)",
|
||||
ValueType::OBJECT => "MEDIUMBLOB",
|
||||
ValueType::FLOAT => "FLOAT(53)",
|
||||
ValueType::BOOL => "BIT(1)",
|
||||
ValueType::BOOL => "BOOLEAN",
|
||||
ValueType::SCALAR, ValueType::OBJECT => "BYTEA",
|
||||
};
|
||||
/** @var Serializer<TValue> */
|
||||
|
@ -23,6 +23,8 @@ use Amp\Redis\RedisClient;
|
||||
use Amp\Sync\LocalKeyedMutex;
|
||||
use danog\AsyncOrm\Driver\DriverArray;
|
||||
use danog\AsyncOrm\FieldConfig;
|
||||
use danog\AsyncOrm\Internal\Serializer\BoolString;
|
||||
use danog\AsyncOrm\Internal\Serializer\FloatString;
|
||||
use danog\AsyncOrm\Internal\Serializer\IntString;
|
||||
use danog\AsyncOrm\Internal\Serializer\Passthrough;
|
||||
use danog\AsyncOrm\KeyType;
|
||||
@ -59,6 +61,8 @@ final class RedisArray extends DriverArray
|
||||
/** @var Serializer<TValue> */
|
||||
$serializer = match ($config->valueType) {
|
||||
ValueType::INT => new IntString,
|
||||
ValueType::FLOAT => new FloatString,
|
||||
ValueType::BOOL => new BoolString,
|
||||
ValueType::SCALAR, ValueType::OBJECT => $serializer,
|
||||
default => new Passthrough
|
||||
};
|
||||
|
47
src/Internal/Serializer/BoolInt.php
Normal file
47
src/Internal/Serializer/BoolInt.php
Normal file
@ -0,0 +1,47 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* This file is part of AsyncOrm.
|
||||
* AsyncOrm is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
|
||||
* AsyncOrm is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||
* See the GNU Affero General Public License for more details.
|
||||
* You should have received a copy of the GNU General Public License along with AsyncOrm.
|
||||
* If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* @author Daniil Gentili <daniil@daniil.it>
|
||||
* @author Alexander Pankratov <alexander@i-c-a.su>
|
||||
* @copyright 2016-2024 Daniil Gentili <daniil@daniil.it>
|
||||
* @copyright 2016-2024 Alexander Pankratov <alexander@i-c-a.su>
|
||||
* @license https://opensource.org/licenses/AGPL-3.0 AGPLv3
|
||||
* @link https://daniil.it/AsyncOrm AsyncOrm documentation
|
||||
*/
|
||||
|
||||
namespace danog\AsyncOrm\Internal\Serializer;
|
||||
|
||||
use AssertionError;
|
||||
use danog\AsyncOrm\Serializer;
|
||||
|
||||
/**
|
||||
* Bool casting serializer.
|
||||
*
|
||||
* @internal
|
||||
*
|
||||
* @implements Serializer<bool>
|
||||
*/
|
||||
final class BoolInt implements Serializer
|
||||
{
|
||||
public function serialize(mixed $value): mixed
|
||||
{
|
||||
return match ($value) {
|
||||
true => 1,
|
||||
false => 0,
|
||||
};
|
||||
}
|
||||
public function deserialize(mixed $value): mixed
|
||||
{
|
||||
return match ($value) {
|
||||
1 => true,
|
||||
0 => false,
|
||||
};
|
||||
}
|
||||
}
|
46
src/Internal/Serializer/BoolString.php
Normal file
46
src/Internal/Serializer/BoolString.php
Normal file
@ -0,0 +1,46 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* This file is part of AsyncOrm.
|
||||
* AsyncOrm is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
|
||||
* AsyncOrm is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||
* See the GNU Affero General Public License for more details.
|
||||
* You should have received a copy of the GNU General Public License along with AsyncOrm.
|
||||
* If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* @author Daniil Gentili <daniil@daniil.it>
|
||||
* @author Alexander Pankratov <alexander@i-c-a.su>
|
||||
* @copyright 2016-2024 Daniil Gentili <daniil@daniil.it>
|
||||
* @copyright 2016-2024 Alexander Pankratov <alexander@i-c-a.su>
|
||||
* @license https://opensource.org/licenses/AGPL-3.0 AGPLv3
|
||||
* @link https://daniil.it/AsyncOrm AsyncOrm documentation
|
||||
*/
|
||||
|
||||
namespace danog\AsyncOrm\Internal\Serializer;
|
||||
|
||||
use danog\AsyncOrm\Serializer;
|
||||
|
||||
/**
|
||||
* Bool casting serializer.
|
||||
*
|
||||
* @internal
|
||||
*
|
||||
* @implements Serializer<bool>
|
||||
*/
|
||||
final class BoolString implements Serializer
|
||||
{
|
||||
public function serialize(mixed $value): mixed
|
||||
{
|
||||
return match ($value) {
|
||||
true => '1',
|
||||
false => '0',
|
||||
};
|
||||
}
|
||||
public function deserialize(mixed $value): mixed
|
||||
{
|
||||
return match ($value) {
|
||||
'1' => true,
|
||||
'0' => false,
|
||||
};
|
||||
}
|
||||
}
|
40
src/Internal/Serializer/FloatString.php
Normal file
40
src/Internal/Serializer/FloatString.php
Normal file
@ -0,0 +1,40 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* This file is part of AsyncOrm.
|
||||
* AsyncOrm is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
|
||||
* AsyncOrm is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||
* See the GNU Affero General Public License for more details.
|
||||
* You should have received a copy of the GNU General Public License along with AsyncOrm.
|
||||
* If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* @author Daniil Gentili <daniil@daniil.it>
|
||||
* @author Alexander Pankratov <alexander@i-c-a.su>
|
||||
* @copyright 2016-2024 Daniil Gentili <daniil@daniil.it>
|
||||
* @copyright 2016-2024 Alexander Pankratov <alexander@i-c-a.su>
|
||||
* @license https://opensource.org/licenses/AGPL-3.0 AGPLv3
|
||||
* @link https://daniil.it/AsyncOrm AsyncOrm documentation
|
||||
*/
|
||||
|
||||
namespace danog\AsyncOrm\Internal\Serializer;
|
||||
|
||||
use danog\AsyncOrm\Serializer;
|
||||
|
||||
/**
|
||||
* Float casting serializer.
|
||||
*
|
||||
* @internal
|
||||
*
|
||||
* @implements Serializer<float>
|
||||
*/
|
||||
final class FloatString implements Serializer
|
||||
{
|
||||
public function serialize(mixed $value): mixed
|
||||
{
|
||||
return (string) $value;
|
||||
}
|
||||
public function deserialize(mixed $value): mixed
|
||||
{
|
||||
return (float) $value;
|
||||
}
|
||||
}
|
@ -109,11 +109,14 @@ final class OrmTest extends TestCase
|
||||
}
|
||||
}
|
||||
|
||||
private static int $cnt = 0;
|
||||
|
||||
#[DataProvider('provideSettingsKeysValues')]
|
||||
public function testBasic(Settings $settings, KeyType $keyType, string|int $key, ValueType $valueType, mixed $value): void
|
||||
{
|
||||
$cnt = self::$cnt++;
|
||||
$field = new FieldConfig(
|
||||
'testBasic',
|
||||
"testBasic_$cnt",
|
||||
$settings,
|
||||
$keyType,
|
||||
$valueType
|
||||
|
Loading…
Reference in New Issue
Block a user