Ejemplo n.º 1
0
 public function testParsingARequestWithPagination()
 {
     // Given...
     $has = function ($param) {
         return in_array($param, ['page', 'size']);
     };
     $get = function ($param) {
         return 'page' == $param ? 2 : 4;
     };
     $queryOverrides = ['has' => $has, 'get' => $get];
     $request = self::createRequest('/api/v1/users', $queryOverrides);
     $params = Stub::makeEmpty('GoIntegro\\Hateoas\\JsonApi\\Request\\Params', ['primaryClass' => self::RESOURCE_CLASS]);
     $parser = new PaginationParser(self::createMetadataMiner(), self::$config);
     // When...
     $pagination = $parser->parse($request, $params);
     // Then...
     $this->assertNotNull($pagination);
     $this->assertNull($pagination->total);
     $this->assertEquals(2, $pagination->page);
     $this->assertEquals(4, $pagination->size);
     $this->assertEquals(4, $pagination->offset);
     $this->assertInstanceOf('GoIntegro\\Hateoas\\Http\\Url', $pagination->paginationlessUrl);
 }
Ejemplo n.º 2
0
 /**
  * Parsea ciertos parámetros de un pedido de HTTP.
  * @param Request $request
  * @throws ResourceNotFoundException
  * @throws ActionNotAllowedException
  * @throws ParseException
  * @throws EntityAccessDeniedException
  * @throws EntityNotFoundException
  */
 public function parse(Request $request)
 {
     $content = $request->getContent();
     if (!empty($content) && self::HTTP_DELETE == $request->getMethod()) {
         throw new ParseException(self::ERROR_CONTENT_ON_DELETE);
     }
     $params = new Params();
     $params->path = $this->parsePath($request);
     $params->i18n = $this->parseI18n($request);
     $params->primaryType = $this->parsePrimaryType($request);
     $params->primaryClass = $this->getEntityClass($params->primaryType);
     $params->relationship = $this->parseRelationship($request, $params);
     $params->primaryIds = $this->parsePrimaryIds($request, $params->relationship);
     $params->relationshipIds = $this->parseRelationshipIds($request);
     $params->locale = $this->localeNegotiator->negotiate($request);
     if (!empty($this->translatableListener) && !empty($params->locale)) {
         $params->translatable = TRUE;
         $this->translatableListener->setTranslatableLocale($params->locale);
     } else {
         $params->translatable = FALSE;
     }
     if ($request->query->has('include')) {
         $params->include = $this->parseInclude($request);
     }
     if ($request->query->has('fields')) {
         $params->sparseFields = $this->parseSparseFields($request, $params->primaryType);
     }
     if ($request->query->has('page')) {
         $params->pagination = $this->paginationParser->parse($request, $params);
     }
     $params->sorting = $this->sortingParser->parse($request, $params);
     $params->filters = $this->filterParser->parse($request, $params);
     $params->action = $this->actionParser->parse($request, $params);
     $params->resourceConfig = $this->resourceEntityMapper->getResourcesConfig()->get($params->primaryType);
     // Needs the params from the ActionParser.
     $params->entities = $this->entityFinder->find($params);
     // Needs the params from the ActionParser (and ParamEntityFinder).
     $params->resources = $this->bodyParser->parse($request, $params);
     return $params;
 }