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

Accept an array of properties in get/getLiteral/getResource methods #326

Merged
merged 3 commits into from
Aug 27, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
12 changes: 7 additions & 5 deletions lib/Graph.php
Original file line number Diff line number Diff line change
Expand Up @@ -592,10 +592,10 @@ protected function checkValueParam(&$value)
*
* This method will return null if the property does not exist.
*
* @param string $resource The URI of the resource (e.g. http://example.com/joe#me)
* @param string $propertyPath A valid property path
* @param string $type The type of value to filter by (e.g. literal or resource)
* @param string $lang The language to filter by (e.g. en)
* @param string $resource The URI of the resource (e.g. http://example.com/joe#me)
* @param string|array $propertyPath A valid property path
* @param string $type The type of value to filter by (e.g. literal or resource)
* @param string $lang The language to filter by (e.g. en)
*
* @throws \InvalidArgumentException
* @return mixed A value associated with the property
Expand All @@ -608,9 +608,11 @@ public function get($resource, $propertyPath, $type = null, $lang = null)
return $this->getSingleProperty($resource, $propertyPath->getUri(), $type, $lang);
} elseif (is_string($propertyPath) and preg_match('|^(\^?)<(.+)>|', $propertyPath, $matches)) {
return $this->getSingleProperty($resource, "$matches[1]$matches[2]", $type, $lang);
} elseif (is_array($propertyPath)) {
$propertyPath = implode('|', $propertyPath); // convert to path expression
} elseif ($propertyPath === null or !is_string($propertyPath)) {
throw new \InvalidArgumentException(
'$propertyPath should be a string or EasyRdf\Resource and cannot be null'
'$propertyPath should be a string, array or EasyRdf\Resource and cannot be null'
);
} elseif ($propertyPath === '') {
throw new \InvalidArgumentException(
Expand Down
4 changes: 2 additions & 2 deletions test/EasyRdf/GraphTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -820,7 +820,7 @@ public function testGetNullProperty()
{
$this->setExpectedException(
'InvalidArgumentException',
'$propertyPath should be a string or EasyRdf\Resource and cannot be null'
'$propertyPath should be a string, array or EasyRdf\Resource and cannot be null'
);
$this->graph->get($this->uri, null);
}
Expand All @@ -838,7 +838,7 @@ public function testGetNonStringProperty()
{
$this->setExpectedException(
'InvalidArgumentException',
'$propertyPath should be a string or EasyRdf\Resource and cannot be null'
'$propertyPath should be a string, array or EasyRdf\Resource and cannot be null'
);
$this->graph->get($this->uri, $this);
}
Expand Down
22 changes: 20 additions & 2 deletions test/EasyRdf/ResourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,15 @@ public function testGetMultipleProperties()
);
}

public function testGetMultiplePropertiesArray()
{
$this->setupTestGraph();
$this->assertStringEquals(
'Test A',
$this->resource->get(['rdf:test','rdf:foobar'])
);
}

public function testGetMultipleProperties2()
{
$this->setupTestGraph();
Expand All @@ -416,6 +425,15 @@ public function testGetMultipleProperties2()
);
}

public function testGetMultiplePropertiesArray2()
{
$this->setupTestGraph();
$this->assertStringEquals(
'Test A',
$this->resource->get(['rdf:foobar','rdf:test'])
);
}

public function testGetNonExistantProperty()
{
$this->setupTestGraph();
Expand All @@ -427,7 +445,7 @@ public function testGetNullKey()
$this->setupTestGraph();
$this->setExpectedException(
'InvalidArgumentException',
'$propertyPath should be a string or EasyRdf\Resource and cannot be null'
'$propertyPath should be a string, array or EasyRdf\Resource and cannot be null'
);
$this->resource->get(null);
}
Expand All @@ -447,7 +465,7 @@ public function testGetNonStringKey()
$this->setupTestGraph();
$this->setExpectedException(
'InvalidArgumentException',
'$propertyPath should be a string or EasyRdf\Resource and cannot be null'
'$propertyPath should be a string, array or EasyRdf\Resource and cannot be null'
);
$this->resource->get($this);
}
Expand Down