Skip to content

Commit

Permalink
Also allow more than one relationship to be saved via a variadic prop…
Browse files Browse the repository at this point in the history
…erty argument on save method
  • Loading branch information
thewunder committed Mar 25, 2024
1 parent de70cdc commit 0ae65b9
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
4 changes: 1 addition & 3 deletions src/ObjectMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,9 +194,7 @@ public function load(array $objects, string ...$properties): array
$objectsByClass = $this->groupByClass($objects);
$loadedObjects = [];
foreach ($objectsByClass as $classObjects) {
foreach ($properties as $property) {
$loadedObjects += $relationshipManager->load($classObjects, $property);
}
$loadedObjects += $relationshipManager->load($classObjects, ...$properties);
}
return $loadedObjects;
}
Expand Down
18 changes: 12 additions & 6 deletions src/Relationship/RelationshipManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,24 +30,30 @@ public function addHandler(RelationshipHandler $handler): void
$this->handlers[$handler::getRelationshipClass()] = $handler;
}

public function save(array $objects, string $property): void
public function save(array $objects, string ...$properties): void
{
if (empty($objects)) {
return;
}

$relationshipType = $this->readAttribute(reset($objects), $property);
$this->getHandler($relationshipType)->save($objects, $relationshipType);
foreach ($properties as $property) {
$relationshipType = $this->readAttribute(reset($objects), $property);
$this->getHandler($relationshipType)->save($objects, $relationshipType);
}
}

public function load(array $objects, string $property): array
public function load(array $objects, string ...$properties): array
{
if (empty($objects)) {
return [];
}

$relationshipType = $this->readAttribute(reset($objects), $property);
return $this->getHandler($relationshipType)->load($objects, $relationshipType);
$loadedObjects = [];
foreach ($properties as $property) {
$relationshipType = $this->readAttribute(reset($objects), $property);
$loadedObjects += $this->getHandler($relationshipType)->load($objects, $relationshipType);
}
return $loadedObjects;
}

/**
Expand Down

0 comments on commit 0ae65b9

Please sign in to comment.