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

Commit 9461b70

Browse files
Added $only and $except properties
1 parent 72fca12 commit 9461b70

File tree

6 files changed

+139
-2
lines changed

6 files changed

+139
-2
lines changed

src/DataTransferObject.php

+33-2
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,13 @@ abstract class DataTransferObject implements Contract
2626
use Reflection;
2727
use To;
2828

29+
protected const DISALLOW = ['map', 'only', 'except'];
30+
2931
protected $map = [];
3032

31-
protected $disallow = ['map', 'disallow'];
33+
protected $only = [];
34+
35+
protected $except = [];
3236

3337
/**
3438
* @param array $items
@@ -68,6 +72,8 @@ public function set(string $key, $value): DataTransferObject
6872
*/
6973
public function merge(array $items): DataTransferObject
7074
{
75+
$items = $this->filter($items);
76+
7177
$this->setMap($items);
7278
$this->setItems($items);
7379

@@ -84,6 +90,31 @@ public function toArray(): array
8490
return $this->getProperties($this);
8591
}
8692

93+
protected function filter(array $items): array
94+
{
95+
return $this->filterOnly(
96+
$this->filterExcept($items)
97+
);
98+
}
99+
100+
protected function filterOnly(array $items): array
101+
{
102+
if ($keys = $this->only) {
103+
return Arr::only($items, $keys);
104+
}
105+
106+
return $items;
107+
}
108+
109+
protected function filterExcept(array $items): array
110+
{
111+
if ($keys = $this->except) {
112+
return Arr::except($items, $keys);
113+
}
114+
115+
return $items;
116+
}
117+
87118
/**
88119
* @param array $items
89120
*
@@ -168,7 +199,7 @@ protected function isAllowProperty(string $key): bool
168199

169200
protected function isAllowKey(string $key): bool
170201
{
171-
return ! in_array(Str::lower($key), $this->disallow, true);
202+
return ! in_array(Str::lower($key), self::DISALLOW, true);
172203
}
173204

174205
protected function sourceKeyDoesntExist(array $items, string $key): bool

tests/Fixtures/Except.php

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tests\Fixtures;
6+
7+
use DragonCode\SimpleDataTransferObject\DataTransferObject;
8+
9+
class Except extends DataTransferObject
10+
{
11+
public $foo;
12+
13+
public $bar;
14+
15+
public $baq;
16+
17+
protected $map = [
18+
'qwe' => 'foo',
19+
'rty' => 'bar',
20+
];
21+
22+
protected $except = [
23+
'bar',
24+
];
25+
}

tests/Fixtures/Only.php

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tests\Fixtures;
6+
7+
use DragonCode\SimpleDataTransferObject\DataTransferObject;
8+
9+
class Only extends DataTransferObject
10+
{
11+
public $foo;
12+
13+
public $bar;
14+
15+
public $baq;
16+
17+
protected $map = [
18+
'qwe' => 'foo',
19+
'rty' => 'bar',
20+
];
21+
22+
protected $only = [
23+
'foo',
24+
];
25+
}

tests/TestCase.php

+2
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,6 @@ abstract class TestCase extends BaseTestCase
1313
protected $bar = 'Bar';
1414

1515
protected $baz = 'Baz';
16+
17+
protected $baq = 'Baq';
1618
}

tests/Unit/ExceptTest.php

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tests\Unit;
6+
7+
use Tests\Fixtures\Except;
8+
use Tests\TestCase;
9+
10+
class ExceptTest extends TestCase
11+
{
12+
public function testMake()
13+
{
14+
$object = Except::make([
15+
'bar' => $this->bar,
16+
'baq' => $this->baq,
17+
'qwe' => $this->foo,
18+
'rty' => $this->baq,
19+
]);
20+
21+
$this->assertIsArray($object->toArray());
22+
23+
$this->assertSame([
24+
'foo' => $this->foo,
25+
'bar' => $this->baq,
26+
'baq' => $this->baq,
27+
], $object->toArray());
28+
}
29+
}

tests/Unit/OnlyTest.php

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tests\Unit;
6+
7+
use Tests\Fixtures\Only;
8+
use Tests\TestCase;
9+
10+
class OnlyTest extends TestCase
11+
{
12+
public function testMake()
13+
{
14+
$object = Only::make([
15+
'foo' => $this->foo,
16+
'bar' => $this->bar,
17+
'baq' => $this->baq,
18+
]);
19+
20+
$this->assertSame($this->foo, $object->foo);
21+
22+
$this->assertNull($object->bar);
23+
$this->assertNull($object->baq);
24+
}
25+
}

0 commit comments

Comments
 (0)