Compare commits

...

4 Commits

5 changed files with 210 additions and 115 deletions

View File

@@ -19,7 +19,8 @@ class CloudObject {
private $coid; private $coid;
private $node; private $node;
private $reader; private $reader = null;
private $retriever = null;
public function __construct(IRI $coid, Node $node) { public function __construct(IRI $coid, Node $node) {
Assert::eq((string)$coid, $node->getId(), "COID and Node ID must match."); Assert::eq((string)$coid, $node->getId(), "COID and Node ID must match.");
@@ -32,17 +33,29 @@ class CloudObject {
* Specify a custom NodeReader to use for this CloudObject. * Specify a custom NodeReader to use for this CloudObject.
* If not set, a default NodeReader will be used. * If not set, a default NodeReader will be used.
*/ */
public function setReader(NodeReader $reader) { public function setReader(NodeReader $reader) : self {
$this->reader = $reader; $this->reader = $reader;
return $this;
} }
private function getReader() { private function getReader() : NodeReader {
if (!$this->reader) { if (!$this->reader) {
$this->reader = new NodeReader; $this->reader = new NodeReader;
} }
return $this->reader; 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. * Get the COID of this object.
*/ */
@@ -51,7 +64,7 @@ class CloudObject {
} }
/** /**
* Get the raw object node. * Get the object node encapsulated in this CloudObject.
*/ */
public function getObjectNode() : Node { public function getObjectNode() : Node {
return $this->node; return $this->node;
@@ -105,4 +118,19 @@ class CloudObject {
return $this->getReader()->getFirstValueNode($this->node, $property, $default); 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);
}
} }

View File

@@ -29,6 +29,7 @@ class ObjectRetriever implements CustomCacheAndLogInterface {
private $options; private $options;
private $cache; private $cache;
private $objects; private $objects;
private $defaultReader;
const CO_API_URL = 'https://api.cloudobjects.net/'; const CO_API_URL = 'https://api.cloudobjects.net/';
@@ -107,6 +108,22 @@ class ObjectRetriever implements CustomCacheAndLogInterface {
$this->client = new Client($options); $this->client = new Client($options);
} }
/**
* Set a default reader for CloudObject instances.
*/
public function setDefaultReader(NodeReader $reader) : self {
$this->defaultReader = $reader;
return $this;
}
/**
* Get the default reader for CloudObject instances.
*/
public function getDefaultReader() : NodeReader {
return $this->defaultReader;
}
public function logInfoWithTime($message, $ts) { public function logInfoWithTime($message, $ts) {
if (isset($this->logger)) if (isset($this->logger))
$this->logger->info($message, [ 'elapsed_ms' => round((microtime(true) - $ts) * 1000) ]); $this->logger->info($message, [ 'elapsed_ms' => round((microtime(true) - $ts) * 1000) ]);
@@ -179,7 +196,15 @@ class ObjectRetriever implements CustomCacheAndLogInterface {
return null; return null;
} }
return new CloudObject($coid, $node); $object = (new CloudObject($coid, $node))
->setObjectRetriever($this);
if ($this->defaultReader) {
// Initialize CloudObject with default reader if it is set
$object->setReader($this->defaultReader);
}
return $object;
} }
/** /**

View File

@@ -7,6 +7,7 @@
namespace CloudObjects\SDK; namespace CloudObjects\SDK;
use ML\IRI\IRI; use ML\IRI\IRI;
use ML\JsonLD\Node;
use GuzzleHttp\Client, GuzzleHttp\Handler\MockHandler, use GuzzleHttp\Client, GuzzleHttp\Handler\MockHandler,
GuzzleHttp\HandlerStack, GuzzleHttp\Psr7\Response; GuzzleHttp\HandlerStack, GuzzleHttp\Psr7\Response;
@@ -24,7 +25,8 @@ class NodeReaderMockTest extends \PHPUnit\Framework\TestCase {
private function useRootResourceMock() { private function useRootResourceMock() {
$this->setMockResponse(new Response(200, $this->setMockResponse(new Response(200,
['Content-Type' => 'application/ld+json'], ['Content-Type' => 'application/ld+json'],
'{"@context":{"co":"coid:\/\/cloudobjects.io\/","rdf":"http:\/\/www.w3.org\/1999\/02\/22-rdf-syntax-ns#","agws":"coid:\/\/aauid.net\/","rdfs":"http:\/\/www.w3.org\/2000\/01\/rdf-schema#"},"@id":"coid:\/\/cloudobjects.io","@type":["agws:Service","co:Namespace"],"co:isAtRevision":"6-fbea0c90b2c5e5300e4039ed99be9b2d","co:isVisibleTo":{"@id":"co:Public"},"co:recommendsPrefix":"co","co:wasUpdatedAt":{"@type":"http:\/\/www.w3.org\/2001\/XMLSchema#dateTime","@value":"2017-01-16T17:29:22+00:00"},"rdfs:comment":"The CloudObjects namespace defines the essential objects.","rdfs:label":"CloudObjects"}')); '{"@context":{"co":"coid:\/\/cloudobjects.io\/","rdf":"http:\/\/www.w3.org\/1999\/02\/22-rdf-syntax-ns#","agws":"coid:\/\/aauid.net\/","rdfs":"http:\/\/www.w3.org\/2000\/01\/rdf-schema#"},"@id":"coid:\/\/cloudobjects.io","@type":["agws:Service","co:Namespace"],"co:isAtRevision":"6-fbea0c90b2c5e5300e4039ed99be9b2d","co:isVisibleTo":{"@id":"co:Public"},"co:recommendsPrefix":"co","co:wasUpdatedAt":{"@type":"http:\/\/www.w3.org\/2001\/XMLSchema#dateTime","@value":"2017-01-16T17:29:22+00:00"},"rdfs:comment":"The CloudObjects namespace defines the essential objects.","rdfs:label":"CloudObjects"}'
));
} }
protected function setUp(): void { protected function setUp(): void {
@@ -35,6 +37,7 @@ class NodeReaderMockTest extends \PHPUnit\Framework\TestCase {
'rdfs' => 'http://www.w3.org/2000/01/rdf-schema#' 'rdfs' => 'http://www.w3.org/2000/01/rdf-schema#'
] ]
]); ]);
$this->retriever->setDefaultReader($this->reader);
} }
public function testHasType1() { public function testHasType1() {
@@ -70,6 +73,17 @@ class NodeReaderMockTest extends \PHPUnit\Framework\TestCase {
$this->assertEquals('theDefaultValue', $this->reader->getFirstValueString($object, 'coid://cloudobjects.io/makesTriplesVisibleTo', 'theDefaultValue')); $this->assertEquals('theDefaultValue', $this->reader->getFirstValueString($object, 'coid://cloudobjects.io/makesTriplesVisibleTo', 'theDefaultValue'));
$this->assertEquals('theDefaultValue', $this->reader->getFirstValueString($object, 'co:makesTriplesVisibleTo', 'theDefaultValue')); $this->assertEquals('theDefaultValue', $this->reader->getFirstValueString($object, 'co:makesTriplesVisibleTo', 'theDefaultValue'));
$object = $this->retriever->getCloudObject($coid);
$this->assertEquals('CloudObjects', $object->getString('http://www.w3.org/2000/01/rdf-schema#label'));
$this->assertEquals('CloudObjects', $object->getString('rdfs:label'));
$this->assertNull($object->getString('coid://cloudobjects.io/makesTriplesVisibleTo'));
$this->assertNull($object->getString('co:makesTriplesVisibleTo'));
$this->assertEquals('theDefaultValue', $object->getString('coid://cloudobjects.io/makesTriplesVisibleTo', 'theDefaultValue'));
$this->assertEquals('theDefaultValue', $object->getString('co:makesTriplesVisibleTo', 'theDefaultValue'));
} }
public function testGetFirstValueIRI1() { public function testGetFirstValueIRI1() {
@@ -77,11 +91,19 @@ class NodeReaderMockTest extends \PHPUnit\Framework\TestCase {
$this->useRootResourceMock(); $this->useRootResourceMock();
$object = $this->retriever->getObjectNode($coid); $object = $this->retriever->getObjectNode($coid);
$this->assertInstanceOf('ML\IRI\IRI', $this->reader->getFirstValueIRI($object, 'coid://cloudobjects.io/isVisibleTo')); $this->assertInstanceOf(IRI::class, $this->reader->getFirstValueIRI($object, 'coid://cloudobjects.io/isVisibleTo'));
$this->assertInstanceOf('ML\IRI\IRI', $this->reader->getFirstValueIRI($object, 'co:isVisibleTo')); $this->assertInstanceOf(IRI::class, $this->reader->getFirstValueIRI($object, 'co:isVisibleTo'));
$this->assertEquals(new IRI('coid://cloudobjects.io/Public'), $this->reader->getFirstValueIRI($object, 'coid://cloudobjects.io/isVisibleTo')); $this->assertEquals(new IRI('coid://cloudobjects.io/Public'), $this->reader->getFirstValueIRI($object, 'coid://cloudobjects.io/isVisibleTo'));
$this->assertEquals(new IRI('coid://cloudobjects.io/Public'), $this->reader->getFirstValueIRI($object, 'co:isVisibleTo')); $this->assertEquals(new IRI('coid://cloudobjects.io/Public'), $this->reader->getFirstValueIRI($object, 'co:isVisibleTo'));
$object = $this->retriever->getCloudObject($coid);
$this->assertInstanceOf(IRI::class, $object->getIRI('coid://cloudobjects.io/isVisibleTo'));
$this->assertInstanceOf(IRI::class, $object->getIRI('co:isVisibleTo'));
$this->assertEquals(new IRI('coid://cloudobjects.io/Public'), $object->getIRI('coid://cloudobjects.io/isVisibleTo'));
$this->assertEquals(new IRI('coid://cloudobjects.io/Public'), $object->getIRI('co:isVisibleTo'));
} }
public function testGetFirstValueNode1() { public function testGetFirstValueNode1() {
@@ -89,11 +111,19 @@ class NodeReaderMockTest extends \PHPUnit\Framework\TestCase {
$this->useRootResourceMock(); $this->useRootResourceMock();
$object = $this->retriever->getObjectNode($coid); $object = $this->retriever->getObjectNode($coid);
$this->assertInstanceOf('ML\JsonLD\Node', $this->reader->getFirstValueNode($object, 'coid://cloudobjects.io/isVisibleTo')); $this->assertInstanceOf(Node::class, $this->reader->getFirstValueNode($object, 'coid://cloudobjects.io/isVisibleTo'));
$this->assertInstanceOf('ML\JsonLD\Node', $this->reader->getFirstValueNode($object, 'co:isVisibleTo')); $this->assertInstanceOf(Node::class, $this->reader->getFirstValueNode($object, 'co:isVisibleTo'));
$this->assertEquals('coid://cloudobjects.io/Public', $this->reader->getFirstValueNode($object, 'coid://cloudobjects.io/isVisibleTo')->getId()); $this->assertEquals('coid://cloudobjects.io/Public', $this->reader->getFirstValueNode($object, 'coid://cloudobjects.io/isVisibleTo')->getId());
$this->assertEquals('coid://cloudobjects.io/Public', $this->reader->getFirstValueNode($object, 'co:isVisibleTo')->getId()); $this->assertEquals('coid://cloudobjects.io/Public', $this->reader->getFirstValueNode($object, 'co:isVisibleTo')->getId());
$object = $this->retriever->getCloudObject($coid);
$this->assertInstanceOf(Node::class, $object->getNode('coid://cloudobjects.io/isVisibleTo'));
$this->assertInstanceOf(Node::class, $object->getNode('co:isVisibleTo'));
$this->assertEquals('coid://cloudobjects.io/Public', $object->getNode('coid://cloudobjects.io/isVisibleTo')->getId());
$this->assertEquals('coid://cloudobjects.io/Public', $object->getNode('co:isVisibleTo')->getId());
} }
public function testGetAllValuesString1() { public function testGetAllValuesString1() {
@@ -126,8 +156,8 @@ class NodeReaderMockTest extends \PHPUnit\Framework\TestCase {
$this->assertCount(2, $this->reader->getAllValuesIRI($object, '@type')); $this->assertCount(2, $this->reader->getAllValuesIRI($object, '@type'));
$this->assertInstanceOf('ML\IRI\IRI', $this->reader->getAllValuesIRI($object, 'coid://cloudobjects.io/isVisibleTo')[0]); $this->assertInstanceOf(IRI::class, $this->reader->getAllValuesIRI($object, 'coid://cloudobjects.io/isVisibleTo')[0]);
$this->assertInstanceOf('ML\IRI\IRI', $this->reader->getAllValuesIRI($object, 'co:isVisibleTo')[0]); $this->assertInstanceOf(IRI::class, $this->reader->getAllValuesIRI($object, 'co:isVisibleTo')[0]);
$this->assertEquals(new IRI('coid://cloudobjects.io/Public'), $this->reader->getAllValuesIRI($object, 'coid://cloudobjects.io/isVisibleTo')[0]); $this->assertEquals(new IRI('coid://cloudobjects.io/Public'), $this->reader->getAllValuesIRI($object, 'coid://cloudobjects.io/isVisibleTo')[0]);
$this->assertEquals(new IRI('coid://cloudobjects.io/Public'), $this->reader->getAllValuesIRI($object, 'co:isVisibleTo')[0]); $this->assertEquals(new IRI('coid://cloudobjects.io/Public'), $this->reader->getAllValuesIRI($object, 'co:isVisibleTo')[0]);
@@ -146,8 +176,8 @@ class NodeReaderMockTest extends \PHPUnit\Framework\TestCase {
$this->assertCount(2, $this->reader->getAllValuesNode($object, '@type')); $this->assertCount(2, $this->reader->getAllValuesNode($object, '@type'));
$this->assertInstanceOf('ML\JsonLD\Node', $this->reader->getAllValuesNode($object, 'coid://cloudobjects.io/isVisibleTo')[0]); $this->assertInstanceOf(Node::class, $this->reader->getAllValuesNode($object, 'coid://cloudobjects.io/isVisibleTo')[0]);
$this->assertInstanceOf('ML\JsonLD\Node', $this->reader->getAllValuesNode($object, 'co:isVisibleTo')[0]); $this->assertInstanceOf(Node::class, $this->reader->getAllValuesNode($object, 'co:isVisibleTo')[0]);
$this->assertEquals('coid://cloudobjects.io/Public', $this->reader->getAllValuesNode($object, 'coid://cloudobjects.io/isVisibleTo')[0]->getId()); $this->assertEquals('coid://cloudobjects.io/Public', $this->reader->getAllValuesNode($object, 'coid://cloudobjects.io/isVisibleTo')[0]->getId());
$this->assertEquals('coid://cloudobjects.io/Public', $this->reader->getAllValuesNode($object, 'co:isVisibleTo')[0]->getId()); $this->assertEquals('coid://cloudobjects.io/Public', $this->reader->getAllValuesNode($object, 'co:isVisibleTo')[0]->getId());

View File

@@ -43,7 +43,7 @@ class ObjectRetrieverMockTest extends \PHPUnit\Framework\TestCase {
// Test CloudObject interface // Test CloudObject interface
$object = $this->retriever->getCloudObject($coid); $object = $this->retriever->getCloudObject($coid);
$this->assertNotNull($object); $this->assertNotNull($object);
$this->assertEquals((string)$coid, $object->getObjectNode()->getID()); $this->assertEquals((string)$coid, $object->getObjectNode()->getId());
$this->assertEquals($coid, $object->getCOID()); $this->assertEquals($coid, $object->getCOID());
$this->assertNotNull($object->getString('http://www.w3.org/2000/01/rdf-schema#label')); $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')); $this->assertEquals('CloudObjects', $object->getString('http://www.w3.org/2000/01/rdf-schema#label'));

View File

@@ -28,11 +28,23 @@ class ObjectRetrieverPublicTest extends \PHPUnit\Framework\TestCase {
$coid = new IRI('coid://cloudobjects.io'); $coid = new IRI('coid://cloudobjects.io');
$object = $this->retriever->getObjectNode($coid); $object = $this->retriever->getObjectNode($coid);
$this->assertNotNull($object); $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->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() { public function testGetCOIDList() {
$coid = new IRI('coid://cloudobjects.io'); $coid = new IRI('coid://cloudobjects.io');
$list = $this->stringifyItems( $list = $this->stringifyItems(