Skip to content

Commit

Permalink
Merge pull request #5260 from neos/task/streamline-value-object-mappi…
Browse files Browse the repository at this point in the history
…ng-usages

TASK: Streamline value object mapping usages
  • Loading branch information
mhsdesign authored Sep 25, 2024
2 parents 1fb99b7 + c3a2349 commit 918e651
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 45 deletions.
5 changes: 3 additions & 2 deletions Neos.ContentRepository.Core/Classes/EventStore/Events.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,9 @@ public function getIterator(): \Traversable
}

/**
* @param \Closure $callback
* @return array<mixed>
* @template T
* @param \Closure(EventInterface|DecoratedEvent $event): T $callback
* @return list<T>
*/
public function map(\Closure $callback): array
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,9 @@ public function merge(self $other): self
}

/**
* @param \Closure(SubtreeTag): mixed $callback
* @return array<mixed>
* @template T
* @param \Closure(SubtreeTag $tag): T $callback
* @return list<T>
*/
public function map(\Closure $callback): array
{
Expand Down
14 changes: 12 additions & 2 deletions Neos.ContentRepository.Core/Classes/NodeType/NodeTypeNames.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public static function fromArray(array $array): self
*/
public static function fromStringArray(array $array): self
{
return new self(... array_map(
return new self(...array_map(
fn(string $serializedNodeTypeName): NodeTypeName => NodeTypeName::fromString($serializedNodeTypeName),
$array
));
Expand Down Expand Up @@ -79,12 +79,22 @@ public function getIterator(): \Traversable
yield from $this->nodeTypeNames;
}

/**
* @template T
* @param \Closure(NodeTypeName $nodeTypeName): T $callback
* @return list<T>
*/
public function map(\Closure $callback): array
{
return array_map($callback, $this->nodeTypeNames);
}

/**
* @return array<string>
*/
public function toStringArray(): array
{
return array_map(fn(NodeTypeName $nodeTypeName) => $nodeTypeName->value, $this->nodeTypeNames);
return $this->map(fn(NodeTypeName $nodeTypeName) => $nodeTypeName->value);
}

public function isEmpty(): bool
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,9 @@ public function all(): SubtreeTags
}

/**
* @param \Closure(SubtreeTag $tag, bool $inherited): mixed $callback
* @return array<mixed>
* @template T
* @param \Closure(SubtreeTag $tag, bool $inherited): T $callback
* @return list<T>
*/
public function map(\Closure $callback): array
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,8 +209,9 @@ public function nextAll(Node $referenceNode): self
}

/**
* @param \Closure(Node $node): mixed $callback
* @return array<mixed>
* @template T
* @param \Closure(Node $node): T $callback
* @return list<T>
*/
public function map(\Closure $callback): array
{
Expand All @@ -219,8 +220,6 @@ public function map(\Closure $callback): array

public function toNodeAggregateIds(): NodeAggregateIds
{
return NodeAggregateIds::create(...$this->map(
fn (Node $node): NodeAggregateId => $node->aggregateId,
));
return NodeAggregateIds::fromNodes($this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
* @implements \IteratorAggregate<string,NodeAggregateId>
* @api
*/
final class NodeAggregateIds implements \IteratorAggregate, \JsonSerializable
final class NodeAggregateIds implements \IteratorAggregate, \Countable, \JsonSerializable
{
/**
* @var array<string,NodeAggregateId>
Expand All @@ -38,7 +38,7 @@ private function __construct(NodeAggregateId ...$nodeAggregateIds)

public static function createEmpty(): self
{
return new self(...[]);
return new self();
}

public static function create(NodeAggregateId ...$nodeAggregateIds): self
Expand Down Expand Up @@ -70,9 +70,9 @@ public static function fromJsonString(string $jsonString): self

public static function fromNodes(Nodes $nodes): self
{
return self::fromArray(
array_map(fn(Node $node) => $node->aggregateId, iterator_to_array($nodes))
);
return self::fromArray($nodes->map(
fn (Node $node): NodeAggregateId => $node->aggregateId,
));
}

public function merge(self $other): self
Expand Down Expand Up @@ -116,4 +116,19 @@ public function getIterator(): \Traversable
{
yield from $this->nodeAggregateIds;
}

/**
* @template T
* @param \Closure(NodeAggregateId $nodeAggregateId): T $callback
* @return list<T>
*/
public function map(\Closure $callback): array
{
return array_map($callback, array_values($this->nodeAggregateIds));
}

public function count(): int
{
return count($this->nodeAggregateIds);
}
}
37 changes: 12 additions & 25 deletions Neos.Neos/Classes/Fusion/Cache/CacheTagSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,44 +37,38 @@ public function __construct(CacheTag ...$tags)
public static function forDescendantOfNodesFromNodes(
Nodes $nodes
): self {
return new self(...array_map(
CacheTag::forDescendantOfNodeFromNode(...),
iterator_to_array($nodes),
));
return new self(...$nodes->map(CacheTag::forDescendantOfNodeFromNode(...)));
}

public static function forDescendantOfNodesFromNodesWithoutWorkspace(
Nodes $nodes,
): self {
return new self(...array_map(
return new self(...$nodes->map(
static fn (Node $node) => CacheTag::forDescendantOfNode(
$node->contentRepositoryId,
CacheTagWorkspaceName::ANY,
$node->aggregateId,
),
iterator_to_array($nodes)
)
));
}

public static function forNodeAggregatesFromNodes(
Nodes $nodes
): self {
return new self(...array_map(
CacheTag::forNodeAggregateFromNode(...),
iterator_to_array($nodes)
return new self(...$nodes->map(
CacheTag::forNodeAggregateFromNode(...)
));
}

public static function forNodeAggregatesFromNodesWithoutWorkspace(
Nodes $nodes
): self {
return new self(...array_map(
return new self(...$nodes->map(
static fn (Node $node) => CacheTag::forNodeAggregate(
$node->contentRepositoryId,
CacheTagWorkspaceName::ANY,
$node->aggregateId
),
iterator_to_array($nodes),
)
));
}

Expand All @@ -83,24 +77,22 @@ public static function forNodeTypeNames(
WorkspaceName|CacheTagWorkspaceName $workspaceName,
NodeTypeNames $nodeTypeNames
): self {
return new self(...array_map(
return new self(...$nodeTypeNames->map(
static fn (NodeTypeName $nodeTypeName): CacheTag => CacheTag::forNodeTypeName(
$contentRepositoryId,
$workspaceName,
$nodeTypeName
),
iterator_to_array($nodeTypeNames)
)
));
}

public static function forWorkspaceNameFromNodes(Nodes $nodes): self
{
return new self(...array_map(
return new self(...$nodes->map(
static fn (Node $node): CacheTag => CacheTag::forWorkspaceName(
$node->contentRepositoryId,
$node->workspaceName,
),
iterator_to_array($nodes)
)
));
}

Expand All @@ -117,12 +109,7 @@ public function add(CacheTag $cacheTag): self
*/
public function toStringArray(): array
{
return array_unique(
array_map(
static fn (CacheTag $tag): string => $tag->value,
array_values($this->tags)
)
);
return array_keys($this->tags);
}

public function union(self $other): self
Expand Down
4 changes: 2 additions & 2 deletions Neos.Neos/Classes/Fusion/MenuItemsImplementation.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ protected function buildItems(): array
$maximumLevels = min($maximumLevels, $maxLevelsBasedOnLastLevel);
} elseif ($lastLevels < 0) {
$currentNodeAncestorAggregateIds = $this->getCurrentNodeRootlineAggregateIds();
$depthOfCurrentDocument = count(iterator_to_array($currentNodeAncestorAggregateIds)) - 1;
$depthOfCurrentDocument = count($currentNodeAncestorAggregateIds) - 1;
$maxLevelsBasedOnLastLevel = max($depthOfCurrentDocument + $lastLevels - $depthOfEntryParentNodeAggregateId + 1, 0);
$maximumLevels = min($maximumLevels, $maxLevelsBasedOnLastLevel);
}
Expand Down Expand Up @@ -311,7 +311,7 @@ protected function getCurrentNodeRootlineAggregateIds(): NodeAggregateIds
);

$this->currentNodeRootlineAggregateIds = NodeAggregateIds::create($this->currentNode->aggregateId)
->merge(NodeAggregateIds::fromNodes($currentNodeAncestors));
->merge($currentNodeAncestors->toNodeAggregateIds());

return $this->currentNodeRootlineAggregateIds;
}
Expand Down

0 comments on commit 918e651

Please sign in to comment.