Initial SQLite development (#1)
Reviewed-on: #1
This commit was merged in pull request #1.
This commit is contained in:
17
src/Mapper/ArrayMapper.php
Normal file
17
src/Mapper/ArrayMapper.php
Normal 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;
|
||||
}
|
||||
}
|
||||
17
src/Mapper/CountMapper.php
Normal file
17
src/Mapper/CountMapper.php
Normal 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];
|
||||
}
|
||||
}
|
||||
44
src/Mapper/DocumentMapper.php
Normal file
44
src/Mapper/DocumentMapper.php
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
24
src/Mapper/ExistsMapper.php
Normal file
24
src/Mapper/ExistsMapper.php
Normal 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
19
src/Mapper/Mapper.php
Normal 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;
|
||||
}
|
||||
30
src/Mapper/StringMapper.php
Normal file
30
src/Mapper/StringMapper.php
Normal 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]
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user