<?php

namespace Cloudtek\DynamoDM\Iterator;

use Cloudtek\DynamoDM\Type\MapType;
use Cloudtek\DynamoDM\Type\Type;

/**
 * Wraps an \Iterator, adding unmarshalling behavior.
 *
 * @internal
 */
final class UnmarshallingIterator extends IteratorIterator
{
    /**
     * {@inheritdoc}
     */
    public function current()
    {
        return $this->unmarshal(parent::current());
    }

    /**
     * @param array|null $item
     *
     * @return object|null
     */
    private function unmarshal(array $item)
    {
        if ($item === null) {
            return null;
        }

        $map = [MapType::$dynamoKey => $item];

        return Type::convertUnmappedToPHPValue($map);
    }
}