Skip to content

Commit

Permalink
FEATURE: Add NodeSerializer
Browse files Browse the repository at this point in the history
  • Loading branch information
mhsdesign committed Feb 12, 2024
1 parent 9f8205d commit cd7ad38
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public static function create(
WorkspaceName $workspaceName,
DimensionSpacePoint $dimensionSpacePoint,
NodeAggregateId $nodeAggregateId,
) {
): self {
return new self($contentRepositoryId, $workspaceName, $dimensionSpacePoint, $nodeAggregateId);
}

Expand Down
59 changes: 59 additions & 0 deletions Neos.ContentRepositoryRegistry/Classes/NodeSerializer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

declare(strict_types=1);

namespace Neos\ContentRepositoryRegistry;

use Neos\ContentRepository\Core\Projection\ContentGraph\Node;
use Neos\ContentRepository\Core\Projection\ContentGraph\VisibilityConstraints;
use Neos\ContentRepository\Core\SharedModel\Node\NodeIdentity;
use Neos\Flow\Annotations as Flow;

/**
* Utility to convert the {@see Node} to its {@see NodeIdentity} and reverse.
*
* @api
*/
#[Flow\Scope('singleton')]
final readonly class NodeSerializer
{
public function __construct(
private ContentRepositoryRegistry $contentRepositoryRegistry
) {
}

public function denormalizeNodeFromIdentity(NodeIdentity $identity): Node
{
$contentRepository = $this->contentRepositoryRegistry->get($identity->contentRepositoryId);
$workspace = $contentRepository->getWorkspaceFinder()->findOneByName($identity->workspaceName);
if (!$workspace) {
throw new \RuntimeException(sprintf('Workspace could not be found while deserializing node NodeIdentity<%s>.', json_encode($identity, JSON_PARTIAL_OUTPUT_ON_ERROR)), 1707757488);
}
$subgraph = $contentRepository->getContentGraph()->getSubgraph(
$workspace->currentContentStreamId,
$identity->dimensionSpacePoint,
// todo policy? Or what to prevent from accidentally showing unwanted nodes.
VisibilityConstraints::withoutRestrictions()
);
$node = $subgraph->findNodeById($identity->nodeAggregateId);
if (!$node) {
throw new \RuntimeException(sprintf('NodeAggregateId could not be found while deserializing node NodeIdentity<%s>.', json_encode($identity, JSON_PARTIAL_OUTPUT_ON_ERROR)), 1707772263);
}
return $node;
}

public function normalizeNodeToIdentity(Node $node): NodeIdentity
{
$contentRepository = $this->contentRepositoryRegistry->get($node->subgraphIdentity->contentRepositoryId);
$workspace = $contentRepository->getWorkspaceFinder()->findOneByCurrentContentStreamId($node->subgraphIdentity->contentStreamId);
if (!$workspace) {
throw new \RuntimeException(sprintf('Workspace could not be found for current content stream %s.', $node->subgraphIdentity->contentStreamId->value), 1707757787);
}
return NodeIdentity::create(
$node->subgraphIdentity->contentRepositoryId,
$workspace->workspaceName,
$node->subgraphIdentity->dimensionSpacePoint,
$node->nodeAggregateId
);
}
}

0 comments on commit cd7ad38

Please sign in to comment.