public function testRepresentation() { $representation = new KnpPaginatorRepresentationFactory(PaginationInterface::PAGE_PARAMETER_NAME, PaginationInterface::LIMIT_PARAMETER_NAME); $paginator = new Paginator(); $pagination = $paginator->paginate(['item_1', 'item_2'], 1, 10); $response = $representation->createRepresentation($pagination, new Request()); self::assertTrue(is_array($response->getInline()->getResources())); self::assertTrue(count($response->getInline()->getResources()) == 2); $pagination = $paginator->paginate(new ArrayCollection(['test_item', 'test_item2', 'test_item_3']), 1, 10); $response = $representation->createRepresentation($pagination, new Request()); self::assertTrue(is_array($response->getInline()->getResources())); self::assertTrue(count($response->getInline()->getResources()) == 3); $pagination = $paginator->paginate(new \ArrayObject(['test_item', 'test_item2']), 1, 10); $response = $representation->createRepresentation($pagination, new Request()); self::assertTrue(is_array($response->getInline()->getResources())); self::assertTrue(count($response->getInline()->getResources()) == 2); }
/** * @param GetResponseForControllerResultEvent $event */ public function onKernelView(GetResponseForControllerResultEvent $event) { $controllerResult = $event->getControllerResult(); if ($controllerResult instanceof ResourcesListResponseInterface) { $responseContext = $controllerResult->getResponseContext(); if ($responseContext->getIntention() === ResponseContextInterface::INTENTION_API) { $factory = new KnpPaginatorRepresentationFactory(); $representation = $factory->createRepresentation($controllerResult->getResources(), $event->getRequest()); $event->setResponse($this->viewHandler->handle(View::create($representation, $responseContext->getStatusCode()))); } } elseif ($controllerResult instanceof SingleResourceResponseInterface) { $responseContext = $controllerResult->getResponseContext(); if ($responseContext->getIntention() === ResponseContextInterface::INTENTION_API) { $event->setResponse($this->viewHandler->handle(View::create($controllerResult->getResource(), $responseContext->getStatusCode()))); } } }