public function applyFiltersAndOrderBy(QueryBuilder $queryBuilder, ResourceEvent $event, $entityClass, $parameters) { $metadata = $this->getObjectManager()->getClassMetadata($entityClass); $query = $event->getRequest()->getQuery(); $this->getFilterManager()->filter($queryBuilder, $metadata, $query['filter']); $this->getOrderByManager()->orderBy($queryBuilder, $metadata, $query['order-by']); }
/** * @param string $entityClass * @param array $data * * @return array */ public function filter(ResourceEvent $event, $entityClass, $data) { $validate = $this->validateOAuth2(); if ($validate instanceof ApiProblem) { return $validate; } $request = $event->getRequest()->getQuery()->toArray(); $identity = $event->getIdentity()->getAuthenticationIdentity(); $data->user = $identity['user_id']; return $data; }
/** * @group 7 */ public function testDispatchEvent() { $queryParams = new Parameters(['foo' => 'bar']); $event = new ResourceEvent(); $event->setName('fetchAll'); $event->setQueryParams($queryParams); // $event->setParam('user_id', 1); $event->setRouteMatch(new RouteMatch(['user_id' => 1])); $result = $this->listener->dispatch($event); $this->assertInstanceOf(ResultSet::class, $result); }
/** * @param ResourceEvent $event * @param string $entityClass * @param array $parameters * * @return QueryBuilder */ public function createQuery(ResourceEvent $event, $entityClass, $parameters) { $noteId = $event->getParam('id'); /** @var Entity\Repository\User $userRepository */ $userRepository = $this->getObjectManager()->getRepository('Secretary\\Entity\\User'); /** @var Entity\User $user */ $user = $userRepository->findOneBy(array('email' => $event->getIdentity()->getName())); /** @var Entity\Repository\Note $noteRepository */ $noteRepository = $this->getObjectManager()->getRepository($entityClass); $queryBuilder = $noteRepository->fetchNoteWithUserDataQuery($noteId, $user->getId()); return $queryBuilder; }
public function createQuery(ResourceEvent $event, $entityClass, $parameters) { $validate = $this->validateOAuth2(null); if ($validate instanceof ApiProblem) { return $validate; } $identity = $event->getIdentity()->getAuthenticationIdentity(); $userId = $identity['user_id']; $user = $this->getObjectManager()->getRepository('RollNApi\\Entity\\User')->find($userId); $queryBuilder = $this->getObjectManager()->createQueryBuilder(); $queryBuilder->select('row')->from($entityClass, 'row')->andwhere('row.user = :user')->setParameter('user', $user); return $queryBuilder; }
/** * {@inheritDoc} */ public function createQuery(ResourceEvent $event, $entityClass, $parameters) { $request = $event->getRequest()->getQuery()->toArray(); $queryBuilder = $this->getObjectManager()->createQueryBuilder(); $queryBuilder->select('row')->from($entityClass, 'row'); if (isset($request[$this->getFilterKey()])) { $metadata = $this->getObjectManager()->getClassMetadata($entityClass); $this->getFilterManager()->filter($queryBuilder, $metadata, $request[$this->getFilterKey()]); } if (isset($request[$this->getOrderByKey()])) { $metadata = $this->getObjectManager()->getClassMetadata($entityClass); $this->getOrderByManager()->orderBy($queryBuilder, $metadata, $request[$this->getOrderByKey()]); } return $queryBuilder; }
/** * @param string $entityClass * @param array $parameters * * @return mixed This will return an ORM or ODM Query\Builder */ public function createQuery(ResourceEvent $event, $entityClass, $parameters) { $queryBuilder = $this->getObjectManager()->createQueryBuilder(); $queryBuilder->select($this->entityAlias)->from($entityClass, $this->entityAlias); $routeParams = $event->getRouteMatch()->getParams(); $where = $event->getQueryParam('where', []); if (is_array($where)) { $where = array_merge($where, $routeParams); unset($where['controller'], $where['version'], $where['action']); } // trigger event for manipulating $where $providerEvent = new Event(Event::EVENT_WHERE, $where, array('entityClass' => $entityClass)); $providerEvent->setEntityManager($this->getObjectManager()); $this->getEventManager()->trigger($providerEvent); $where = $providerEvent->getTarget(); if ($where) { $where = $this->parseWhere($where); $this->addWhere($queryBuilder, $where); } // trigger event for manipulating $order $order = $event->getQueryParam('order', false); $providerEvent = new Event(Event::EVENT_ORDER, $order, array('entityClass' => $entityClass)); $providerEvent->setEntityManager($this->getObjectManager()); $this->getEventManager()->trigger($providerEvent); $order = $providerEvent->getTarget(); if ($order) { $this->addOrder($queryBuilder, $order); } // trigger event for manipulating $limit $limit = $event->getQueryParam('limit', false); $providerEvent = new Event(Event::EVENT_LIMIT, $limit, array('entityClass' => $entityClass)); $providerEvent->setEntityManager($this->getObjectManager()); $this->getEventManager()->trigger($providerEvent); $limit = $providerEvent->getTarget(); if ($limit) { $this->addLimit($queryBuilder, $limit); } // trigger event for manipulating $where $providerEvent = new Event(Event::EVENT_QUERYBUILDER, $queryBuilder, array('entityClass' => $entityClass)); $providerEvent->setEntityManager($this->getObjectManager()); $this->getEventManager()->trigger($providerEvent); return $queryBuilder; }
public function createQuery(ResourceEvent $event, $entityClass, $parameters) { $validate = $this->validateOAuth2(null); if ($validate instanceof ApiProblem) { return $validate; } $request = $this->getServiceLocator()->getServiceLocator()->get('Request')->getQuery()->toArray(); $identity = $event->getIdentity()->getAuthenticationIdentity(); $userId = $identity['user_id']; $user = $this->getObjectManager()->getRepository('RollNApi\\Entity\\User')->find($userId); $queryBuilder = $this->getObjectManager()->createQueryBuilder(); $queryBuilder->select('row')->from($entityClass, 'row')->andwhere('row.user = :user')->setParameter('user', $user); if (isset($request['filter'])) { $metadata = $this->getObjectManager()->getMetadataFactory()->getAllMetadata(); $filterManager = $this->getServiceLocator()->getServiceLocator()->get('ZfDoctrineQueryBuilderFilterManagerOrm'); $filterManager->filter($queryBuilder, $metadata[0], $request['filter']); } if (isset($request['order-by'])) { $metadata = $this->getObjectManager()->getMetadataFactory()->getAllMetadata(); $orderByManager = $this->getServiceLocator()->getServiceLocator()->get('ZfDoctrineQueryBuilderOrderByManagerOrm'); $orderByManager->orderBy($queryBuilder, $metadata[0], $request['order-by']); } return $queryBuilder; }
/** * Prepare event parameters * * Merges any event parameters set in the resources with arguments passed * to a resource method, and passes them to the `prepareArgs` method of the * event manager. * * If an input filter is composed, this, too, is injected into the event. * * @param string $name * @param array $args * @return ResourceEvent */ protected function prepareEvent($name, array $args) { $event = new ResourceEvent($name, $this, $this->prepareEventParams($args)); $event->setIdentity($this->getIdentity()); $event->setInputFilter($this->getInputFilter()); $event->setQueryParams($this->getQueryParams()); $event->setRouteMatch($this->getRouteMatch()); return $event; }
/** * 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())); } }
/** * @depends testRequestIsMutable */ public function testRequestIsNullable(ResourceEvent $event) { $event->setRequest(null); $this->assertNull($event->getRequest()); }
/** * @depends testFetch */ public function testGetResource() { $this->imgManMock = $this->getMockBuilder('ImgMan\\Service\\ImageService')->disableOriginalConstructor()->getMock(); $resource = new ImgManConnectedResource($this->imgManMock); $this->assertNull($resource->getResource()); $this->imgManMock = $this->getMockBuilder('ImgMan\\Service\\ImageService')->disableOriginalConstructor()->setMethods(['has', 'get', 'getSrc'])->getMock(); $this->imgManMock->expects($this->any())->method('has')->with($this->equalTo('testId'), $this->equalTo(CoreInterface::RENDITION_ORIGINAL))->will($this->returnValue(true)); $this->imgManMock->expects($this->any())->method('getSrc')->with($this->equalTo('testId'), $this->equalTo(CoreInterface::RENDITION_ORIGINAL))->will($this->returnValue(null)); $this->imgManMock->expects($this->any())->method('get')->with($this->equalTo('testId'))->will($this->returnValue($this->getMock('ImgManTest\\Apigility\\Asset\\TestImage'))); $resource = new ImgManConnectedResource($this->imgManMock); $event = new ResourceEvent(); $event->setTarget(new Resource()); $event->setName('fetch'); $event->setParam('id', 'testId'); $event->setRequest(new Request()); $resource->setEntityClass('ImgManTest\\Apigility\\Asset\\TestImage'); $resource->dispatch($event); $this->assertInstanceOf('ZF\\Rest\\ResourceInterface', $resource->getResource()); }
/** * @group 7 */ public function testDispatchShouldPassWhitelistedQueryParamsToFetchAllMethod() { $queryParams = new Parameters(array('foo' => 'bar')); $event = new ResourceEvent(); $event->setName('fetchAll'); $event->setQueryParams($queryParams); $this->listener->dispatch($event); $this->assertEquals($queryParams, $this->listener->testCase->paramsPassedToListener); }
/** * @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); }