/** * {@inheritDoc} */ public function read(Request $request) { $manager = $this->getServiceLocator()->get('Omeka\\ModuleManager'); $response = new Response(); $representation = $this->getRepresentation($manager->getModule($request->getId())); $response->setContent($representation); return $response; }
/** * Set an error result to the MvcEvent and return the result. * * @param MvcEvent $event * @param Exception $error */ protected function getErrorResult(MvcEvent $event, \Exception $error) { $response = new Response(); $response->setStatus(Response::ERROR); $result = new ApiJsonModel($response, $this->getViewOptions()); $result->setException($error); $event->setResult($result); return $result; }
public function testConstructorSetsProperties() { $response = new Response('content'); $this->assertEquals('content', $response->getContent()); }
/** * Check whether the response content is valid. * * A valid response content is a representation object or an array * containing representation objects. * * @param Response $response * @return bool */ protected function isValidResponseContent(Response $response) { $content = $response->getContent(); if ($content instanceof RepresentationInterface) { return true; } if (is_array($content)) { foreach ($content as $representation) { if (!$representation instanceof RepresentationInterface) { return false; } } return true; } return false; }
/** * {@inheritDoc} */ public function search(Request $request) { $query = $request->getContent(); // Set default query parameters if (!isset($query['page'])) { $query['page'] = null; } if (!isset($query['per_page'])) { $query['per_page'] = null; } if (!isset($query['limit'])) { $query['limit'] = null; } if (!isset($query['offset'])) { $query['offset'] = null; } if (!isset($query['sort_by'])) { $query['sort_by'] = null; } if (isset($query['sort_order']) && in_array(strtoupper($query['sort_order']), ['ASC', 'DESC'])) { $query['sort_order'] = strtoupper($query['sort_order']); } else { $query['sort_order'] = 'ASC'; } // Begin building the search query. $entityClass = $this->getEntityClass(); $qb = $this->getEntityManager()->createQueryBuilder()->select($entityClass)->from($entityClass, $entityClass); $this->buildQuery($qb, $query); $qb->groupBy("{$entityClass}.id"); // Trigger the search.query event. $event = new Event(Event::API_SEARCH_QUERY, $this, ['services' => $this->getServiceLocator(), 'queryBuilder' => $qb, 'request' => $request]); $this->getEventManager()->trigger($event); // Get the total results. $totalResultsQb = clone $qb; $totalResults = $totalResultsQb->resetDQLPart('groupBy')->select("COUNT(DISTINCT {$entityClass}.id)")->getQuery()->getSingleScalarResult(); // Do not make the request if the max results (limit) is 0. Useful if // the only information needed is total results. $representations = []; if ($qb->getMaxResults() || null === $qb->getMaxResults()) { // Finish building the search query. In addition to any sorting the // adapters add, always sort by entity ID. $this->sortQuery($qb, $query); $qb->addOrderBy("{$entityClass}.id", $query['sort_order']); $this->limitQuery($qb, $query); foreach ($qb->getQuery()->getResult() as $entity) { $representations[] = $this->getRepresentation($entity); } } $response = new Response($representations); $response->setTotalResults($totalResults); return $response; }
/** * Detect and account for API response errors. */ protected function detectError(Response $response) { if ($response->getStatus() === Response::ERROR_VALIDATION) { $this->getController()->messenger()->addError('There was an error during validation'); } }