@@ -26,9 +26,13 @@ abstract class DataTransferObject implements Contract
26
26
use Reflection;
27
27
use To;
28
28
29
+ protected const DISALLOW = ['map ' , 'only ' , 'except ' ];
30
+
29
31
protected $ map = [];
30
32
31
- protected $ disallow = ['map ' , 'disallow ' ];
33
+ protected $ only = [];
34
+
35
+ protected $ except = [];
32
36
33
37
/**
34
38
* @param array $items
@@ -68,6 +72,8 @@ public function set(string $key, $value): DataTransferObject
68
72
*/
69
73
public function merge (array $ items ): DataTransferObject
70
74
{
75
+ $ items = $ this ->filter ($ items );
76
+
71
77
$ this ->setMap ($ items );
72
78
$ this ->setItems ($ items );
73
79
@@ -84,6 +90,31 @@ public function toArray(): array
84
90
return $ this ->getProperties ($ this );
85
91
}
86
92
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
+
87
118
/**
88
119
* @param array $items
89
120
*
@@ -168,7 +199,7 @@ protected function isAllowProperty(string $key): bool
168
199
169
200
protected function isAllowKey (string $ key ): bool
170
201
{
171
- return ! in_array (Str::lower ($ key ), $ this -> disallow , true );
202
+ return ! in_array (Str::lower ($ key ), self :: DISALLOW , true );
172
203
}
173
204
174
205
protected function sourceKeyDoesntExist (array $ items , string $ key ): bool
0 commit comments