/** * Dispatch an incoming event to the appropriate method * * Marshals arguments from the event parameters. * * @param ResourceEvent $event * @return mixed */ public function dispatch(ResourceEvent $event) { $this->event = $event; switch ($event->getName()) { case 'create': $data = $event->getParam('data', []); return $this->create($data); case 'delete': $id = $event->getParam('id', null); return $this->delete($id); case 'deleteList': $data = $event->getParam('data', []); return $this->deleteList($data); case 'fetch': $id = $event->getParam('id', null); return $this->fetch($id); case 'fetchAll': $queryParams = $event->getQueryParams() ?: []; return $this->fetchAll($queryParams); case 'patch': $id = $event->getParam('id', null); $data = $event->getParam('data', []); return $this->patch($id, $data); case 'patchList': $data = $event->getParam('data', []); return $this->patchList($data); case 'replaceList': $data = $event->getParam('data', []); return $this->replaceList($data); case 'update': $id = $event->getParam('id', null); $data = $event->getParam('data', []); return $this->update($id, $data); default: throw new Exception\RuntimeException(sprintf('%s has not been setup to handle the event "%s"', __METHOD__, $event->getName())); } }
/** * @param ResourceEvent $event * @return CommandInterface */ protected function createCommand(ResourceEvent $event) { $normalizer = $this->getNormalizer(); if ($normalizer === null) { throw new Exception\ConfigException('Cannot use request to command mapping; no Normalizer provided'); } $commandMapping = $this->getRequestCommandMapping($event->getName()); if (!isset($commandMapping['command_class'])) { throw new Exception\ConfigException(sprintf('Invalid request to command mapping configuration for event "%s"', $event->getName())); } $commandClass = $commandMapping['command_class']; $data = array(); switch ($event->getName()) { case 'create': case 'deleteList': case 'patch': case 'patchList': case 'patchMultiple': case 'update': case 'replaceList': $data = $this->getBodyParams($event); // Return filtered data if input filter is present $inputFilter = $this->getInputFilter(); // When there is no data, the input filter returns all fields with default value, // therefore do not use input filter when body params are empty if ($inputFilter !== null && !empty($data)) { $data = $inputFilter->getValues(); } break; case 'fetchAll': /// Note that the paging related params are already transformed and JSON params are decoded... $data = $this->getQueryParams($event, false, false); break; default: // Do nothing break; } if (is_a($commandClass, CollectionCommandInterface::CLASS, true)) { $collectionCommand = $commandClass::create(); $collection = $normalizer->denormalize($data, 'array<' . $collectionCommand->getObjectClassName() . '>'); $collectionCommand->setCollectionData($collection); return $collectionCommand; } /** @todo The normalizer should know from which version to denormalize from */ return $normalizer->denormalize($data, $commandClass); }