Compare commits

...

4 Commits

5 changed files with 210 additions and 115 deletions

View File

@@ -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.");
@@ -32,17 +33,29 @@ class CloudObject {
* Specify a custom NodeReader to use for this CloudObject.
* If not set, a default NodeReader will be used.
*/
public function setReader(NodeReader $reader) {
public function setReader(NodeReader $reader) : self {
$this->reader = $reader;
return $this;
}
private function getReader() {
private function getReader() : NodeReader {
if (!$this->reader) {
$this->reader = new NodeReader;
}
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.
*/
@@ -51,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;
@@ -105,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);
}
}

View File

@@ -29,6 +29,7 @@ class ObjectRetriever implements CustomCacheAndLogInterface {
private $options;
private $cache;
private $objects;
private $defaultReader;
const CO_API_URL = 'https://api.cloudobjects.net/';
@@ -107,6 +108,22 @@ class ObjectRetriever implements CustomCacheAndLogInterface {
$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) {
if (isset($this->logger))
$this->logger->info($message, [ 'elapsed_ms' => round((microtime(true) - $ts) * 1000) ]);
@@ -179,7 +196,15 @@ class ObjectRetriever implements CustomCacheAndLogInterface {
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,150 +7,180 @@
namespace CloudObjects\SDK;
use ML\IRI\IRI;
use ML\JsonLD\Node;
use GuzzleHttp\Client, GuzzleHttp\Handler\MockHandler,
GuzzleHttp\HandlerStack, GuzzleHttp\Psr7\Response;
GuzzleHttp\HandlerStack, GuzzleHttp\Psr7\Response;
class NodeReaderMockTest extends \PHPUnit\Framework\TestCase {
private $retriever;
private $reader;
private $retriever;
private $reader;
private function setMockResponse(Response $response) {
$mock = new MockHandler([$response]);
$handler = HandlerStack::create($mock);
$this->retriever->setClient(new Client(['handler' => $handler]));
}
private function setMockResponse(Response $response) {
$mock = new MockHandler([$response]);
$handler = HandlerStack::create($mock);
$this->retriever->setClient(new Client(['handler' => $handler]));
}
private function useRootResourceMock() {
$this->setMockResponse(new Response(200,
['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"}'));
}
private function useRootResourceMock() {
$this->setMockResponse(new Response(200,
['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"}'
));
}
protected function setUp(): void {
$this->retriever = new ObjectRetriever;
$this->reader = new NodeReader([
'prefixes' => [
'co' => 'coid://cloudobjects.io/',
'rdfs' => 'http://www.w3.org/2000/01/rdf-schema#'
]
]);
}
protected function setUp(): void {
$this->retriever = new ObjectRetriever;
$this->reader = new NodeReader([
'prefixes' => [
'co' => 'coid://cloudobjects.io/',
'rdfs' => 'http://www.w3.org/2000/01/rdf-schema#'
]
]);
$this->retriever->setDefaultReader($this->reader);
}
public function testHasType1() {
$coid = new IRI('coid://cloudobjects.io');
$this->useRootResourceMock();
$object = $this->retriever->getObjectNode($coid);
public function testHasType1() {
$coid = new IRI('coid://cloudobjects.io');
$this->useRootResourceMock();
$object = $this->retriever->getObjectNode($coid);
$this->assertTrue($this->reader->hasType($object, 'coid://cloudobjects.io/Namespace'));
$this->assertTrue($this->reader->hasType($object, 'co:Namespace'));
$this->assertFalse($this->reader->hasType($object, 'coid://cloudobjects.io/MemberRole'));
$this->assertFalse($this->reader->hasType($object, 'co:MemberRole'));
}
$this->assertTrue($this->reader->hasType($object, 'coid://cloudobjects.io/Namespace'));
$this->assertTrue($this->reader->hasType($object, 'co:Namespace'));
$this->assertFalse($this->reader->hasType($object, 'coid://cloudobjects.io/MemberRole'));
$this->assertFalse($this->reader->hasType($object, 'co:MemberRole'));
}
public function testHasPropertyValue1() {
$coid = new IRI('coid://cloudobjects.io');
$this->useRootResourceMock();
$object = $this->retriever->getObjectNode($coid);
public function testHasPropertyValue1() {
$coid = new IRI('coid://cloudobjects.io');
$this->useRootResourceMock();
$object = $this->retriever->getObjectNode($coid);
$this->assertTrue($this->reader->hasPropertyValue($object, 'http://www.w3.org/2000/01/rdf-schema#label', 'CloudObjects'));
$this->assertTrue($this->reader->hasPropertyValue($object, 'rdfs:label', 'CloudObjects'));
}
$this->assertTrue($this->reader->hasPropertyValue($object, 'http://www.w3.org/2000/01/rdf-schema#label', 'CloudObjects'));
$this->assertTrue($this->reader->hasPropertyValue($object, 'rdfs:label', 'CloudObjects'));
}
public function testGetFirstValueString1() {
$coid = new IRI('coid://cloudobjects.io');
$this->useRootResourceMock();
$object = $this->retriever->getObjectNode($coid);
public function testGetFirstValueString1() {
$coid = new IRI('coid://cloudobjects.io');
$this->useRootResourceMock();
$object = $this->retriever->getObjectNode($coid);
$this->assertEquals('CloudObjects', $this->reader->getFirstValueString($object, 'http://www.w3.org/2000/01/rdf-schema#label'));
$this->assertEquals('CloudObjects', $this->reader->getFirstValueString($object, 'rdfs:label'));
$this->assertEquals('CloudObjects', $this->reader->getFirstValueString($object, 'http://www.w3.org/2000/01/rdf-schema#label'));
$this->assertEquals('CloudObjects', $this->reader->getFirstValueString($object, 'rdfs:label'));
$this->assertNull($this->reader->getFirstValueString($object, 'coid://cloudobjects.io/makesTriplesVisibleTo'));
$this->assertNull($this->reader->getFirstValueString($object, 'co:makesTriplesVisibleTo'));
$this->assertNull($this->reader->getFirstValueString($object, 'coid://cloudobjects.io/makesTriplesVisibleTo'));
$this->assertNull($this->reader->getFirstValueString($object, 'co:makesTriplesVisibleTo'));
$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, 'coid://cloudobjects.io/makesTriplesVisibleTo', 'theDefaultValue'));
$this->assertEquals('theDefaultValue', $this->reader->getFirstValueString($object, 'co:makesTriplesVisibleTo', 'theDefaultValue'));
public function testGetFirstValueIRI1() {
$coid = new IRI('coid://cloudobjects.io');
$this->useRootResourceMock();
$object = $this->retriever->getObjectNode($coid);
$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->assertInstanceOf('ML\IRI\IRI', $this->reader->getFirstValueIRI($object, 'coid://cloudobjects.io/isVisibleTo'));
$this->assertInstanceOf('ML\IRI\IRI', $this->reader->getFirstValueIRI($object, 'co:isVisibleTo'));
$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() {
$coid = new IRI('coid://cloudobjects.io');
$this->useRootResourceMock();
$object = $this->retriever->getObjectNode($coid);
$this->assertInstanceOf(IRI::class, $this->reader->getFirstValueIRI($object, 'coid://cloudobjects.io/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, '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, 'co:isVisibleTo'));
public function testGetFirstValueNode1() {
$coid = new IRI('coid://cloudobjects.io');
$this->useRootResourceMock();
$object = $this->retriever->getObjectNode($coid);
$object = $this->retriever->getCloudObject($coid);
$this->assertInstanceOf('ML\JsonLD\Node', $this->reader->getFirstValueNode($object, 'coid://cloudobjects.io/isVisibleTo'));
$this->assertInstanceOf('ML\JsonLD\Node', $this->reader->getFirstValueNode($object, 'co:isVisibleTo'));
$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() {
$coid = new IRI('coid://cloudobjects.io');
$this->useRootResourceMock();
$object = $this->retriever->getObjectNode($coid);
$this->assertInstanceOf(Node::class, $this->reader->getFirstValueNode($object, 'coid://cloudobjects.io/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, 'co: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());
public function testGetAllValuesString1() {
$coid = new IRI('coid://cloudobjects.io');
$this->useRootResourceMock();
$object = $this->retriever->getObjectNode($coid);
$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() {
$coid = new IRI('coid://cloudobjects.io');
$this->useRootResourceMock();
$object = $this->retriever->getObjectNode($coid);
$this->assertCount(1, $this->reader->getAllValuesString($object, 'http://www.w3.org/2000/01/rdf-schema#label'));
$this->assertCount(1, $this->reader->getAllValuesString($object, 'rdfs:label'));
$this->assertCount(1, $this->reader->getAllValuesString($object, 'http://www.w3.org/2000/01/rdf-schema#label'));
$this->assertCount(1, $this->reader->getAllValuesString($object, 'rdfs:label'));
$this->assertEquals('CloudObjects', $this->reader->getAllValuesString($object, 'http://www.w3.org/2000/01/rdf-schema#label')[0]);
$this->assertEquals('CloudObjects', $this->reader->getAllValuesString($object, 'rdfs:label')[0]);
$this->assertEquals('CloudObjects', $this->reader->getAllValuesString($object, 'http://www.w3.org/2000/01/rdf-schema#label')[0]);
$this->assertEquals('CloudObjects', $this->reader->getAllValuesString($object, 'rdfs:label')[0]);
$this->assertCount(0, $this->reader->getAllValuesString($object, 'coid://cloudobjects.io/makesTriplesVisibleTo'));
$this->assertCount(0, $this->reader->getAllValuesString($object, 'co:makesTriplesVisibleTo'));
$this->assertCount(0, $this->reader->getAllValuesString($object, 'coid://cloudobjects.io/makesTriplesVisibleTo'));
$this->assertCount(0, $this->reader->getAllValuesString($object, 'co:makesTriplesVisibleTo'));
$this->assertCount(2, $this->reader->getAllValuesString($object, '@type'));
}
$this->assertCount(2, $this->reader->getAllValuesString($object, '@type'));
}
public function testGetAllValuesIRI1() {
$coid = new IRI('coid://cloudobjects.io');
$this->useRootResourceMock();
$object = $this->retriever->getObjectNode($coid);
public function testGetAllValuesIRI1() {
$coid = new IRI('coid://cloudobjects.io');
$this->useRootResourceMock();
$object = $this->retriever->getObjectNode($coid);
$this->assertCount(0, $this->reader->getAllValuesIRI($object, 'http://www.w3.org/2000/01/rdf-schema#label'));
$this->assertCount(0, $this->reader->getAllValuesIRI($object, 'rdfs:label'));
$this->assertCount(0, $this->reader->getAllValuesIRI($object, 'http://www.w3.org/2000/01/rdf-schema#label'));
$this->assertCount(0, $this->reader->getAllValuesIRI($object, 'rdfs:label'));
$this->assertCount(1, $this->reader->getAllValuesIRI($object, 'coid://cloudobjects.io/isVisibleTo'));
$this->assertCount(1, $this->reader->getAllValuesIRI($object, 'co:isVisibleTo'));
$this->assertCount(1, $this->reader->getAllValuesIRI($object, 'coid://cloudobjects.io/isVisibleTo'));
$this->assertCount(1, $this->reader->getAllValuesIRI($object, 'co:isVisibleTo'));
$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('ML\IRI\IRI', $this->reader->getAllValuesIRI($object, 'co:isVisibleTo')[0]);
$this->assertInstanceOf(IRI::class, $this->reader->getAllValuesIRI($object, 'coid://cloudobjects.io/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, '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, 'co:isVisibleTo')[0]);
}
public function testGetAllValuesNode1() {
$coid = new IRI('coid://cloudobjects.io');
$this->useRootResourceMock();
$object = $this->retriever->getObjectNode($coid);
public function testGetAllValuesNode1() {
$coid = new IRI('coid://cloudobjects.io');
$this->useRootResourceMock();
$object = $this->retriever->getObjectNode($coid);
$this->assertCount(0, $this->reader->getAllValuesNode($object, 'http://www.w3.org/2000/01/rdf-schema#label'));
$this->assertCount(0, $this->reader->getAllValuesNode($object, 'rdfs:label'));
$this->assertCount(0, $this->reader->getAllValuesNode($object, 'http://www.w3.org/2000/01/rdf-schema#label'));
$this->assertCount(0, $this->reader->getAllValuesNode($object, 'rdfs:label'));
$this->assertCount(1, $this->reader->getAllValuesNode($object, 'coid://cloudobjects.io/isVisibleTo'));
$this->assertCount(1, $this->reader->getAllValuesNode($object, 'co:isVisibleTo'));
$this->assertCount(1, $this->reader->getAllValuesNode($object, 'coid://cloudobjects.io/isVisibleTo'));
$this->assertCount(1, $this->reader->getAllValuesNode($object, 'co:isVisibleTo'));
$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('ML\JsonLD\Node', $this->reader->getAllValuesNode($object, 'co:isVisibleTo')[0]);
$this->assertInstanceOf(Node::class, $this->reader->getAllValuesNode($object, 'coid://cloudobjects.io/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, 'co: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());
}
}

View File

@@ -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'));

View File

@@ -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() {