public function test() { $queryStringUrlGenerator = new CallableUrlGenerator(function ($route, array $parameters) { return $route . '?' . http_build_query($parameters); }); $hateoas = HateoasBuilder::create()->setUrlGenerator(null, $queryStringUrlGenerator)->build(); $halHateoas = HateoasBuilder::create()->setUrlGenerator(null, $queryStringUrlGenerator)->setXmlSerializer(new XmlHalSerializer())->build(); $collection = new RouteAwareRepresentation(new SimpleCollection(array('Adrien', 'William'), 'authors', 'users'), '/authors', array('query' => 'willdurand/Hateoas')); $this->string($hateoas->serialize($collection, 'xml'))->isEqualTo(<<<XML <?xml version="1.0" encoding="UTF-8"?> <result> <users rel="authors"> <entry><![CDATA[Adrien]]></entry> <entry><![CDATA[William]]></entry> </users> <link rel="self" href="/authors?query=willdurand%2FHateoas"/> </result> XML )->string($halHateoas->serialize($collection, 'xml'))->isEqualTo(<<<XML <?xml version="1.0" encoding="UTF-8"?> <result href="/authors?query=willdurand%2FHateoas"> <resource rel="authors"><![CDATA[Adrien]]></resource> <resource rel="authors"><![CDATA[William]]></resource> </result> XML )->string($halHateoas->serialize($collection, 'json'))->isEqualTo('{' . '"_links":{' . '"self":{' . '"href":"\\/authors?query=willdurand%2FHateoas"' . '}' . '},' . '"_embedded":{' . '"authors":[' . '"Adrien",' . '"William"' . ']' . '}' . '}'); }
public function test() { $hateoas = HateoasBuilder::buildHateoas(); $halHateoas = HateoasBuilder::create()->setXmlSerializer(new XmlHalSerializer())->build(); $collection = new SimpleCollection(array('Adrien', 'William'), 'authors'); $collection->setXmlElementName('users'); $this->string($hateoas->serialize($collection, 'xml'))->isEqualTo(<<<XML <?xml version="1.0" encoding="UTF-8"?> <collection> <users rel="authors"> <entry><![CDATA[Adrien]]></entry> <entry><![CDATA[William]]></entry> </users> </collection> XML )->string($halHateoas->serialize($collection, 'xml'))->isEqualTo(<<<XML <?xml version="1.0" encoding="UTF-8"?> <collection> <resource rel="authors"><![CDATA[Adrien]]></resource> <resource rel="authors"><![CDATA[William]]></resource> </collection> XML )->string($halHateoas->serialize($collection, 'json'))->isEqualTo('{"_embedded":{"authors":["Adrien","William"]}}'); }
public function testSerializeAdrienBrault() { $hateoas = HateoasBuilder::create()->setXmlSerializer(new XmlHalSerializer())->build(); $adrienBrault = new AdrienBrault(); $this->assertSame(<<<XML <?xml version="1.0" encoding="UTF-8"?> <result href="http://adrienbrault.fr"> <first_name><![CDATA[Adrien]]></first_name> <last_name><![CDATA[Brault]]></last_name> <link rel="computer" href="http://www.apple.com/macbook-pro/"/> <link rel="dynamic-relation" href="awesome!!!"/> <resource rel="computer"> <name><![CDATA[MacBook Pro]]></name> </resource> <resource rel="broken-computer"> <name><![CDATA[Windows Computer]]></name> </resource> <resource rel="smartphone"> <name><![CDATA[iPhone 6]]></name> </resource> <resource rel="smartphone"> <name><![CDATA[Nexus 5]]></name> </resource> <resource rel="dynamic-relation"><![CDATA[wowowow]]></resource> </result> XML , $hateoas->serialize($adrienBrault, 'xml')); }
public function getExtensions() { $hateoas = \Hateoas\HateoasBuilder::create()->setUrlGenerator(null, new CallableUrlGenerator(function ($name, $parameters, $absolute) { return sprintf('%s/%s%s', $absolute ? 'http://example.com' : '', $name, strtr('/id', $parameters)); }))->build(); return array(new LinkExtension($hateoas->getLinkHelper())); }
protected function sendJsonResponse($object, $statusCode = 200) { $hateoas = HateoasBuilder::create()->build(); $json = $hateoas->serialize($object, 'json'); $response = new Response(); $response->setContent($json); $response->setStatusCode($statusCode); $response->headers->set('Content-Type', 'application/json'); return $response; }
protected function setUp() { $this->hateoas = HateoasBuilder::create()->setUrlGenerator(null, new CallableUrlGenerator(function ($name, $parameters, $absolute) { if ($name === 'user_get') { return sprintf('%s%s', $absolute ? 'http://example.com' : '', strtr('/users/id', $parameters)); } if ($name === 'post_get') { return sprintf('%s%s', $absolute ? 'http://example.com' : '', strtr('/posts/id', $parameters)); } throw new \RuntimeException('Cannot generate URL'); }))->build(); }
public function handle(\Symfony\Component\HttpFoundation\Request $request, $type = self::MASTER_REQUEST, $catch = true) { $product = new \Test\Product('Aspirin'); $product->setVariants([new Variant(), new Variant(), new Variant()]); $serializer = \Hateoas\HateoasBuilder::create()->setDebug(true)->build(); $mimeType = $request->attributes->get('_mime_type'); $version = (int) explode(';version=', $mimeType)[1]; $json = $serializer->serialize($product, $request->attributes->get('_format'), \JMS\Serializer\SerializationContext::create()->setVersion($version)); $response = \Symfony\Component\HttpFoundation\Response::create($json, 200); $response->headers->set('Content-Type', $request->attributes->get('_mime_type')); return $response; }
protected function setUp() { parent::setUp(); $this->queryStringUrlGenerator = new CallableUrlGenerator(function ($route, array $parameters, $absolute) { if ('' !== ($queryString = http_build_query($parameters))) { $queryString = '?' . $queryString; } return ($absolute ? 'http://example.com' : '') . $route . $queryString; }); $this->hateoas = HateoasBuilder::create()->setUrlGenerator(null, $this->queryStringUrlGenerator)->build(); $this->halHateoas = HateoasBuilder::create()->setUrlGenerator(null, $this->queryStringUrlGenerator)->setXmlSerializer(new XmlHalSerializer())->build(); }
/** * @param $data * * @return \Symfony\Component\HttpFoundation\Response */ public function hateoasResponse($data) { if (get_class($data) == 'Illuminate\\Pagination\\LengthAwarePaginator') { $data = $this->formateHatoeasPaged($data); } $hateoas = HateoasBuilder::create()->setUrlGenerator(null, new CallableUrlGenerator(function ($route, array $parameters, $absolute) { $fullUrl = route($route, $parameters); return $absolute ? $fullUrl : str_replace(url(), '', $fullUrl); }))->build(); $json = $hateoas->serialize($data, 'json'); // todo: insert correct status code based on action (ie 206 partial, 201 created etc) return response($json, 200)->header('Content-Type', 'application/json'); }
/** * @param ContainerInterface $container * @param Configurable $config * * @return void */ public function register(ContainerInterface $container, Configurable $config) { if (!$config instanceof Config) { throw new \InvalidArgumentException('Wrong Config object'); } $hateoas = HateoasBuilder::create(); $hateoas->setDebug($config->debug); $hateoas->setUrlGenerator(null, new CallableUrlGenerator($config->urlGenerator)); if (!$config->debug) { $hateoas->setCacheDir($config->cacheDir); $hateoas->addMetadataDir($config->metadataDir); } $container->add($config->getServiceName(), $hateoas->build()); }
public function testAlternativeUrlGenerator() { $brokenUrlGenerator = new CallableUrlGenerator(function ($name, $parameters) { return $name . '?' . http_build_query($parameters); }); $hateoas = HateoasBuilder::create()->setUrlGenerator('my_generator', $brokenUrlGenerator)->build(); $this->string($hateoas->serialize(new WithAlternativeRouter(), 'xml'))->isEqualTo(<<<XML <?xml version="1.0" encoding="UTF-8"?> <result> <link rel="search" href="/search?query=hello"/> </result> XML ); }
public function testCyclicalReferences() { $hateoas = HateoasBuilder::create()->build(); $reference1 = new CircularReference1(); $reference2 = new CircularReference2(); $reference1->setReference2($reference2); $reference2->setReference1($reference1); $this->assertSame(<<<XML <?xml version="1.0" encoding="UTF-8"?> <result> <name><![CDATA[reference1]]></name> <entry rel="reference2"> <name><![CDATA[reference2]]></name> <entry rel="reference1"/> </entry> </result> XML , $hateoas->serialize($reference1, 'xml')); $this->assertSame('{' . '"name":"reference1",' . '"_embedded":{' . '"reference2":{' . '"name":"reference2",' . '"_embedded":{' . '"reference1":null' . '}' . '}' . '}' . '}', $hateoas->serialize($reference1, 'json')); }
/** * Registers services on the given app. * * This method should only be used to configure services and parameters. * It should not get services. * * @param Container $app */ public function register(Container $app) { AnnotationRegistry::registerLoader('class_exists'); $app['pagerFantaFactory'] = function () { return new PagerfantaFactory(); }; $app['templated_url_generator'] = function () use($app) { return new Rfc6570Generator($app['routes'], $app['request_context']); }; $app['api.converters.pagination'] = function () use($app) { return new PaginationConverter($app['config']['api.default_pagination_limit'], $app['config']['api.max_pagination_limit']); }; $app['api.response.builder'] = function () use($app) { $hateoas = HateoasBuilder::create()->setUrlGenerator(null, new CallableUrlGenerator(function ($route, array $parameters, $absolute) use($app) { return $app['url_generator']->generate($route, $parameters, $absolute); }))->setUrlGenerator('templated', new CallableUrlGenerator(function ($route, array $parameters, $absolute) use($app) { return $app['templated_url_generator']->generate($route, $parameters, $absolute); }))->setCacheDir($app['cache_dir'] . '/serializer')->setDebug($app['debug'])->build(); return new ApiResponseBuilder($hateoas); }; }
/** * @return \Hateoas\Hateoas */ public function getHateoas() { $hateoasBuilder = Builder::create()->setCacheDir($this->config['cacheDir'])->setDebug($this->config['debugMode']); $this->setJsonSerializer($hateoasBuilder); $this->setXmlSerializer($hateoasBuilder); $this->addUrlGenerators($hateoasBuilder); if (!empty($this->config['expressionContextVariables'])) { $this->addExpressionContextVariables($hateoasBuilder); } if ($this->config['expressionLanguage']) { $hateoasBuilder->setExpressionLanguage(new $this->config['expressionLanguage']()); } if (!empty($this->config['expressionFunctions'])) { $this->setExpressionLanguageFunctions($hateoasBuilder); } if ($this->config['relationProviderResolvers']) { $this->addRelationProviderResolvers($hateoasBuilder); } $hateoasBuilder->includeInterfaceMetadata($this->config['includeInterfaceMetadata']); return $hateoasBuilder->build(); }
public function testWithPagerfanta() { $hateoas = HateoasBuilder::create()->setUrlGenerator(null, new CallableUrlGenerator(function ($route, array $parameters) { return $route . '?' . http_build_query($parameters); }))->build(); $factory = new PagerfantaFactory(); $pagerfanta = new Pagerfanta(new ArrayAdapter(array('bim', 'bam', 'boom'))); $collection = $factory->create($pagerfanta, 'my_route'); $this->string($hateoas->serialize($collection, 'xml'))->isEqualTo(<<<XML <?xml version="1.0" encoding="UTF-8"?> <collection page="1" limit="10" pages="1"> <entry><![CDATA[bim]]></entry> <entry><![CDATA[bam]]></entry> <entry><![CDATA[boom]]></entry> <link rel="self" href="my_route?page=1&limit=10"/> <link rel="first" href="my_route?page=1&limit=10"/> <link rel="last" href="my_route?page=1&limit=10"/> </collection> XML ); }