Skip to content

Instantly share code, notes, and snippets.

@indeyets
Created October 8, 2013 15:06
Show Gist options
  • Save indeyets/6886193 to your computer and use it in GitHub Desktop.
Save indeyets/6886193 to your computer and use it in GitHub Desktop.
<?php
namespace GridsBy\SPARQL\GraphStore;
use Guzzle\Http\Client;
class Connection
{
private $endpoint = '';
private $client = null;
/**
* @param string $endpoint
*/
public function __construct($endpoint)
{
$this->endpoint = $endpoint;
$this->client = new Client();
}
public function insert(\EasyRdf_Graph $graph, $graph_iri)
{
return $this->insertFromString($graph->serialise('turtle'), $graph_iri, 'text/turtle');
}
public function insertFromFile($path, $graph_iri)
{
if (!file_exists($path)) {
throw new \UnexpectedValueException("'{$path}' is not a valid file path");
}
return $this->insertFromString(file_get_contents($path), $graph_iri);
}
protected function insertFromString($data, $graph_iri, $mime_type = null)
{
if (null === $mime_type) {
$format = \EasyRdf_Format::guessFormat($data);
$mime_type = $format->getDefaultMimeType();
}
$params = ['graph' => $graph_iri];
$uri = $this->endpoint.'?'.http_build_query($params);
$request = $this->client->post($uri, ['Content-type' => $mime_type], $data);
return $request->send();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment