Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TASK: docs and readability of Node::getProperty #4328

Merged
merged 1 commit into from
Nov 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function __construct(
}

/**
* @param array<string,mixed> $valueAndType
* @param array{type:string,value:mixed} $valueAndType
*/
public static function fromArray(array $valueAndType): self
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public static function createEmpty(): self
}

/**
* @param array<string,mixed> $propertyValues
* @param array<string,array{type:string,value:mixed}|SerializedPropertyValue|null> $propertyValues
kdambekalns marked this conversation as resolved.
Show resolved Hide resolved
*/
public static function fromArray(array $propertyValues): self
{
Expand Down Expand Up @@ -129,6 +129,9 @@ public function splitByScope(NodeType $nodeType): array
);
}

/**
* @phpstan-assert-if-true !null $this->getProperty()
*/
public function propertyExists(string $propertyName): bool
{
return isset($this->values[$propertyName]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,23 @@
*
* The node does not have structure information, i.e. no infos
* about its children. To f.e. fetch children, you need to fetch
* the subgraph via $node->subgraphIdentity and then
* call findChildNodes() on the subgraph.
* the subgraph {@see ContentGraphInterface::getSubgraph()} via
* $subgraphIdentity {@see Node::$subgraphIdentity}. and then
* call findChildNodes() {@see ContentSubgraphInterface::findChildNodes()}
* on the subgraph.
*
* @api Note: The constructor is not part of the public API
*/
final readonly class Node
{
/**
* @param ContentSubgraphIdentity $subgraphIdentity This is part of the node's "Read Model" identity which is defined by: {@see self::subgraphIdentity} and {@see self::nodeAggregateId}
* @param ContentSubgraphIdentity $subgraphIdentity This is part of the node's "Read Model" identity which is defined by: {@see self::subgraphIdentity} and {@see self::nodeAggregateId}. With this information, you can fetch a Subgraph using {@see ContentGraphInterface::getSubgraph()}.
* @param NodeAggregateId $nodeAggregateId NodeAggregateId (identifier) of this node. This is part of the node's "Read Model" identity which is defined by: {@see self::subgraphIdentity} and {@see self::nodeAggregateId}
* @param OriginDimensionSpacePoint $originDimensionSpacePoint The DimensionSpacePoint the node originates in. Usually needed to address a Node in a NodeAggregate in order to update it.
* @param NodeAggregateClassification $classification The classification (regular, root, tethered) of this node
* @param NodeTypeName $nodeTypeName The node's node type name; always set, even if unknown to the NodeTypeManager
* @param NodeType|null $nodeType The node's node type, null if unknown to the NodeTypeManager - @deprecated Don't rely on this too much, as the capabilities of the NodeType here will probably change a lot; Ask the {@see NodeTypeManager} instead
* @param PropertyCollection $properties All properties of this node. References are NOT part of this API; To access references, {@see ContentSubgraphInterface::findReferences()} can be used; To read the serialized properties, call properties->serialized().
* @param PropertyCollection $properties All properties of this node. References are NOT part of this API; To access references, {@see ContentSubgraphInterface::findReferences()} can be used; To read the serialized properties use {@see PropertyCollection::serialized()}.
* @param NodeName|null $nodeName The optional name of this node {@see ContentSubgraphInterface::findChildNodeConnectedThroughEdgeName()}
* @param Timestamps $timestamps Creation and modification timestamps of this node
*/
Expand All @@ -69,25 +71,23 @@ public static function create(ContentSubgraphIdentity $subgraphIdentity, NodeAgg
}

/**
* Returns the specified property.
*
* If the node has a content object attached, the property will be fetched
* there if it is gettable.
* Returns the specified property, or null if it does not exist (or was set to null -> unset)
*
* @param string $propertyName Name of the property
* @return mixed value of the property
* @api
*/
public function getProperty(string $propertyName): mixed
{
return $this->properties[$propertyName];
return $this->properties->offsetGet($propertyName);
kdambekalns marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* If this node has a property with the given name. Does NOT check the NodeType; but checks
* for a non-NULL property value.
* If this node has a property with the given name. It does not check if the property exists in the current NodeType schema.
*
* That means that {@see self::getProperty()} will not be null, except for the rare case the property deserializing returns null.
*
* @param string $propertyName
* @param string $propertyName Name of the property
* @return boolean
* @api
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
* @implements \IteratorAggregate<string,mixed>
* @api This object should not be instantiated by 3rd parties, but it is part of the {@see Node} read model
*/
final class PropertyCollection implements \ArrayAccess, \IteratorAggregate
final class PropertyCollection implements \ArrayAccess, \IteratorAggregate, \Countable
{
/**
* Properties from Nodes
Expand Down Expand Up @@ -56,14 +56,16 @@ public function offsetExists($offset): bool

public function offsetGet($offset): mixed
{
if (!isset($this->deserializedPropertyValuesRuntimeCache[$offset])) {
$serializedProperty = $this->serializedPropertyValues->getProperty($offset);
$this->deserializedPropertyValuesRuntimeCache[$offset] = $serializedProperty === null
? null
: $this->propertyConverter->deserializePropertyValue($serializedProperty);
if (array_key_exists($offset, $this->deserializedPropertyValuesRuntimeCache)) {
return $this->deserializedPropertyValuesRuntimeCache[$offset];
}

return $this->deserializedPropertyValuesRuntimeCache[$offset];
$serializedProperty = $this->serializedPropertyValues->getProperty($offset);
if ($serializedProperty === null) {
return null;
}
return $this->deserializedPropertyValuesRuntimeCache[$offset] =
$this->propertyConverter->deserializePropertyValue($serializedProperty);
}

public function offsetSet($offset, $value): never
Expand All @@ -90,4 +92,9 @@ public function serialized(): SerializedPropertyValues
{
return $this->serializedPropertyValues;
}

public function count(): int
{
return count($this->serializedPropertyValues);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,9 @@ public function execute(
DimensionSpacePointSet $coveredDimensionSpacePoints,
ContentStreamId $contentStreamForWriting
): ?CommandResult {
if ($node->hasProperty($this->propertyName)) {
$properties = $node->properties;
/** @var SerializedPropertyValue $serializedPropertyValue safe since Node::hasProperty */
$serializedPropertyValue = $properties->serialized()->getProperty($this->propertyName);
$properties = $node->properties->serialized();
if ($properties->propertyExists($this->propertyName)) {
$serializedPropertyValue = $properties->getProperty($this->propertyName);
$propertyValue = $serializedPropertyValue->value;
if (!is_string($propertyValue)) {
throw new \Exception(
Expand Down
Loading