HiveBrain v1.2.0
Get Started
← Back to all entries
patternphpMinor

ZF2 Model Mapper

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
zf2modelmapper

Problem

In the ZF2 model mapper, mappings are quite often duplicated in the original mapping class & other mapping classes.

I am using a static mapping methods (mapToInternal, mapToExternal) to remove this duplication. Could this be done any better?

id) ? : $item->setId($external->id);
    empty($external->account_id) ? : $item->setAccountId($external->account_id);
    empty($external->title) ? : $item->setTitle($external->title);
    empty($external->description) ? : $item->setDescription($external->description);
    empty($external->created_on) ? : $item->setCreatedOn($external->created_on);
    empty($external->updated_on) ? : $item->setUpdatedOn($external->updated_on);

    return $item;
}

/**
 * @param ItemEntity $item
 * @return array
 */
static public function mapToExternal(ItemEntity $item)
{
    $data = array(
        'id'          => $item->getId(),
        // @todo complete once login is done
        'account_id'  => 1, //$item->getAccountId(),
        'title'       => $item->getTitle(),
        'description' => $item->getDescription(),
        'updated_on'  => $item->getUpdatedOn(),
        'created_on'  => $item->getCreatedOn(),
    );

    return $data;
}

/**
 * return array
 */
public function findAll()
{
    $response = $this->getDao()->findAll();

    $items = array();
    foreach ($response as $item) {
        $itemEntity = self::mapToInternal($item);
        $statisticEntity = ItemStatisticTable::mapToInternal($item);
        $itemEntity->setStatistic($statisticEntity);

        $items[] = $itemEntity;
    }

    return $items;
}
// ...

Solution

Two minor notes:

-
This function uses only data of ItemEntity:

static public function mapToExternal(ItemEntity $item)
{
    $data = array(
        'id'          => $item->getId(),
        // @todo complete once login is done
        'account_id'  => 1, //$item->getAccountId(),
        'title'       => $item->getTitle(),
        'description' => $item->getDescription(),
        'updated_on'  => $item->getUpdatedOn(),
        'created_on'  => $item->getCreatedOn(),
    );

    return $data;
}


I would consider putting this function into the ItemEntity class. It seems data envy.

-
It's confusing here that $items does not contain any $item:

$items = array();
foreach ($response as $item) {
    $itemEntity = self::mapToInternal($item);
    $statisticEntity = ItemStatisticTable::mapToInternal($item);
    $itemEntity->setStatistic($statisticEntity);

    $items[] = $itemEntity;
}

return $items;


I'd rename it to $result to describe its purpose.

Code Snippets

static public function mapToExternal(ItemEntity $item)
{
    $data = array(
        'id'          => $item->getId(),
        // @todo complete once login is done
        'account_id'  => 1, //$item->getAccountId(),
        'title'       => $item->getTitle(),
        'description' => $item->getDescription(),
        'updated_on'  => $item->getUpdatedOn(),
        'created_on'  => $item->getCreatedOn(),
    );

    return $data;
}
$items = array();
foreach ($response as $item) {
    $itemEntity = self::mapToInternal($item);
    $statisticEntity = ItemStatisticTable::mapToInternal($item);
    $itemEntity->setStatistic($statisticEntity);

    $items[] = $itemEntity;
}

return $items;

Context

StackExchange Code Review Q#26116, answer score: 2

Revisions (0)

No revisions yet.