170 lines
5.0 KiB
PHP
170 lines
5.0 KiB
PHP
<?php
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
namespace CloudObjects\SDK;
|
|
|
|
use ML\IRI\IRI;
|
|
use ML\JsonLD\Node;
|
|
use CloudObjects\SDK\NodeReader;
|
|
use Webmozart\Assert\Assert;
|
|
|
|
/**
|
|
* A CloudObject encapsulates an object node for an object stored in CloudObjects Core
|
|
* with convenience methods to access properties.
|
|
*/
|
|
class CloudObject {
|
|
|
|
private $coid;
|
|
private $node;
|
|
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.");
|
|
|
|
$this->coid = $coid;
|
|
$this->node = $node;
|
|
}
|
|
|
|
/**
|
|
* Specify a custom NodeReader to use for this CloudObject.
|
|
* If not set, a default NodeReader will be used.
|
|
*/
|
|
public function setReader(NodeReader $reader) : self {
|
|
$this->reader = $reader;
|
|
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* Get the NodeReader used for this CloudObject.
|
|
* Returns a default NodeReader if non has been set.
|
|
*/
|
|
protected 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.
|
|
*/
|
|
public function getCOID() : IRI {
|
|
return $this->coid;
|
|
}
|
|
|
|
/**
|
|
* Get the object node encapsulated in this CloudObject.
|
|
*/
|
|
public function getAsNode() : Node {
|
|
return $this->node;
|
|
}
|
|
|
|
/**
|
|
* Checks if a property exists on this object.
|
|
*/
|
|
public function has($property) : bool {
|
|
return $this->getReader()->hasProperty($this->node, $property);
|
|
}
|
|
|
|
/**
|
|
* Get the value of a property as a string.
|
|
* If the property has multiple values, only the first is returned.
|
|
*/
|
|
public function getString($property, string $default = null) : ?string {
|
|
return $this->getReader()->getFirstValueString($this->node, $property, $default);
|
|
}
|
|
|
|
/**
|
|
* Get the value of a property as an integer.
|
|
* If the property has multiple values, only the first is returned.
|
|
*/
|
|
public function getInt($property, int $default = null) : ?int {
|
|
return $this->getReader()->getFirstValueInt($this->node, $property, $default);
|
|
}
|
|
|
|
/**
|
|
* Get the value of a property as a float.
|
|
* If the property has multiple values, only the first is returned.
|
|
*/
|
|
public function getFloat($property, float $default = null) : ?float {
|
|
return $this->getReader()->getFirstValueFloat($this->node, $property, $default);
|
|
}
|
|
|
|
/**
|
|
* Get the value of a property as a boolean.
|
|
* If the property has multiple values, only the first is returned.
|
|
*/
|
|
public function getBool($property, bool $default = null) : ?bool {
|
|
return $this->getReader()->getFirstValueBool($this->node, $property, $default);
|
|
}
|
|
|
|
/**
|
|
* Get the value of a property as an IRI.
|
|
* If the property has multiple values, only the first is returned.
|
|
*/
|
|
public function getIRI($property, IRI $default = null) : ?IRI {
|
|
return $this->getReader()->getFirstValueIRI($this->node, $property, $default);
|
|
}
|
|
|
|
/**
|
|
* Get the value of a property as a Node.
|
|
* If the property has multiple values, only the first is returned.
|
|
*/
|
|
public function getNode($property, Node $default = null) : ?Node {
|
|
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);
|
|
}
|
|
|
|
/**
|
|
* 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);
|
|
}
|
|
|
|
/**
|
|
* Get the revision of the object.
|
|
*/
|
|
public function getRevision() : string {
|
|
return $this->getString(Constants::PROPERTY_REVISION);
|
|
}
|
|
|
|
}
|