/**
  * Transform a resource to an elastica document.
  *
  * @param $uri
  * @param $index
  * @param $type
  *
  * @return Document|null
  */
 public function transform($uri, $index, $type)
 {
     if ($index && $this->serializerHelper->isTypeIndexed($index, $type)) {
         $frame = $this->serializerHelper->getTypeFramePath($index, $type);
         $phpClass = TypeMapper::get($type);
         if (!$phpClass) {
             $phpClass = "EasyRdf\\Resource";
         }
         $jsonLd = $this->jsonLdSerializer->serialize(new $phpClass($uri), $frame, array("includeParentClassFrame" => true));
         $graph = json_decode($jsonLd, true);
         if (!isset($graph['@graph'][0])) {
             return;
         }
         $resource = $graph['@graph'][0];
         $resource['@id'] = $uri;
         $json = json_encode($resource);
         $json = str_replace('@id', '_id', $json);
         $json = str_replace('@type', '_type', $json);
         $index = $this->configManager->getIndexConfiguration($index)->getElasticSearchName();
         return new Document($uri, $json, $type, $index);
     }
     return;
 }
示例#2
0
 /**
  * @throws \Twig_Error_Loader
  */
 public function testSerialize()
 {
     $loader = new JsonLdFrameLoader();
     $loader->addPath(__DIR__ . '/Fixtures', 'namespace');
     $registry = new RdfNamespaceRegistry();
     $foaf = new Graph('http://njh.me/foaf.rdf');
     $foaf->parseFile(__DIR__ . '/Fixtures/foaf.rdf');
     $graphProvider = new SimpleGraphProvider();
     $metadataFactory = $this->getMockBuilder('Metadata\\MetadataFactory')->disableOriginalConstructor()->getMock();
     // new MetadataFactory();
     $resource = $foaf->primaryTopic();
     $serializer = new JsonLdSerializer($registry, $loader, $graphProvider, $metadataFactory);
     $serialized = $serializer->serialize($resource, '@namespace/frame.jsonld');
     $decoded = json_decode($serialized, true);
     $this->assertEquals($resource->get('foaf:name'), $decoded['@graph'][0]['foaf:name']);
 }
 /**
  * @return object|array|scalar
  */
 public function getResult()
 {
     return $this->serializer->serialize($this->resource, $this->frame, $this->options);
 }