18
18
* limitations under the License.
19
19
*/
20
20
21
+ use LogicException ;
21
22
use stdClass ;
22
23
use UnexpectedValueException ;
23
24
@@ -57,7 +58,7 @@ class ObjectMerge
57
58
*/
58
59
public function __invoke (stdClass ...$ objects )
59
60
{
60
- return self ::doMerge (false , self ::DEFAULT_OPTS , null , $ objects );
61
+ return self ::doMerge (false , self ::DEFAULT_OPTS , $ objects );
61
62
}
62
63
63
64
/**
@@ -66,7 +67,7 @@ public function __invoke(stdClass ...$objects)
66
67
*/
67
68
public static function merge (stdClass ...$ objects )
68
69
{
69
- return self ::doMerge (false , self ::DEFAULT_OPTS , null , $ objects );
70
+ return self ::doMerge (false , self ::DEFAULT_OPTS , $ objects );
70
71
}
71
72
72
73
/**
@@ -75,7 +76,7 @@ public static function merge(stdClass ...$objects)
75
76
*/
76
77
public static function mergeRecursive (stdClass ...$ objects )
77
78
{
78
- return self ::doMerge (true , self ::DEFAULT_OPTS , null , $ objects );
79
+ return self ::doMerge (true , self ::DEFAULT_OPTS , $ objects );
79
80
}
80
81
81
82
/**
@@ -85,7 +86,7 @@ public static function mergeRecursive(stdClass ...$objects)
85
86
*/
86
87
public static function mergeOpts ($ opts , stdClass ...$ objects )
87
88
{
88
- return self ::doMerge (false , $ opts , null , $ objects );
89
+ return self ::doMerge (false , $ opts , $ objects );
89
90
}
90
91
91
92
/**
@@ -95,7 +96,7 @@ public static function mergeOpts($opts, stdClass ...$objects)
95
96
*/
96
97
public static function mergeRecursiveOpts ($ opts , stdClass ...$ objects )
97
98
{
98
- return self ::doMerge (true , $ opts , null , $ objects );
99
+ return self ::doMerge (true , $ opts , $ objects );
99
100
}
100
101
101
102
/**
@@ -153,22 +154,20 @@ private static function compareTypes($left, $right)
153
154
/**
154
155
* @param bool $recurse
155
156
* @param int $opts
156
- * @param callable $cb
157
- * @param string|int $key
158
157
* @param array $leftValue
159
158
* @param array $rightValue
160
159
* @return array
161
160
*/
162
- private static function mergeArrayValues ($ recurse , $ opts , $ cb , $ key , array $ leftValue , array $ rightValue )
161
+ private static function mergeArrayValues ($ recurse , $ opts , array $ leftValue , array $ rightValue )
163
162
{
164
163
$ out = array_merge ($ leftValue , $ rightValue );
165
164
166
165
foreach ($ out as $ i => &$ v ) {
167
166
$ vt = gettype ($ v );
168
167
if (self ::OBJECT_T === $ vt ) {
169
- $ v = self ::mergeObjectValues ($ recurse , $ opts , $ cb , $ i , new stdClass (), $ v );
168
+ $ v = self ::mergeObjectValues ($ recurse , $ opts , new stdClass (), $ v );
170
169
} elseif (self ::ARRAY_T === $ vt ) {
171
- $ v = self ::mergeArrayValues ($ recurse , $ opts , $ cb , $ i , [], $ v );
170
+ $ v = self ::mergeArrayValues ($ recurse , $ opts , [], $ v );
172
171
}
173
172
}
174
173
@@ -182,22 +181,20 @@ private static function mergeArrayValues($recurse, $opts, $cb, $key, array $left
182
181
/**
183
182
* @param bool $recurse
184
183
* @param int $opts
185
- * @param callable $cb
186
- * @param string|int $key
187
184
* @param stdClass $leftValue
188
185
* @param stdClass $rightValue
189
186
* @return stdClass
190
187
*/
191
- private static function mergeObjectValues ($ recurse , $ opts , $ cb , $ key , stdClass $ leftValue , stdClass $ rightValue )
188
+ private static function mergeObjectValues ($ recurse , $ opts , stdClass $ leftValue , stdClass $ rightValue )
192
189
{
193
190
$ out = new stdClass ();
191
+
194
192
foreach (array_merge (get_object_vars ($ leftValue ), get_object_vars ($ rightValue )) as $ k => $ v ) {
195
193
$ leftDefined = property_exists ($ leftValue , $ k );
196
194
$ rightDefined = property_exists ($ rightValue , $ k );
197
195
$ out ->{$ k } = self ::mergeValues (
198
196
$ recurse ,
199
197
$ opts ,
200
- $ cb ,
201
198
$ k ,
202
199
$ leftDefined ? $ leftValue ->{$ k } : OBJECT_MERGE_UNDEFINED ,
203
200
$ rightDefined ? $ rightValue ->{$ k } : OBJECT_MERGE_UNDEFINED
@@ -209,19 +206,18 @@ private static function mergeObjectValues($recurse, $opts, $cb, $key, stdClass $
209
206
/**
210
207
* @param bool $recurse
211
208
* @param int $opts
212
- * @param callable $cb
213
209
* @param string|int $key
214
210
* @param mixed $leftValue
215
211
* @param mixed $rightValue
216
- * @return mixed
212
+ * @return array|stdClass
217
213
*/
218
- private static function mergeValues ($ recurse , $ opts , $ cb , $ key , $ leftValue , $ rightValue )
214
+ private static function mergeValues ($ recurse , $ opts , $ key , $ leftValue , $ rightValue )
219
215
{
220
216
$ leftUndefined = OBJECT_MERGE_UNDEFINED === $ leftValue ;
221
217
$ rightUndefined = OBJECT_MERGE_UNDEFINED === $ rightValue ;
222
218
223
219
if ($ leftUndefined && $ rightUndefined ) {
224
- throw new \ LogicException (
220
+ throw new LogicException (
225
221
sprintf (
226
222
'Both left and right values are "undefined": $recurse=%s; $opts=%d; $key=%s ' ,
227
223
$ recurse ? 'true ' : 'false ' ,
@@ -260,20 +256,19 @@ private static function mergeValues($recurse, $opts, $cb, $key, $leftValue, $rig
260
256
}
261
257
262
258
if (self ::ARRAY_T === $ leftType ) {
263
- return self ::mergeArrayValues ($ recurse , $ opts , $ cb , $ key , $ leftValue , $ rightValue );
259
+ return self ::mergeArrayValues ($ recurse , $ opts , $ leftValue , $ rightValue );
264
260
}
265
261
266
- return self ::mergeObjectValues ($ recurse , $ opts , $ cb , $ key , $ leftValue , $ rightValue );
262
+ return self ::mergeObjectValues ($ recurse , $ opts , $ leftValue , $ rightValue );
267
263
}
268
264
269
265
/**
270
266
* @param bool $recurse
271
267
* @param int $opts
272
- * @param callable $cb
273
268
* @param array $objects
274
269
* @return mixed|null
275
270
*/
276
- private static function doMerge ($ recurse , $ opts , $ cb , array $ objects )
271
+ private static function doMerge ($ recurse , $ opts , array $ objects )
277
272
{
278
273
if ([] === $ objects ) {
279
274
return null ;
@@ -291,7 +286,7 @@ private static function doMerge($recurse, $opts, $cb, array $objects)
291
286
continue ;
292
287
}
293
288
294
- $ root = self ::mergeObjectValues ($ recurse , $ opts , $ cb , null , $ root , $ object );
289
+ $ root = self ::mergeObjectValues ($ recurse , $ opts , $ root , $ object );
295
290
}
296
291
297
292
return $ root ;
0 commit comments