diff --git a/CloudObjects/SDK/CloudObject.php b/CloudObjects/SDK/CloudObject.php index a60d3d7..8d2c6b9 100644 --- a/CloudObjects/SDK/CloudObject.php +++ b/CloudObjects/SDK/CloudObject.php @@ -66,7 +66,7 @@ class CloudObject { /** * Get the object node encapsulated in this CloudObject. */ - public function getObjectNode() : Node { + public function getAsNode() : Node { return $this->node; } @@ -133,4 +133,20 @@ class CloudObject { return $this->retriever->getCloudObject($coid); } + + /** + * Get the value of a property and, if it's a COID, retrieve the corresponding object node. + */ + public function getObjectNode($property) : ?Node { + 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->getObjectNode($coid); + } + } diff --git a/tests/OfflineTests/ObjectRetrieverMockTest.php b/tests/OfflineTests/ObjectRetrieverMockTest.php index 5fd76e7..034a304 100644 --- a/tests/OfflineTests/ObjectRetrieverMockTest.php +++ b/tests/OfflineTests/ObjectRetrieverMockTest.php @@ -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->getAsNode()->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'));