Skip to content

Commit

Permalink
TASK: Adjust namings to remove obsolete fromNameAnd part
Browse files Browse the repository at this point in the history
and remove duplicated `fromReferences` naming in chain, now its `create` for root level
  • Loading branch information
mhsdesign committed Oct 31, 2024
1 parent 7cadf90 commit 3310d74
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ private function mapNodeReferencesToSerializedNodeReferences(NodeReferencesToWri
);
}

$serializedReferencesByProperty[] = SerializedNodeReferencesForName::fromNameAndSerializedReferences(
$serializedReferencesByProperty[] = SerializedNodeReferencesForName::fromSerializedReferences(
$referencesByProperty->referenceName,
$serializedReferences
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ private static function serializeProjectedReferences(References $references): Se
}

foreach ($serializedReferencesByName as $name => $referenceObjects) {
$serializedReferences[] = SerializedNodeReferencesForName::fromNameAndSerializedReferences(ReferenceName::fromString($name), $referenceObjects);
$serializedReferences[] = SerializedNodeReferencesForName::fromSerializedReferences(ReferenceName::fromString($name), $referenceObjects);
}

return SerializedNodeReferences::fromArray($serializedReferences);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

use Neos\ContentRepository\Core\Feature\NodeModification\Dto\PropertyValuesToWrite;
use Neos\ContentRepository\Core\SharedModel\Node\NodeAggregateId;
use Neos\ContentRepository\Core\SharedModel\Node\ReferenceName;

/**
* A single node references to write
Expand All @@ -40,6 +39,11 @@ private function __construct(
) {
}

/**
* The node aggregate id as target of the reference to write
*
* To set a collection of node aggregate ids as targets see {@see NodeReferencesForName::fromTargets()} as utility
*/
public static function fromTarget(NodeAggregateId $target): self
{
return new self($target, PropertyValuesToWrite::createEmpty());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,26 +45,26 @@ private function __construct(
$this->references = $references;
}

public static function fromNameAndTargets(ReferenceName $name, NodeAggregateIds $nodeAggregateIds): self
/**
* As the previously set references will be replaced by writing new references specifying
* no references for a name will delete the previous ones
*/
public static function createEmpty(ReferenceName $name): self
{
$references = array_map(NodeReferenceToWrite::fromTarget(...), iterator_to_array($nodeAggregateIds));
return new self($name, ...$references);
return new self($name, ...[]);
}

/**
* @param NodeReferenceToWrite[] $references
*/
public static function fromNameAndReferences(ReferenceName $name, array $references): self
public static function fromTargets(ReferenceName $name, NodeAggregateIds $nodeAggregateIds): self
{
$references = array_map(NodeReferenceToWrite::fromTarget(...), iterator_to_array($nodeAggregateIds));
return new self($name, ...$references);
}

/**
* As the previously set references will be replaced by writing new references specifying
* no references for a name will delete the previous ones
* @param NodeReferenceToWrite[] $references
*/
public static function createEmpty(ReferenceName $name): self
public static function fromReferences(ReferenceName $name, array $references): self
{
return new self($name, ...[]);
return new self($name, ...$references);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,28 +28,28 @@
/**
* @var array<string, NodeReferencesForName>
*/
public array $references;
public array $referencesForName;

private function __construct(NodeReferencesForName ...$references)
private function __construct(NodeReferencesForName ...$items)
{
$referencesByName = [];
foreach ($references as $reference) {
if (isset($referencesByName[$reference->referenceName->value])) {
throw new \InvalidArgumentException(sprintf('NodeReferencesToWrite does not accept references for the same name %s multiple times.', $reference->referenceName->value), 1718193720);
$referencesForName = [];
foreach ($items as $item) {
if (isset($referencesForName[$item->referenceName->value])) {
throw new \InvalidArgumentException(sprintf('NodeReferencesToWrite does not accept references for the same name %s multiple times.', $item->referenceName->value), 1718193720);
}
$referencesByName[$reference->referenceName->value] = $reference;
$referencesForName[$item->referenceName->value] = $item;
}
$this->references = $referencesByName;
$this->referencesForName = $referencesForName;
}

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

public static function fromReferences(NodeReferencesForName ...$references): self
public static function create(NodeReferencesForName ...$referencesForName): self
{
return new self(...$references);
return new self(...$referencesForName);
}

/**
Expand All @@ -62,23 +62,23 @@ public static function fromArray(array $references): self

public function withReference(NodeReferencesForName $referencesForName): self
{
$references = $this->references;
$references = $this->referencesForName;
$references[$referencesForName->referenceName->value] = $referencesForName;
return new self(...$references);
}

public function merge(NodeReferencesToWrite $other): self
{
return new self(...array_merge($this->references, $other->references));
return new self(...array_merge($this->referencesForName, $other->referencesForName));
}

public function getIterator(): \Traversable
{
yield from array_values($this->references);
yield from array_values($this->referencesForName);
}

public function isEmpty(): bool
{
return count($this->references) === 0;
return count($this->referencesForName) === 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ private function __construct(
* @param SerializedNodeReference[] $references
* @return self
*/
public static function fromNameAndSerializedReferences(ReferenceName $referenceName, array $references): self
public static function fromSerializedReferences(ReferenceName $referenceName, array $references): self
{
return new self($referenceName, ...$references);
}

public static function fromNameAndTargets(ReferenceName $referenceName, NodeAggregateIds $nodeAggregateIds): self
public static function fromTargets(ReferenceName $referenceName, NodeAggregateIds $nodeAggregateIds): self
{
$references = array_map(SerializedNodeReference::fromTarget(...), iterator_to_array($nodeAggregateIds));
return new self($referenceName, ...$references);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,52 +28,52 @@ public function testMultipleNamesAreNotAllowedInConstructor(): void
{
self::expectException(\InvalidArgumentException::class);

NodeReferencesToWrite::fromReferences(
NodeReferencesToWrite::create(
NodeReferencesForName::createEmpty(ReferenceName::fromString('foo')),
NodeReferencesForName::fromNameAndTargets(ReferenceName::fromString('bar'), NodeAggregateIds::fromArray(['fooo'])),
NodeReferencesForName::fromNameAndTargets(ReferenceName::fromString('foo'), NodeAggregateIds::fromArray(['abc'])),
NodeReferencesForName::fromTargets(ReferenceName::fromString('bar'), NodeAggregateIds::fromArray(['fooo'])),
NodeReferencesForName::fromTargets(ReferenceName::fromString('foo'), NodeAggregateIds::fromArray(['abc'])),
);
}

public function testMergeOverridesPrevious(): void
{
$a = NodeReferencesToWrite::fromReferences(
NodeReferencesForName::fromNameAndTargets(ReferenceName::fromString('foo'), NodeAggregateIds::fromArray(['abc'])),
NodeReferencesForName::fromNameAndTargets(ReferenceName::fromString('bar'), NodeAggregateIds::fromArray(['fooo'])),
$a = NodeReferencesToWrite::create(
NodeReferencesForName::fromTargets(ReferenceName::fromString('foo'), NodeAggregateIds::fromArray(['abc'])),
NodeReferencesForName::fromTargets(ReferenceName::fromString('bar'), NodeAggregateIds::fromArray(['fooo'])),
);

$b = NodeReferencesToWrite::fromReferences(
NodeReferencesForName::fromNameAndTargets(ReferenceName::fromString('new'), NodeAggregateIds::fromArray(['la-li-lu'])),
$b = NodeReferencesToWrite::create(
NodeReferencesForName::fromTargets(ReferenceName::fromString('new'), NodeAggregateIds::fromArray(['la-li-lu'])),
NodeReferencesForName::createEmpty(ReferenceName::fromString('foo')),
);

$c = $a->merge($b);

self::assertEquals(
iterator_to_array(NodeReferencesToWrite::fromReferences(
iterator_to_array(NodeReferencesToWrite::create(
NodeReferencesForName::createEmpty(ReferenceName::fromString('foo')),
NodeReferencesForName::fromNameAndTargets(ReferenceName::fromString('bar'), NodeAggregateIds::fromArray(['fooo'])),
NodeReferencesForName::fromNameAndTargets(ReferenceName::fromString('new'), NodeAggregateIds::fromArray(['la-li-lu'])),
NodeReferencesForName::fromTargets(ReferenceName::fromString('bar'), NodeAggregateIds::fromArray(['fooo'])),
NodeReferencesForName::fromTargets(ReferenceName::fromString('new'), NodeAggregateIds::fromArray(['la-li-lu'])),
)),
iterator_to_array($c)
);
}

public function testAppendOverridesPrevious(): void
{
$a = NodeReferencesToWrite::fromReferences(
NodeReferencesForName::fromNameAndTargets(ReferenceName::fromString('foo'), NodeAggregateIds::fromArray(['abc'])),
NodeReferencesForName::fromNameAndTargets(ReferenceName::fromString('bar'), NodeAggregateIds::fromArray(['fooo'])),
$a = NodeReferencesToWrite::create(
NodeReferencesForName::fromTargets(ReferenceName::fromString('foo'), NodeAggregateIds::fromArray(['abc'])),
NodeReferencesForName::fromTargets(ReferenceName::fromString('bar'), NodeAggregateIds::fromArray(['fooo'])),
);

$b = NodeReferencesForName::fromNameAndTargets(ReferenceName::fromString('bar'), NodeAggregateIds::fromArray(['la-li-lu', 'second']));
$b = NodeReferencesForName::fromTargets(ReferenceName::fromString('bar'), NodeAggregateIds::fromArray(['la-li-lu', 'second']));

$c = $a->withReference($b);

self::assertEquals(
iterator_to_array(NodeReferencesToWrite::fromReferences(
NodeReferencesForName::fromNameAndTargets(ReferenceName::fromString('foo'), NodeAggregateIds::fromArray(['abc'])),
NodeReferencesForName::fromNameAndTargets(ReferenceName::fromString('bar'), NodeAggregateIds::fromArray(['la-li-lu', 'second']))
iterator_to_array(NodeReferencesToWrite::create(
NodeReferencesForName::fromTargets(ReferenceName::fromString('foo'), NodeAggregateIds::fromArray(['abc'])),
NodeReferencesForName::fromTargets(ReferenceName::fromString('bar'), NodeAggregateIds::fromArray(['la-li-lu', 'second']))
)),
iterator_to_array($c)
);
Expand All @@ -83,7 +83,7 @@ public function testSameTargetsAreNotAllowedMultipleTimes(): void
{
self::expectException(\InvalidArgumentException::class);

NodeReferencesForName::fromNameAndReferences(ReferenceName::fromString('bar'), [
NodeReferencesForName::fromReferences(ReferenceName::fromString('bar'), [
NodeReferenceToWrite::fromTarget(NodeAggregateId::fromString('node1')),
NodeReferenceToWrite::fromTarget(NodeAggregateId::fromString('node-other')),
NodeReferenceToWrite::fromTarget(NodeAggregateId::fromString('node1')),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ public function extractPropertyValuesAndReferences(array $nodeDataRow, NodeType
if (!is_array($propertyValue)) {
$propertyValue = [$propertyValue];
}
$references[] = SerializedNodeReferencesForName::fromNameAndTargets(
$references[] = SerializedNodeReferencesForName::fromTargets(
ReferenceName::fromString($propertyName),
NodeAggregateIds::fromArray($propertyValue)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ protected function mapRawNodeReferencesToNodeReferencesToWrite(array $deserializ
$properties = isset($referenceData['properties']) ? $this->deserializeProperties($referenceData['properties']) : PropertyValuesToWrite::createEmpty();
$references[] = NodeReferenceToWrite::fromTargetAndProperties(NodeAggregateId::fromString($referenceData['target']), $properties);
}
$referencesForProperty[] = NodeReferencesForName::fromNameAndReferences(ReferenceName::fromString($nodeReferencesForProperty['referenceName']), $references);
$referencesForProperty[] = NodeReferencesForName::fromReferences(ReferenceName::fromString($nodeReferencesForProperty['referenceName']), $references);
}
return NodeReferencesToWrite::fromArray($referencesForProperty);
}
Expand Down

0 comments on commit 3310d74

Please sign in to comment.