/**
  * Handle the dispatch event
  *
  * Does several "pre-flight" checks:
  * - Raises an exception if no resource is composed.
  * - Raises an exception if no route is composed.
  * - Returns a 405 response if the current HTTP request method is not in
  *   $options
  *
  * When the dispatch is complete, it will check to see if an array was
  * returned; if so, it will cast it to a view model using the
  * AcceptableViewModelSelector plugin, and the $acceptCriteria property.
  *
  * @param  MvcEvent $e
  * @return mixed
  * @throws DomainException
  */
 public function onDispatch(MvcEvent $e)
 {
     if (!$this->getResource()) {
         throw new DomainException(sprintf('%s requires that a %s\\ResourceInterface object is composed; none provided', __CLASS__, __NAMESPACE__));
     }
     if (!$this->route) {
         throw new DomainException(sprintf('%s requires that a route name for the resource is composed; none provided', __CLASS__));
     }
     // Check for an API-Problem in the event
     $return = $e->getParam('api-problem', false);
     // If no return value dispatch the parent event
     if (!$return) {
         $return = parent::onDispatch($e);
     }
     if (!$return instanceof ApiProblem && !$return instanceof JsonLDEntity && !$return instanceof JsonLDCollection) {
         return $return;
     }
     if ($return instanceof ApiProblem) {
         return new ApiProblemResponse($return);
     }
     // Set the fallback content negotiation to use HalJson.
     $e->setParam('ZFContentNegotiationFallback', 'JsonLD');
     // Use content negotiation for creating the view model
     $viewModel = new ContentNegotiationViewModel(['payload' => $return]);
     $e->setResult($viewModel);
     return $viewModel;
 }
 public function onDispatch(MvcEvent $event)
 {
     $routeMatch = $event->getRouteMatch();
     /** @var \Zend\Http\Request $request */
     $request = $event->getRequest();
     $method = strtolower($request->getMethod());
     if ($routeMatch && $method === 'patch') {
         $id = $this->getIdentifier($routeMatch, $request);
         $data = $this->processBodyContent($request);
         // We assume we have to patch multiple resources when
         // there's a comma (the separator) in the ID.
         if ($id !== false && strpos($id, ',') !== false) {
             $ids = explode(',', $id);
             $return = $this->patchMultiple($ids, $data);
             $routeMatch->setParam('action', 'patchMultiple');
             $event->setResult($return);
             return $this->processResult($event, $return);
         }
     }
     return parent::onDispatch($event);
 }
 public function testOnDispatchRaisesDomainExceptionOnMissingRoute()
 {
     $controller = new RestController();
     $controller->setResource($this->resource);
     $this->setExpectedException('ZF\\ApiProblem\\Exception\\DomainException', 'route');
     $controller->onDispatch($this->event);
 }