Initial SQLite development (#1)

Reviewed-on: #1
This commit was merged in pull request #1.
This commit is contained in:
2024-06-08 23:58:45 +00:00
parent e91acee70f
commit f784f3e52c
66 changed files with 5509 additions and 2 deletions

View File

@@ -0,0 +1,17 @@
<?php declare(strict_types=1);
namespace BitBadger\PDODocument\Mapper;
/**
* A mapper that returns the associative array from the database
*/
class ArrayMapper implements Mapper
{
/**
* @inheritDoc
*/
public function map(array $result): array
{
return $result;
}
}

View File

@@ -0,0 +1,17 @@
<?php declare(strict_types=1);
namespace BitBadger\PDODocument\Mapper;
/**
* A mapper that returns the integer value of the first item in the results
*/
class CountMapper implements Mapper
{
/**
* @inheritDoc
*/
public function map(array $result): int
{
return (int) $result[0];
}
}

View File

@@ -0,0 +1,44 @@
<?php declare(strict_types=1);
namespace BitBadger\PDODocument\Mapper;
use BitBadger\PDODocument\DocumentException;
use JsonMapper;
use JsonMapper_Exception;
/**
* Map domain class instances from JSON documents
*
* @template TDoc The type of document returned by this mapper
* @implements Mapper<TDoc> Provide a mapping from JSON
*/
class DocumentMapper implements Mapper
{
/**
* Constructor
*
* @param class-string<TDoc> $className The type of class to be returned by this mapping
* @param string $fieldName The name of the field (optional; defaults to `data`)
*/
public function __construct(public string $className, public string $fieldName = 'data') { }
/**
* Map a result to a domain class instance
*
* @param array $result An associative array representing a single database result
* @return TDoc The document, deserialized from its JSON representation
* @throws DocumentException If the JSON cannot be deserialized
*/
public function map(array $result): mixed
{
try {
$json = json_decode($result[$this->fieldName]);
if (is_null($json)) {
throw new DocumentException("Could not map document for $this->className: " . json_last_error_msg());
}
return (new JsonMapper())->map($json, $this->className);
} catch (JsonMapper_Exception $ex) {
throw new DocumentException("Could not map document for $this->className", previous: $ex);
}
}
}

View File

@@ -0,0 +1,24 @@
<?php declare(strict_types=1);
namespace BitBadger\PDODocument\Mapper;
use BitBadger\PDODocument\{Configuration, DocumentException, Mode};
/**
* Map an EXISTS result to a boolean value
*/
class ExistsMapper implements Mapper
{
/**
* @inheritDoc
* @throws DocumentException If the database mode has not been set
*/
public function map(array $result): bool
{
return match (Configuration::$mode) {
Mode::PgSQL => (bool)$result[0],
Mode::SQLite => (int)$result[0] > 0,
default => throw new DocumentException('Database mode not set; cannot map existence result'),
};
}
}

19
src/Mapper/Mapper.php Normal file
View File

@@ -0,0 +1,19 @@
<?php declare(strict_types=1);
namespace BitBadger\PDODocument\Mapper;
/**
* Map an associative array of results to a domain class
*
* @template T The type of document retrieved by this mapper
*/
interface Mapper
{
/**
* Map a result to the specified type
*
* @param array $result An associative array representing a single database result
* @return T The item mapped from the given result
*/
public function map(array $result): mixed;
}

View File

@@ -0,0 +1,30 @@
<?php declare(strict_types=1);
namespace BitBadger\PDODocument\Mapper;
/**
* Map a string result from the
*
* @implements Mapper<string>
*/
class StringMapper implements Mapper
{
/**
* Constructor
*
* @param string $fieldName The name of the field to be retrieved as a string
*/
public function __construct(public string $fieldName) { }
/**
* @inheritDoc
*/
public function map(array $result): ?string
{
return match (false) {
key_exists($this->fieldName, $result) => null,
is_string($result[$this->fieldName]) => "{$result[$this->fieldName]}",
default => $result[$this->fieldName]
};
}
}