Add retriever to CloudObject and allow retrieval of related objects
This commit is contained in:
@@ -19,7 +19,8 @@ class CloudObject {
|
||||
|
||||
private $coid;
|
||||
private $node;
|
||||
private $reader;
|
||||
private $reader = null;
|
||||
private $retriever = null;
|
||||
|
||||
public function __construct(IRI $coid, Node $node) {
|
||||
Assert::eq((string)$coid, $node->getId(), "COID and Node ID must match.");
|
||||
@@ -45,6 +46,16 @@ class CloudObject {
|
||||
return $this->reader;
|
||||
}
|
||||
|
||||
/**
|
||||
* Specify an ObjectRetriever to use for retrieval
|
||||
* of related objects.
|
||||
*/
|
||||
public function setObjectRetriever(ObjectRetriever $retriever) : self {
|
||||
$this->retriever = $retriever;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the COID of this object.
|
||||
*/
|
||||
@@ -53,7 +64,7 @@ class CloudObject {
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the raw object node.
|
||||
* Get the object node encapsulated in this CloudObject.
|
||||
*/
|
||||
public function getObjectNode() : Node {
|
||||
return $this->node;
|
||||
@@ -107,4 +118,19 @@ class CloudObject {
|
||||
return $this->getReader()->getFirstValueNode($this->node, $property, $default);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the value of a property and, if it's a COID, retrieve the corresponding CloudObject.
|
||||
*/
|
||||
public function getCloudObject($property) : ?CloudObject {
|
||||
Assert::notNull($this->retriever, "No ObjectRetriever set for CloudObject. Cannot retrieve related object.");
|
||||
|
||||
$coid = $this->getReader()->getFirstValueIRI($this->node, $property);
|
||||
|
||||
if (!($coid instanceof IRI)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $this->retriever->getCloudObject($coid);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -196,7 +196,8 @@ class ObjectRetriever implements CustomCacheAndLogInterface {
|
||||
return null;
|
||||
}
|
||||
|
||||
$object = new CloudObject($coid, $node);
|
||||
$object = (new CloudObject($coid, $node))
|
||||
->setObjectRetriever($this);
|
||||
|
||||
if ($this->defaultReader) {
|
||||
// Initialize CloudObject with default reader if it is set
|
||||
|
||||
@@ -43,7 +43,7 @@ class ObjectRetrieverMockTest extends \PHPUnit\Framework\TestCase {
|
||||
// Test CloudObject interface
|
||||
$object = $this->retriever->getCloudObject($coid);
|
||||
$this->assertNotNull($object);
|
||||
$this->assertEquals((string)$coid, $object->getObjectNode()->getID());
|
||||
$this->assertEquals((string)$coid, $object->getObjectNode()->getId());
|
||||
$this->assertEquals($coid, $object->getCOID());
|
||||
$this->assertNotNull($object->getString('http://www.w3.org/2000/01/rdf-schema#label'));
|
||||
$this->assertEquals('CloudObjects', $object->getString('http://www.w3.org/2000/01/rdf-schema#label'));
|
||||
|
||||
@@ -28,9 +28,21 @@ class ObjectRetrieverPublicTest extends \PHPUnit\Framework\TestCase {
|
||||
$coid = new IRI('coid://cloudobjects.io');
|
||||
$object = $this->retriever->getObjectNode($coid);
|
||||
$this->assertNotNull($object);
|
||||
$this->assertEquals((string)$coid, $object->getID());
|
||||
$this->assertEquals((string)$coid, $object->getId());
|
||||
$this->assertNotNull($object->getProperty('http://www.w3.org/2000/01/rdf-schema#label'));
|
||||
$this->assertEquals('CloudObjects', $object->getProperty('http://www.w3.org/2000/01/rdf-schema#label')->getValue());
|
||||
$this->assertEquals('CloudObjects', $object->getProperty('http://www.w3.org/2000/01/rdf-schema#label')->getValue());
|
||||
}
|
||||
|
||||
public function testGetRelatedObject() {
|
||||
$coid = new IRI('coid://cloudobjects.io');
|
||||
$object = $this->retriever->getCloudObject($coid);
|
||||
$this->assertNotNull($object);
|
||||
$this->assertNotNull($object->getIRI('coid://cloudobjects.io/isVisibleTo'));
|
||||
$this->assertEquals('coid://cloudobjects.io/Public', $object->getString('coid://cloudobjects.io/isVisibleTo'));
|
||||
|
||||
$relatedObject = $object->getCloudObject('coid://cloudobjects.io/isVisibleTo');
|
||||
$this->assertNotNull($relatedObject);
|
||||
$this->assertEquals('coid://cloudobjects.io/Public', (string)$relatedObject->getCOID());
|
||||
}
|
||||
|
||||
public function testGetCOIDList() {
|
||||
|
||||
Reference in New Issue
Block a user