From 16479f40942d47436f4498893d648773b00a22d9 Mon Sep 17 00:00:00 2001 From: Jeremy Dormitzer Date: Tue, 29 Jan 2019 21:49:46 -0500 Subject: [PATCH] Implement ObjectsService::replace --- src/Objects/ObjectsService.php | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/Objects/ObjectsService.php b/src/Objects/ObjectsService.php index 232daa8..058f9b5 100644 --- a/src/Objects/ObjectsService.php +++ b/src/Objects/ObjectsService.php @@ -279,13 +279,25 @@ class ObjectsService * Fully replaces the object referenced by $id by the new $fields * * @param string $id The id of the object to replace - * @param array $fields The new fields to replace the object with + * @param array $replacement The new fields to replace the object with * @return ActivityPubObject|null The replaced object, or null * if no object with $id exists */ - public function replace( $id, $fields ) + public function replace( $id, $replacement ) { - // TODO implement me + $existing = $this->getObject( $id ); + if ( ! $existing ) { + return; + } + $delta = array(); + foreach ( $existing->getFields() as $field ) { + if ( array_key_exists( $field->getName(), $replacement ) ) { + $delta[$field->getName()] = $replacement[$field->getName()]; + } else { + $delta[$field->getName()] = null; + } + } + return $this->update( $id, $delta ); } } ?>