/** * @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']); }
/** * Parses a given stream and returns an iterator containing Statement instances representing the * previously read data. The stream parses the data not as a whole but in chunks. * * @param string $inputStream Filename of the stream to parse which contains RDF * serialized data. * @param string $baseUri The base URI of the parsed content. If this URI is null * the inputStreams URL is taken as base URI. * @param string $serialization The serialization of the inputStream. If null is given * the parser will either apply some standard serialization, * or the only one it is supporting, or will try to guess * the correct serialization, or will throw an Exception. * Supported formats are a subset of the following: * json, rdfxml, sparql-xml, rdfa, turtle, ntriples, n3 * @return StatementIterator A StatementIterator containing all the Statements parsed by the parser to * far. * @throws \Exception If the base URI $baseUri is no valid URI. */ public function parseStreamToIterator($inputStream, $baseUri = null, $serialization = null) { $graph = new Graph(); // let EasyRdf guess the format if ($serialization === null) { // use PHP's file:// stream, if its a local file if (false === strpos($inputStream, '://')) { $inputStream = 'file://' . $inputStream; } $serialization = Format::guessFormat(file_get_contents($inputStream)); } else { $serialization = Format::getFormat($serialization); } // if format is still null, throw exception, because we dont know what format the given stream is if (null === $serialization) { throw new \Exception('Either given $format is unknown or i could not guess format.'); } $graph->parseFile($inputStream, $serialization->getName()); // transform parsed data to PHP array return $this->rdfPhpToStatementIterator($graph->toRdfPhp()); }