Skip to content

Commit

Permalink
Add preverse return type
Browse files Browse the repository at this point in the history
  • Loading branch information
23r9i0 committed Sep 16, 2016
1 parent 69626b2 commit 5899810
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 9 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

Like wp_parse_args but supports recursivity.

Additionally converts the returned type based on the $args and $defaults
By default converts the returned type based on the $args and $defaults
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "23r9i0/wp_parse_args_recursive",
"description": "Like wp_parse_args but supports recursivity. Additionally converts the returned type based on the $args and $defaults",
"description": "Like wp_parse_args but supports recursivity. By default converts the returned type based on the $args and $defaults",
"type": "library",
"homepage": "https://github.com/23r9i0/wp_parse_args_recursive",
"license": "GPL-2.0+",
Expand Down
11 changes: 4 additions & 7 deletions src/wp_parse_args_recursive.php
Original file line number Diff line number Diff line change
@@ -1,25 +1,22 @@
<?php
/**
* Like wp_parse_args but supports recursivity
* Additionally converts the returned type based on the $args and $defaults
* By default converts the returned type based on the $args and $defaults
*
* Inspired by NestedArray::mergeDeepArray ( Drupal )
*
* @author Drupal
* @author Sergio P.A. (23r9i0) <[email protected]>
*
* @license https://www.gnu.org/licenses/gpl-2.0.html GPL2 or later
*
* @version 1.0
*
* @param array|object $args Values to merge with $defaults
* @param array|object $defaults Array or Object that serves as the defaults.
* @param boolean $preserve_type Optional. Convert output array into object if $args or $defaults if it is. Default true
* @param boolean $preserve_integer_keys Optional. If given, integer keys will be preserved and merged instead of appended. Default false.
*
* @return array|object $output Merged user defined values with defaults.
*/
if ( ! function_exists( 'wp_parse_args_recursive' ) ) {
function wp_parse_args_recursive( $args, $defaults, $preserve_integer_keys = false ) {
function wp_parse_args_recursive( $args, $defaults, $preserve_type = true, $preserve_integer_keys = false ) {
$output = array();

foreach ( array( $defaults, $args ) as $list ) {
Expand All @@ -38,6 +35,6 @@ function wp_parse_args_recursive( $args, $defaults, $preserve_integer_keys = fal
}
}

return ( is_object( $args ) || is_object( $defaults ) ) ? (object) $output : $output;
return ( $preserve_type && ( is_object( $args ) || is_object( $defaults ) ) ) ? (object) $output : $output;
}
}

0 comments on commit 5899810

Please sign in to comment.