public function addMock($method, $uri, $body, $options = array()) { $match = array(); $match['method'] = $method; $match['uri'] = array(); if (isset($options['callback'])) { $match['callback'] = $options['callback']; if (isset($options['callbackArgs'])) { $match['callbackArgs'] = $options['callbackArgs']; } else { $match['callbackArgs'] = array(); } } if (!isset($uri)) { $match['uri'] = null; } else { $match['uri'] = strval($uri); } if ($body instanceof EasyRdf_Http_Response) { $response = $body; } else { if (isset($options['status'])) { $status = $options['status']; } else { $status = 200; } if (isset($options['headers'])) { $headers = $options['headers']; } else { $headers = array(); $format = EasyRdf_Format::guessFormat($body); if (isset($format)) { $headers['Content-Type'] = $format->getDefaultMimeType(); } if (isset($body)) { $headers['Content-Length'] = strlen($body); } } $response = new EasyRdf_Http_Response($status, $headers, $body); } $once = isset($options['once']) ? $options['once'] : false; $this->_mocks[] = array($match, $response, $once); }
/** * Load RDF data into the graph. * * If a URI is supplied, but no data then the data will * be fetched from the URI. * * The document type is optional and can be specified if it * can't be guessed or got from the HTTP headers. * * @param string $uri The URI of the graph * @param string $data Data for the graph * @param string $format The document type of the data */ public function load($uri, $data = null, $format = null) { if (!is_string($uri) or $uri == null or $uri == '') { throw new InvalidArgumentException("\$uri should be a string and cannot be null or empty"); } if (!$data) { # No data was given - try and fetch data from URI # FIXME: prevent loading the same URI multiple times $client = self::getHttpClient(); $client->setUri($uri); $client->setHeaders('Accept', EasyRdf_Format::getHttpAcceptHeader()); $response = $client->request(); if (!$response->isSuccessful()) { throw new EasyRdf_Exception("HTTP request for {$uri} failed: " . $response->getMessage()); } $data = $response->getBody(); if (!$format) { $format = $response->getHeader('Content-Type'); $format = preg_replace('/;(.+)$/', '', $format); } } # Guess the format if it is Unknown if (!$format) { $format = EasyRdf_Format::guessFormat($data); } if (!$format) { throw new EasyRdf_Exception("Unable to load data of an unknown format."); } # Parse the data $format = EasyRdf_Format::getFormat($format); $parser = $format->newParser(); return $parser->parse($this, $data, $format, $uri); }
/** * Parse some RDF data into the graph object. * * @param string $data Data to parse for the graph * @param string $format Optional format of the data * @param string $uri The URI of the data to load */ public function parse($data, $format = null, $uri = null) { $this->checkResourceParam($uri, true); if (!isset($format) or $format == 'guess') { // Guess the format if it is Unknown $format = EasyRdf_Format::guessFormat($data); } else { $format = EasyRdf_Format::getFormat($format); } if (!$format) { throw new EasyRdf_Exception("Unable to parse data of an unknown format."); } $parser = $format->newParser(); return $parser->parse($this, $data, $format, $uri); }
public function testGuessFormatUnknown() { $this->assertNull(EasyRdf_Format::guessFormat('blah blah blah')); }
/** * get the URLs from which the vocabulary data can be downloaded * @return array Array with MIME type as key, URL as value */ public function getDataURLs() { $ret = array(); $urls = $this->resource->allResources("void:dataDump"); foreach ($urls as $url) { // first try dc:format and dc11:format $mimetypelit = $url->getLiteral('dc:format'); if ($mimetypelit === null) { $mimetypelit = $url->getLiteral('dc11:format'); } // if still not found, guess MIME type using file extension if ($mimetypelit !== null) { $mimetype = $mimetypelit->getValue(); } else { $format = EasyRdf_Format::guessFormat(null, $url->getURI()); if ($format === null) { trigger_error("Could not guess format for <{$url}>.", E_USER_WARNING); continue; } $mimetypes = array_keys($format->getMimeTypes()); $mimetype = $mimetypes[0]; } $ret[$mimetype] = $url->getURI(); } return $ret; }