Skip to content

Commit

Permalink
Add method to Document to set multiple attributes
Browse files Browse the repository at this point in the history
This makes it easier and quicker to update several attributes on a
document from a different document.
  • Loading branch information
stnguyen90 committed Jul 12, 2023
1 parent 4035d3f commit d42ddd9
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/Database/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,21 @@ public function setAttribute(string $key, $value, string $type = self::SET_TYPE_
return $this;
}

/**
* Set Attributes.
*
* @param array $attributes
* @return self
*/
public function setAttributes(array $attributes): self
{
foreach ($attributes as $key => $value) {
$this->setAttribute($key, $value);
}

return $this;
}

/**
* Remove Attribute.
*
Expand Down
24 changes: 24 additions & 0 deletions tests/Database/DocumentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,30 @@ public function testSetAttribute(): void
$this->assertEquals(['one'], $this->document->getAttribute('list', []));
}

public function testSetAttributes(): void
{
$document = new Document(['$id' => ID::custom(''), '$collection' => 'users']);

$otherDocument = new Document([
'$id' => ID::custom('new'),
'$permissions' => [
Permission::read(Role::any()),
Permission::update(Role::user('new')),
Permission::delete(Role::user('new')),
],
'email' => '[email protected]',
'prefs' => new \stdClass(),
]);

$document->setAttributes($otherDocument->getArrayCopy());

$this->assertEquals($otherDocument->getId(), $document->getId());
$this->assertEquals('users', $document->getCollection());
$this->assertEquals($otherDocument->getPermissions(), $document->getPermissions());
$this->assertEquals($otherDocument->getAttribute('email'), $document->getAttribute('email'));
$this->assertEquals($otherDocument->getAttribute('prefs'), $document->getAttribute('prefs'));
}

public function testRemoveAttribute(): void
{
$this->document->removeAttribute('list');
Expand Down

0 comments on commit d42ddd9

Please sign in to comment.