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

Commit 71dbcc4

Browse files
author
Andrey Helldar
authored
Merge pull request #37 from TheDragonCode/2.x
Added `$only` and `$except` properties
2 parents 24c143f + 9461b70 commit 71dbcc4

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
@@ -69,6 +73,8 @@ public function set(string $key, $value): DataTransferObject
6973
*/
7074
public function merge(array $items): DataTransferObject
7175
{
76+
$items = $this->filter($items);
77+
7278
$this->setMap($items);
7379
$this->setItems($items);
7480

@@ -85,6 +91,31 @@ public function toArray(): array
8591
return $this->getProperties($this);
8692
}
8793

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

170201
protected function isAllowKey(string $key): bool
171202
{
172-
return ! in_array(Str::lower($key), $this->disallow, true);
203+
return ! in_array(Str::lower($key), self::DISALLOW, true);
173204
}
174205

175206
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)