Sometimes, classes have requirements that make them impossible to map automatically. For these cases there is a
MapsItself
interface that defines one
static function: mapObject
.
When the mapper comes across a class that implements this interface, instead of analysing and mapping the class, the mapper will
call the MapsItself
with the provided data and use its result as the mapped value.
use Jerodev\DataMapper\MapsItself;
class UserAdmin implements MapsItself
{
public string $username;
public array $users;
public function __construct(UserRepository $userRepository)
{
$this->users = $userRepository->getUsers();
}
public static function mapSelf(array $data, Mapper $mapper): self
{
$userRepository = self::getContainer()->get(UserRepository::class);
$me = new self($userRepository);
$me->username = $data['username'];
return $me;
}
}