From 0ae65b9168decce2bd8ae62180f22042a0115f28 Mon Sep 17 00:00:00 2001 From: Michael O'Connell Date: Mon, 25 Mar 2024 08:56:20 -0500 Subject: [PATCH] Also allow more than one relationship to be saved via a variadic property argument on save method --- src/ObjectMapper.php | 4 +--- src/Relationship/RelationshipManager.php | 18 ++++++++++++------ 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/src/ObjectMapper.php b/src/ObjectMapper.php index 577e97f..1c271d2 100644 --- a/src/ObjectMapper.php +++ b/src/ObjectMapper.php @@ -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; } diff --git a/src/Relationship/RelationshipManager.php b/src/Relationship/RelationshipManager.php index 96b2576..5d52582 100644 --- a/src/Relationship/RelationshipManager.php +++ b/src/Relationship/RelationshipManager.php @@ -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; } /**