Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add method to Document to set multiple attributes #289

Merged
merged 1 commit into from
Jul 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion src/Database/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ public function getAttribute(string $name, mixed $default = null): mixed
*
* @return self
*/
public function setAttribute(string $key, $value, string $type = self::SET_TYPE_ASSIGN): self
public function setAttribute(string $key, mixed $value, string $type = self::SET_TYPE_ASSIGN): self
{
switch ($type) {
case self::SET_TYPE_ASSIGN:
Expand All @@ -235,6 +235,21 @@ public function setAttribute(string $key, $value, string $type = self::SET_TYPE_
return $this;
}

/**
* Set Attributes.
*
* @param array<string, mixed> $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