Files
pdo-document/src/Mapper/StringMapper.php
T

37 lines
828 B
PHP
Raw Normal View History

2024-07-20 21:47:21 -04:00
<?php
/**
* @author Daniel J. Summers <daniel@bitbadger.solutions>
* @license MIT
*/
declare(strict_types=1);
2024-06-08 23:58:45 +00:00
namespace BitBadger\PDODocument\Mapper;
/**
2024-07-20 21:47:21 -04:00
* Map a string result from the named field
2024-06-08 23:58:45 +00:00
*
* @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]
};
}
}