Skip to content
This repository was archived by the owner on Apr 7, 2025. It is now read-only.

Commit bcebe07

Browse files
author
Andrey Helldar
authored
Merge pull request #31 from TheDragonCode/2.x
Added `merge` method
2 parents 134fcec + 6bb079b commit bcebe07

File tree

2 files changed

+52
-2
lines changed

2 files changed

+52
-2
lines changed

src/DataTransferObject.php

+12-2
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,7 @@ abstract class DataTransferObject implements Contract
3535
*/
3636
public function __construct(array $items = [])
3737
{
38-
$this->setMap($items);
39-
$this->setItems($items);
38+
$this->merge($items);
4039
}
4140

4241
public function get(string $key)
@@ -59,6 +58,17 @@ public function set(string $key, $value): DataTransferObject
5958
return $this;
6059
}
6160

61+
/**
62+
* @throws \ReflectionException
63+
*/
64+
public function merge(array $items): DataTransferObject
65+
{
66+
$this->setMap($items);
67+
$this->setItems($items);
68+
69+
return $this;
70+
}
71+
6272
/**
6373
* @throws ReflectionException
6474
*

tests/Unit/MergeTest.php

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tests\Unit;
6+
7+
use Tests\Fixtures\Map;
8+
use Tests\TestCase;
9+
10+
class MergeTest extends TestCase
11+
{
12+
public function testMerge()
13+
{
14+
$object = Map::make([
15+
'wa' => [
16+
'sd' => $this->foo,
17+
],
18+
19+
'qwe.rty' => $this->bar,
20+
'baz' => $this->baz,
21+
]);
22+
23+
$this->assertSame($this->foo, $object->foo);
24+
$this->assertSame($this->bar, $object->bar);
25+
$this->assertSame($this->baz, $object->baz);
26+
27+
$object->merge([
28+
'wa' => [
29+
'sd' => 'Some 1',
30+
],
31+
32+
'qwe.rty' => 'Some 2',
33+
'baz' => 'Some 3',
34+
]);
35+
36+
$this->assertSame('Some 1', $object->foo);
37+
$this->assertSame('Some 2', $object->bar);
38+
$this->assertSame('Some 3', $object->baz);
39+
}
40+
}

0 commit comments

Comments
 (0)