Skip to content

Commit f4e10ae

Browse files
authored
Merge pull request #289 from utopia-php/feat-document-set-attributes
Add method to Document to set multiple attributes
2 parents 4035d3f + e6aaef4 commit f4e10ae

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

src/Database/Document.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ public function getAttribute(string $name, mixed $default = null): mixed
216216
*
217217
* @return self
218218
*/
219-
public function setAttribute(string $key, $value, string $type = self::SET_TYPE_ASSIGN): self
219+
public function setAttribute(string $key, mixed $value, string $type = self::SET_TYPE_ASSIGN): self
220220
{
221221
switch ($type) {
222222
case self::SET_TYPE_ASSIGN:
@@ -235,6 +235,21 @@ public function setAttribute(string $key, $value, string $type = self::SET_TYPE_
235235
return $this;
236236
}
237237

238+
/**
239+
* Set Attributes.
240+
*
241+
* @param array<string, mixed> $attributes
242+
* @return self
243+
*/
244+
public function setAttributes(array $attributes): self
245+
{
246+
foreach ($attributes as $key => $value) {
247+
$this->setAttribute($key, $value);
248+
}
249+
250+
return $this;
251+
}
252+
238253
/**
239254
* Remove Attribute.
240255
*

tests/Database/DocumentTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,30 @@ public function testSetAttribute(): void
175175
$this->assertEquals(['one'], $this->document->getAttribute('list', []));
176176
}
177177

178+
public function testSetAttributes(): void
179+
{
180+
$document = new Document(['$id' => ID::custom(''), '$collection' => 'users']);
181+
182+
$otherDocument = new Document([
183+
'$id' => ID::custom('new'),
184+
'$permissions' => [
185+
Permission::read(Role::any()),
186+
Permission::update(Role::user('new')),
187+
Permission::delete(Role::user('new')),
188+
],
189+
'email' => 'joe@example.com',
190+
'prefs' => new \stdClass(),
191+
]);
192+
193+
$document->setAttributes($otherDocument->getArrayCopy());
194+
195+
$this->assertEquals($otherDocument->getId(), $document->getId());
196+
$this->assertEquals('users', $document->getCollection());
197+
$this->assertEquals($otherDocument->getPermissions(), $document->getPermissions());
198+
$this->assertEquals($otherDocument->getAttribute('email'), $document->getAttribute('email'));
199+
$this->assertEquals($otherDocument->getAttribute('prefs'), $document->getAttribute('prefs'));
200+
}
201+
178202
public function testRemoveAttribute(): void
179203
{
180204
$this->document->removeAttribute('list');

0 commit comments

Comments
 (0)