/** * @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']); }
/** * 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; }
/** * @param $index * @param $key * @param $typ * @param $type * @param $options * @param $trans * @param $output * @param $reset * @param $showProgress */ protected function populateType($index, $key, Type $typ, $type, $options, $trans, $output, $reset, $showProgress) { if ($reset & $type) { $this->resetter->reset($index, $type, $key, $output); } $output->writeln("Populating " . $key); $this->jsonLdSerializer->getJsonLdFrameLoader()->setEsIndex($index); $class = $this->configManager->getIndexConfiguration($index)->getType($key)->getType(); $size = $this->getSize($class); // no object in triplestore if (!current($size)) { return; } $size = current($size)['count']; $progress = $this->displayInitialAvancement($size, $options, $showProgress, $output); $done = 0; while ($done < $size) { $resources = $this->getResources($class, $options, $done); $docs = array(); /* @var Resource $add */ foreach ($resources as $resource) { $types = $resource->all('rdf:type'); $mostAccurateType = $this->getMostAccurateType($types, $resource, $output, $class); // index only the current resource if the most accurate type is the key populating if ($class === $mostAccurateType) { $doc = $trans->transform($resource->getUri(), $index, $mostAccurateType); if ($doc) { $docs[] = $doc; } } } // send documents to elasticsearch if (count($docs)) { $typ->addDocuments($docs); } else { $output->writeln(""); $output->writeln("nothing to index"); } $done = $this->displayAvancement($options, $done, $size, $showProgress, $output, $progress); //flushing manager for mem usage $this->resourceManager->flush(); } $progress->finish(); }
/** * @return object|array|scalar */ public function getResult() { return $this->serializer->serialize($this->resource, $this->frame, $this->options); }