public function testParseJson() { $json = '{ "http://example.com/resource/r1" : { "http://example.com/resource/p1" : [ { "value" : "Test Data", "type" : "literal", "graphs" : [ "http://pre.ximdex.net:8080/marmotta/context/default" ] } ] } }'; $uri = "http://example.com/resource/r1"; $obj = RdfJson::decode_metadata($uri, $json); $this->assertCount(1, $obj); }
/** * Convert an RDF/JSON string into the MarmottaClient metadata representation. Returns an array of the form * * array( * "http://xmlns.com/foaf/0.1/name" => array(new Literal("Sepp Huber")) * ) * * @param $rdfjson_string */ public static function decode_metadata($uri, $rdfjson_string) { $result = array(); $json_array = json_decode($rdfjson_string, true); foreach ($json_array as $subject => $properties) { if ($uri == $subject) { foreach ($properties as $property => $objects) { $result[$property] = array(); foreach ($objects as $object) { $result[$property][] = RdfJson::decode_node($object); } } } } return $result; }
/** * Return the metadata of the resource identified by the URI passed as argument. In PHP, the returned * object is an array mapping from property URIs to arrays of RDFNodes, representing the values of this * property. * <p/> * Example: * * array( * "http://xmlns.com/foaf/0.1/name" => array(new Literal("Sepp Huber")) * ) * * @param $uri * @return array */ public function getResourceMetadata($uri) { try { $client = new Client(); $request = $client->get($this->getServiceUrl($uri), array("User-Agent" => "Marmotta Client Library (PHP)", "Accept" => "application/rdf+json; rel=meta")); // set authentication if given in configuration if (!empty($this->config->getUsername())) { $request->setAuth($this->config->getUsername(), $this->config->getPassword()); } $response = $request->send(); return RdfJson::decode_metadata($uri, $response->getBody(true)); } catch (BadResponseException $ex) { if ($ex->getResponse()->getStatusCode() == 404) { throw new NotFoundException("could not retrieve resource metadata for resource {$uri}; it does not exist"); } else { if ($ex->getResponse()->getStatusCode() == 406) { throw new ContentFormatException("server does not offer metadata type application/json for resource {$uri}"); } else { throw new MarmottaClientException("could not retrieve resource metadata for resource {$uri}; " . $ex->getResponse()->getReasonPhrase()); } } } }