Skip to content

Instantly share code, notes, and snippets.

@matuszeman
Created August 19, 2014 14:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save matuszeman/b797d9c70a4ce9e0974a to your computer and use it in GitHub Desktop.
Save matuszeman/b797d9c70a4ce9e0974a to your computer and use it in GitHub Desktop.
renderEntity and renderCollection.entity examples
<?php
//...
class Module implements ApigilityProviderInterface
{
public function onBootstrap($e)
{
$app = $e->getTarget();
$this->sm = $app->getServiceManager();
$events = $app->getEventManager();
$events->attach(MvcEvent::EVENT_RENDER, array($this, 'onRender'), 110);
}
public function onRender($e)
{
$helpers = $this->sm->get('ViewHelperManager');
$hal = $helpers->get('hal');
$hal->getEventManager()->attach(['renderCollection.entity'], array($this, 'onRenderCollectionEntity'));
$hal->getEventManager()->attach(['renderEntity'], array($this, 'onRenderCollectionEntity'));
}
public function onRenderCollectionEntity($e)
{
if($e->getName() === 'renderEntity') {
//zf-hal is inconsistent and returns HalEntity if it's 'renderEntity' event ... vvv
$entity = $e->getParam('entity')->entity;
}
else {
// ^^^ ... and returns your domain entity if it's 'renderCollection.entity' event
$entity = $e->getParam('entity');
}
//if it's StockTransactionEntity entity 'enrich' it with with '_embedded.identity' entity
if($entity instanceof StockTransactionEntity) {
$identityRepository = $this->sm->get('KapSecurity\IdentityRepository');
$identity = $identityRepository->find($entity['identity_id']);
$entity['identity'] = $identity;
}
// ... etc. etc.
if($entity instanceof InventoryItemEntity) {
$repository = $this->sm->get('Stock\ItemRepository');
$item = $repository->find($entity['item_id']);
$entity['item'] = $item;
}
if($entity instanceof OrderItemEntity) {
$repository = $this->sm->get('Stock\InventoryItemRepository');
$item = $repository->find($entity['inventory_item_id']);
$entity['inventory_item'] = $item;
$repository = $this->sm->get('Stock\ItemRepository');
$item = $repository->find($item['item_id']);
$entity['item'] = $item;
$repository = $this->sm->get('Stock\OrderRepository');
$order = $repository->find($entity['order_id']);
$entity['order'] = $order;
$identityRepository = $this->sm->get('KapSecurity\IdentityRepository');
$identity = $identityRepository->find($order['owner_id']);
$entity['owner'] = $identity;
}
if($entity instanceof OrderEntity) {
$identityRepository = $this->sm->get('KapSecurity\IdentityRepository');
$identity = $identityRepository->find($entity['owner_id']);
$entity['identity'] = $identity;
}
}
//...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment