First implementation of a new CloudObject node wrapper class

This commit is contained in:
2026-05-15 09:44:44 +00:00
parent 5584f10462
commit f29af2b664
3 changed files with 169 additions and 28 deletions

View File

@@ -0,0 +1,108 @@
<?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;
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) {
$this->reader = $reader;
}
private function getReader() {
if (!$this->reader) {
$this->reader = new NodeReader;
}
return $this->reader;
}
/**
* Get the COID of this object.
*/
public function getCOID() : IRI {
return $this->coid;
}
/**
* Get the raw object node.
*/
public function getObjectNode() : Node {
return $this->node;
}
/**
* 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);
}
}

View File

@@ -170,15 +170,34 @@ class ObjectRetriever implements CustomCacheAndLogInterface {
}
/**
* Get an object description from CloudObjects. Attempts to get object
* from in-memory cache first, stored static configurations next,
* configured external cache third, and finally calls the Object API
* on CloudObjects Core. Returns null if the object was not found.
* Get an object description and return a CloudObject.
*/
public function getCloudObject(IRI $coid) {
$node = $this->getObjectNode($coid);
if (!$node) {
// Object not found
return null;
}
return new CloudObject($coid, $node);
}
/**
* Get an object description and return a node.
*
* @deprecated Use getObjectNode() instead
*/
public function getObject(IRI $coid) {
return $this->getObjectNode($coid);
}
/**
* Get an object description from CloudObjects and return a node.
*
* @param IRI $coid COID of the object
* @return Node|null
*/
public function getObject(IRI $coid) {
public function getObjectNode(IRI $coid) {
if (!COIDParser::isValidCOID($coid))
throw new Exception("Not a valid COID.");
@@ -394,10 +413,11 @@ class ObjectRetriever implements CustomCacheAndLogInterface {
/**
* Get an object description from CloudObjects. Shorthand method for
* "getObject" which allows passing the COID as string instead of IRI.
* "getObjectNode" which allows passing the COID as string instead of IRI.
*
* @param any $coid
* @return Node|null
* @deprecated Interface may change
*/
public function get($coid) {
if (is_string($coid))
@@ -425,7 +445,7 @@ class ObjectRetriever implements CustomCacheAndLogInterface {
$ts = microtime(true);
$cacheId = $object->getId().'#'.$filename;
$fileData = $this->getFromCache($cacheId);
$fileData = $this->getFromCache($cacheId);
// Parse cached data into revision and content
if (isset($fileData)) {