Moved DbTools::detectType to DbType::detect.

This commit is contained in:
flash 2024-10-19 15:16:22 +00:00
parent 010045b14a
commit f137dfaab5
8 changed files with 57 additions and 58 deletions

View file

@ -1 +1 @@
0.2410.191502 0.2410.191515

View file

@ -24,7 +24,7 @@ interface DbStatement {
* *
* @param int $ordinal Index of the target parameter. * @param int $ordinal Index of the target parameter.
* @param mixed $value Value to assign to the parameter. * @param mixed $value Value to assign to the parameter.
* @param int $type Type of the value, if left to DbType::AUTO DbTools::detectType will be used on $value. * @param int $type Type of the value, if left to DbType::AUTO DbType::detect will be used on $value.
* @throws InvalidArgumentException If $ordinal exceeds bounds. * @throws InvalidArgumentException If $ordinal exceeds bounds.
*/ */
function addParameter(int $ordinal, mixed $value, int $type = DbType::AUTO): void; function addParameter(int $ordinal, mixed $value, int $type = DbType::AUTO): void;
@ -36,7 +36,7 @@ interface DbStatement {
* Overwriting lower ordinals than the current cursor should be fine, but your mileage may vary. * Overwriting lower ordinals than the current cursor should be fine, but your mileage may vary.
* *
* @param mixed $value Value to assign to the parameter. * @param mixed $value Value to assign to the parameter.
* @param int $type Type of the value, if left to DbType::AUTO DbTools::detectType will be used on $value. * @param int $type Type of the value, if left to DbType::AUTO DbType::detect will be used on $value.
* @throws RuntimeException If all parameters have already been specified. * @throws RuntimeException If all parameters have already been specified.
*/ */
function nextParameter(mixed $value, int $type = DbType::AUTO): void; function nextParameter(mixed $value, int $type = DbType::AUTO): void;

View file

@ -11,24 +11,6 @@ use Countable;
* Common database actions. * Common database actions.
*/ */
final class DbTools { final class DbTools {
/**
* Detects the DbType of the passed argument. Should be used for DbType::AUTO.
*
* @param mixed $value A value of unknown type.
* @return int DbType of the value passed in the argument.
*/
public static function detectType(mixed $value): int {
if(is_null($value))
return DbType::NULL;
if(is_float($value))
return DbType::FLOAT;
if(is_int($value))
return DbType::INTEGER;
if(is_resource($value))
return DbType::BLOB;
return DbType::STRING;
}
/** /**
* Constructs a partial query for prepared statements of lists. * Constructs a partial query for prepared statements of lists.
* *

View file

@ -1,7 +1,7 @@
<?php <?php
// DbType.php // DbType.php
// Created: 2021-05-02 // Created: 2021-05-02
// Updated: 2024-10-04 // Updated: 2024-10-19
namespace Index\Db; namespace Index\Db;
@ -10,7 +10,7 @@ namespace Index\Db;
*/ */
final class DbType { final class DbType {
/** /**
* Automatically detect the type. Should be used in combination with DbTools::detectType. * Automatically detect the type. Should be used in combination with DbType::detect.
* *
* @var int * @var int
*/ */
@ -50,4 +50,22 @@ final class DbType {
* @var int * @var int
*/ */
public const BLOB = 5; public const BLOB = 5;
/**
* Detects the DbType of the passed argument. Should be used for DbType::AUTO.
*
* @param mixed $value A value of unknown type.
* @return int DbType of the value passed in the argument.
*/
public static function detect(mixed $value): int {
if(is_null($value))
return self::NULL;
if(is_float($value))
return self::FLOAT;
if(is_int($value))
return self::INTEGER;
if(is_resource($value))
return self::BLOB;
return self::STRING;
}
} }

View file

@ -1,11 +1,11 @@
<?php <?php
// MariaDbParameter.php // MariaDbParameter.php
// Created: 2021-05-02 // Created: 2021-05-02
// Updated: 2024-10-04 // Updated: 2024-10-19
namespace Index\Db\MariaDb; namespace Index\Db\MariaDb;
use Index\Db\{DbTools,DbType}; use Index\Db\DbType;
/** /**
* Represents a bound parameter. * Represents a bound parameter.
@ -25,7 +25,7 @@ class MariaDbParameter {
private mixed $value private mixed $value
) { ) {
if($type == DbType::AUTO) if($type == DbType::AUTO)
$type = DbTools::detectType($value); $type = DbType::detect($value);
if($type === DbType::NULL) if($type === DbType::NULL)
$value = null; $value = null;
} }

View file

@ -9,7 +9,7 @@ use SQLite3Result;
use SQLite3Stmt; use SQLite3Stmt;
use InvalidArgumentException; use InvalidArgumentException;
use RuntimeException; use RuntimeException;
use Index\Db\{DbTools,DbType,DbStatement,DbResult}; use Index\Db\{DbType,DbStatement,DbResult};
/** /**
* Represents a prepared SQLite SQL statement. * Represents a prepared SQLite SQL statement.
@ -47,7 +47,7 @@ class SqliteStatement implements DbStatement {
private function bindParameter(int $ordinal, mixed $value, int $type): void { private function bindParameter(int $ordinal, mixed $value, int $type): void {
if($type === DbType::AUTO) if($type === DbType::AUTO)
$type = DbTools::detectType($value); $type = DbType::detect($value);
if($type === DbType::NULL) if($type === DbType::NULL)
$value = null; $value = null;

View file

@ -1,30 +0,0 @@
<?php
// DbToolsTest.php
// Created: 2021-04-28
// Updated: 2024-10-16
declare(strict_types=1);
use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\Attributes\CoversClass;
use Index\Db\{DbTools,DbType};
#[CoversClass(DbTools::class)]
#[CoversClass(DbType::class)]
final class DbToolsTest extends TestCase {
public function testDetectType(): void {
$this->assertEquals(DbType::NULL, DbTools::detectType(null));
$this->assertEquals(DbType::INTEGER, DbTools::detectType(12345));
$this->assertEquals(DbType::FLOAT, DbTools::detectType(123.45));
$this->assertEquals(DbType::STRING, DbTools::detectType('This is a string.'));
$blob = fopen('php://memory', 'r+b');
if($blob === false)
throw new RuntimeException('failed to fopen a memory stream');
fwrite($blob, 'This is a string inside a memory stream.');
fseek($blob, 0);
$this->assertEquals(DbType::BLOB, DbTools::detectType($blob));
}
}

29
tests/DbTypeTest.php Normal file
View file

@ -0,0 +1,29 @@
<?php
// DbTypeTest.php
// Created: 2021-04-28
// Updated: 2024-10-19
declare(strict_types=1);
use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\Attributes\CoversClass;
use Index\Db\DbType;
#[CoversClass(DbType::class)]
final class DbTypeTest extends TestCase {
public function testDetectType(): void {
$this->assertEquals(DbType::NULL, DbType::detect(null));
$this->assertEquals(DbType::INTEGER, DbType::detect(12345));
$this->assertEquals(DbType::FLOAT, DbType::detect(123.45));
$this->assertEquals(DbType::STRING, DbType::detect('This is a string.'));
$blob = fopen('php://memory', 'r+b');
if($blob === false)
throw new RuntimeException('failed to fopen a memory stream');
fwrite($blob, 'This is a string inside a memory stream.');
fseek($blob, 0);
$this->assertEquals(DbType::BLOB, DbType::detect($blob));
}
}