37 lines
829 B
PHP
37 lines
829 B
PHP
<?php
|
|
/**
|
|
* @author Daniel J. Summers <daniel@bitbadger.solutions>
|
|
* @license MIT
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace BitBadger\PDODocument\Mapper;
|
|
|
|
/**
|
|
* Map a string result from the named field
|
|
*
|
|
* @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],
|
|
};
|
|
}
|
|
}
|