Initial SQLite development (#1)
Reviewed-on: #1
This commit was merged in pull request #1.
This commit is contained in:
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user