Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to define custom Graph class in TypeMapper. closes #380 #381

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 4 additions & 2 deletions lib/GraphStore.php
Expand Up @@ -86,14 +86,16 @@ public function getUri()
*/
public function get($uriRef)
{
$graphClass = TypeMapper::getDefaultGraphClass();

if ($uriRef === self::DEFAULT_GRAPH) {
$dataUrl = $this->urlForGraph(self::DEFAULT_GRAPH);
$graph = new Graph();
$graph = new $graphClass();
} else {
$graphUri = $this->parsedUri->resolve($uriRef)->toString();
$dataUrl = $this->urlForGraph($graphUri);

$graph = new Graph($graphUri);
$graph = new $graphClass($graphUri);
}

$graph->load($dataUrl);
Expand Down
4 changes: 3 additions & 1 deletion lib/Sparql/Client.php
Expand Up @@ -40,6 +40,7 @@
use EasyRdf\Graph;
use EasyRdf\Http;
use EasyRdf\RdfNamespace;
use EasyRdf\TypeMapper;
use EasyRdf\Utils;

/**
Expand Down Expand Up @@ -392,7 +393,8 @@ protected function parseResponseToQuery($response)
$result = new Result($response->getBody(), $content_type);
return $result;
} else {
$result = new Graph($this->queryUri, $response->getBody(), $content_type);
$graphClass = TypeMapper::getDefaultGraphClass();
$result = new $graphClass($this->queryUri, $response->getBody(), $content_type);
madbob marked this conversation as resolved.
Show resolved Hide resolved
return $result;
}
}
Expand Down
43 changes: 43 additions & 0 deletions lib/TypeMapper.php
Expand Up @@ -51,6 +51,9 @@ class TypeMapper
/** Default resource class */
private static $defaultResourceClass = 'EasyRdf\Resource';

/** Default graph class */
private static $defaultGraphClass = 'EasyRdf\Graph';

/** Get the registered class for an RDF type
*
* If a type is not registered, then this method will return null.
Expand Down Expand Up @@ -162,6 +165,46 @@ public static function setDefaultResourceClass($class)

return self::$defaultResourceClass = $class;
}

/**
madbob marked this conversation as resolved.
Show resolved Hide resolved
* @return string The default Graph class
*/
public static function getDefaultGraphClass()
{
return self::$defaultGraphClass;
}

/**
* Sets the default graph class
*
* @param string $class The graph full class name (e.g. \MyCompany\Graph)
*
* @throws \InvalidArgumentException
* @return string The default Graph class
*/
public static function setDefaultGraphClass($class)
{
if (!is_string($class) or $class == null or $class == '') {
throw new \InvalidArgumentException(
"\$class should be a string and cannot be null or empty"
);
}

if (!class_exists($class)) {
throw new \InvalidArgumentException(
"Given class should be an existing class"
);
}

$ancestors = class_parents($class);
if (($class != 'EasyRdf\Graph') && (empty($ancestors) || !in_array('EasyRdf\Graph', $ancestors))) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would use 0 == count($ancestors) instead of empty($ancestors) here. It improves code understanding. Never tried it with arrays but doc says:

A variable is considered empty if it does not exist or if its value equals false.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here I've copied the implementation of TypeMapper::setDefaultResourceClass().
Perhaps count() == 0 can be used (but not in Yoda notation, not used anywhere in the whole code).
And, even better: the whole block of conditions can be moved in a dedicated private function, to be reused.

throw new \InvalidArgumentException(
"Given class should have EasyRdf\\Graph as an ancestor"
);
}

return self::$defaultGraphClass = $class;
}
}


Expand Down
73 changes: 73 additions & 0 deletions test/EasyRdf/TypeMapperTest.php
Expand Up @@ -46,6 +46,13 @@ public function myMethod()
}
}

class MyGraphClass extends Graph
{
public function myMethod()
{
return true;
}
}

class TypeMapperTest extends TestCase
{
Expand Down Expand Up @@ -290,4 +297,70 @@ public function testInstantiate()
$this->assertClass('EasyRdf\MyTypeClass', $joesFoaf);
$this->assertTrue($joesFoaf->myMethod());
}

public function testSetNonExtendingDefaultGraphClass()
{
$this->setExpectedException(
'InvalidArgumentException',
'Given class should have EasyRdf\Graph as an ancestor'
);
TypeMapper::setDefaultGraphClass('EasyRdf\Resource');
}

public function testSetBaseDefaultGraphClass()
{
TypeMapper::setDefaultGraphClass('EasyRdf\Graph');
$this->assertEquals('EasyRdf\Graph', TypeMapper::getDefaultGraphClass());
}

public function testSetDefaultGraphClass()
{
$this->setExpectedException(
'InvalidArgumentException',
'Given class should be an existing class'
);
TypeMapper::setDefaultGraphClass('FooBar\Graph');
}

public function testSetDefaultGraphClassEmptyString()
{
$this->setExpectedException(
'InvalidArgumentException',
'$class should be a string and cannot be null or empty'
);
TypeMapper::setDefaultGraphClass('');
}

public function testSetDefaultGraphClassNull()
{
$this->setExpectedException(
'InvalidArgumentException',
'$class should be a string and cannot be null or empty'
);
TypeMapper::setDefaultGraphClass(null);
}

public function testSetDefaultGraphClassNonString()
{
$this->setExpectedException(
'InvalidArgumentException',
'$class should be a string and cannot be null or empty'
);
TypeMapper::setDefaultGraphClass(array());
}

public function testGraphInstantiate()
{
TypeMapper::setDefaultGraphClass('EasyRdf\MyGraphClass');
$graphClass = TypeMapper::getDefaultGraphClass();
$data = readFixture('foaf.json');
$graph = new $graphClass(
'http://www.example.com/joe/foaf.rdf',
$data,
'json'
);
$joe = $graph->resource('http://www.example.com/joe#me');
$this->assertClass('EasyRdf\MyGraphClass', $joe->getGraph());
$this->assertTrue($joe->getGraph()->myMethod());
}
}