Skip to content

Commit c654963

Browse files
committed
improved recursion and updated api
1 parent 5273a8d commit c654963

File tree

6 files changed

+318
-141
lines changed

6 files changed

+318
-141
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class stdClass#56 (1) {
5757

5858
## Merge Options
5959
The `object_merge` and `object_merge_recursive` functions have sister functions named `object_merge_opts` and
60-
`object_merge_recursive_opts` respectively. Each of these requires a 2nd `$opts` argument that must be a bitwise
60+
`object_merge_recursive_opts` respectively. Each of these has a required `$opts` argument that must be a bitwise
6161
inclusive or of your desired options.
6262

6363
#### `OBJECT_MERGE_OPT_CONFLICT_OVERWRITE`
@@ -95,7 +95,7 @@ $o1 = json_decode('{"key":'.PHP_INT_MAX.'}');
9595
$o2 = json_decode('{"key":true');
9696
$o3 = json_decode('{"key":"not a number"}');
9797

98-
$out = object_merge_opts($o1, OBJECT_MERGE_OPT_CONFLICT_EXCEPTION, $o2, $o3);
98+
$out = object_merge_opts(OBJECT_MERGE_OPT_CONFLICT_EXCEPTION, $o1, $o2, $o3);
9999

100100
// UnexpectedValueException thrown
101101
```
@@ -115,7 +115,7 @@ $o1 = json_decode('{"key":["one"]}');
115115
$o2 = json_decode('{"key":["one","two"]}');
116116
$o3 = json_decode('{"key":["one","two","three"]}');
117117

118-
$out = object_merge_recursive_opts($o1, OBJECT_MERGE_OPT_UNIQUE_ARRAYS, $o2, $o3);
118+
$out = object_merge_recursive_opts(OBJECT_MERGE_OPT_UNIQUE_ARRAYS, $o1, $o2, $o3);
119119

120120
var_dump($out);
121121

files/constants.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,5 @@
1919
const OBJECT_MERGE_OPT_CONFLICT_OVERWRITE = 0x0;
2020
const OBJECT_MERGE_OPT_CONFLICT_EXCEPTION = 0x1;
2121
const OBJECT_MERGE_OPT_UNIQUE_ARRAYS = 0x2;
22+
23+
define('OBJECT_MERGE_UNDEFINED', uniqid('__OBJECT_MERGE_UNDEFINED_'));

files/functions.php

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,49 +18,56 @@
1818

1919
use DCarbone\ObjectMerge;
2020

21+
if (!function_exists('object_merge_value_undefined')) {
22+
/**
23+
* @param mixed $value
24+
* @return bool
25+
*/
26+
function object_merge_value_undefined($value)
27+
{
28+
return $value === OBJECT_MERGE_UNDEFINED;
29+
}
30+
}
31+
2132
if (!function_exists('object_merge')) {
2233
/**
23-
* @param stdClass $root
24-
* @param stdClass ...$others
34+
* @param stdClass ...$objects
2535
* @return stdClass
2636
*/
27-
function object_merge(stdClass $root, stdClass ...$others)
37+
function object_merge(stdClass ...$objects)
2838
{
29-
return ObjectMerge::merge($root, ...$others);
39+
return ObjectMerge::merge($objects);
3040
}
3141
}
32-
if (!function_exists('object_merge_opts')) {
42+
if (!function_exists('object_merge_recursive')) {
3343
/**
34-
* @param stdClass $root
35-
* @param int $opts
36-
* @param stdClass ...$others
44+
* @param stdClass ...$objects
3745
* @return stdClass
3846
*/
39-
function object_merge_opts(stdClass $root, $opts, stdClass ...$others)
47+
function object_merge_recursive(stdClass ...$objects)
4048
{
41-
return ObjectMerge::mergeOpts($root, $opts, ...$others);
49+
return ObjectMerge::mergeRecursive($objects);
4250
}
4351
}
44-
if (!function_exists('object_merge_recursive')) {
52+
if (!function_exists('object_merge_opts')) {
4553
/**
46-
* @param stdClass $root
47-
* @param stdClass ...$others
54+
* @param int $opts
55+
* @param stdClass ...$objects
4856
* @return stdClass
4957
*/
50-
function object_merge_recursive(stdClass $root, stdClass ...$others)
58+
function object_merge_opts($opts, stdClass ...$objects)
5159
{
52-
return ObjectMerge::mergeRecursive($root, ...$others);
60+
return ObjectMerge::mergeOpts($opts, $objects);
5361
}
5462
}
5563
if (!function_exists('object_merge_recursive_opts')) {
5664
/**
57-
* @param stdClass $root
5865
* @param int $opts
59-
* @param stdClass ...$others
66+
* @param stdClass ...$objects
6067
* @return stdClass
6168
*/
62-
function object_merge_recursive_opts(stdClass $root, $opts, stdClass ...$others)
69+
function object_merge_recursive_opts($opts, stdClass ...$objects)
6370
{
64-
return ObjectMerge::mergeRecursiveOpts($root, $opts, ...$others);
71+
return ObjectMerge::mergeRecursiveOpts($opts, $objects);
6572
}
6673
}

0 commit comments

Comments
 (0)