Created
August 19, 2014 14:38
-
-
Save matuszeman/b797d9c70a4ce9e0974a to your computer and use it in GitHub Desktop.
renderEntity and renderCollection.entity examples
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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