@@ -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
@@ -69,6 +73,8 @@ public function set(string $key, $value): DataTransferObject
69
73
*/
70
74
public function merge (array $ items ): DataTransferObject
71
75
{
76
+ $ items = $ this ->filter ($ items );
77
+
72
78
$ this ->setMap ($ items );
73
79
$ this ->setItems ($ items );
74
80
@@ -85,6 +91,31 @@ public function toArray(): array
85
91
return $ this ->getProperties ($ this );
86
92
}
87
93
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
+
88
119
/**
89
120
* @param array $items
90
121
*
@@ -169,7 +200,7 @@ protected function isAllowProperty(string $key): bool
169
200
170
201
protected function isAllowKey (string $ key ): bool
171
202
{
172
- return ! in_array (Str::lower ($ key ), $ this -> disallow , true );
203
+ return ! in_array (Str::lower ($ key ), self :: DISALLOW , true );
173
204
}
174
205
175
206
protected function sourceKeyDoesntExist (array $ items , string $ key ): bool
0 commit comments