-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #32 from wp-graphql/fix/lodash-get-restored
fix: lodashGet access restored
- Loading branch information
Showing
3 changed files
with
62 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
/** | ||
* Utils class | ||
* | ||
* @since TBD | ||
* @package Tests\WPGraphQL\Utils | ||
*/ | ||
|
||
namespace Tests\WPGraphQL\Utils; | ||
|
||
class Utils { | ||
/** | ||
* The value returned for undefined resolved values. | ||
* | ||
* Clone of the "get" function from the Lodash JS libra | ||
* | ||
* @param array $object The object to query. | ||
* @param string $path The path of the property to get. | ||
* @param mixed $default The value returned for undefined resolved values. | ||
* @return void | ||
*/ | ||
public static function lodashGet( array $data, string $string, $default = null ) { | ||
$arrStr = explode( '.', $string ); | ||
if ( ! is_array( $arrStr ) ) { | ||
$arrStr = [ $arrStr ]; | ||
} | ||
|
||
$result = $data; | ||
foreach ( $arrStr as $lvl ) { | ||
if ( ! is_null( $lvl ) && isset( $result[ $lvl ] ) ) { | ||
$result = $result[ $lvl ]; | ||
} else { | ||
$result = $default; | ||
} | ||
} | ||
|
||
return $result; | ||
} | ||
} |