-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update functions to be namespaced (supported in PHP 5.6+)
- Loading branch information
Showing
1 changed file
with
65 additions
and
72 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,84 +1,77 @@ | ||
<?php | ||
|
||
use WP_Forge\Helpers\Arr; | ||
namespace WP_Forge\Helpers; | ||
|
||
if (!function_exists('data_get')) { | ||
|
||
/** | ||
* Get an item from an array or object using dot notation. | ||
* | ||
* @param mixed $data | ||
* @param string|array $key | ||
* @param mixed $default | ||
* | ||
* @return mixed | ||
*/ | ||
function data_get($data, $key, $default = null) { | ||
$segments = is_array($key) ? $key : explode('.', $key); | ||
foreach ($segments as $segment) { | ||
if (is_null($segment)) { | ||
return $default; | ||
} | ||
if (Arr::accessible($data) && Arr::exists($data, $segment)) { | ||
$data = $data[$segment]; | ||
} elseif (is_object($data) && isset($data->{$segment})) { | ||
$data = $data->{$segment}; | ||
} else { | ||
return $default; | ||
} | ||
} | ||
|
||
return $data; | ||
} | ||
/** | ||
* Get an item from an array or object using dot notation. | ||
* | ||
* @param mixed $data | ||
* @param string|array $key | ||
* @param mixed $default | ||
* | ||
* @return mixed | ||
*/ | ||
function dataGet( $data, $key, $default = null ) { | ||
$segments = is_array( $key ) ? $key : explode( '.', $key ); | ||
foreach ( $segments as $segment ) { | ||
if ( is_null( $segment ) ) { | ||
return $default; | ||
} | ||
if ( Arr::accessible( $data ) && Arr::exists( $data, $segment ) ) { | ||
$data = $data[ $segment ]; | ||
} elseif ( is_object( $data ) && isset( $data->{$segment} ) ) { | ||
$data = $data->{$segment}; | ||
} else { | ||
return $default; | ||
} | ||
} | ||
|
||
return $data; | ||
} | ||
|
||
if (!function_exists('data_set')) { | ||
|
||
/** | ||
* Set an item on an array or object using dot notation. | ||
* | ||
* @param mixed $target | ||
* @param string|array $key | ||
* @param mixed $value | ||
* @param bool $overwrite | ||
* @return mixed | ||
*/ | ||
function data_set(&$target, $key, $value, $overwrite = true) { | ||
$segments = is_array($key) ? $key : explode('.', $key); | ||
$segment = array_shift($segments); | ||
|
||
if (Arr::accessible($target)) { | ||
if ($segments) { | ||
if (!Arr::exists($target, $segment)) { | ||
$target[$segment] = []; | ||
} | ||
/** | ||
* Set an item on an array or object using dot notation. | ||
* | ||
* @param mixed $target | ||
* @param string|array $key | ||
* @param mixed $value | ||
* @param bool $overwrite | ||
* | ||
* @return mixed | ||
*/ | ||
function dataSet( &$target, $key, $value, $overwrite = true ) { | ||
$segments = is_array( $key ) ? $key : explode( '.', $key ); | ||
$segment = array_shift( $segments ); | ||
|
||
data_set($target[$segment], $segments, $value, $overwrite); | ||
} elseif ($overwrite || !Arr::exists($target, $segment)) { | ||
$target[$segment] = $value; | ||
} | ||
} elseif (is_object($target)) { | ||
if ($segments) { | ||
if (!isset($target->{$segment})) { | ||
$target->{$segment} = []; | ||
} | ||
if ( Arr::accessible( $target ) ) { | ||
if ( $segments ) { | ||
if ( ! Arr::exists( $target, $segment ) ) { | ||
$target[ $segment ] = []; | ||
} | ||
|
||
data_set($target->{$segment}, $segments, $value, $overwrite); | ||
} elseif ($overwrite || !isset($target->{$segment})) { | ||
$target->{$segment} = $value; | ||
} | ||
} else { | ||
$target = []; | ||
dataSet( $target[ $segment ], $segments, $value, $overwrite ); | ||
} elseif ( $overwrite || ! Arr::exists( $target, $segment ) ) { | ||
$target[ $segment ] = $value; | ||
} | ||
} elseif ( is_object( $target ) ) { | ||
if ( $segments ) { | ||
if ( ! isset( $target->{$segment} ) ) { | ||
$target->{$segment} = []; | ||
} | ||
|
||
if ($segments) { | ||
data_set($target[$segment], $segments, $value, $overwrite); | ||
} elseif ($overwrite) { | ||
$target[$segment] = $value; | ||
} | ||
} | ||
dataSet( $target->{$segment}, $segments, $value, $overwrite ); | ||
} elseif ( $overwrite || ! isset( $target->{$segment} ) ) { | ||
$target->{$segment} = $value; | ||
} | ||
} else { | ||
$target = []; | ||
|
||
return $target; | ||
} | ||
if ( $segments ) { | ||
dataSet( $target[ $segment ], $segments, $value, $overwrite ); | ||
} elseif ( $overwrite ) { | ||
$target[ $segment ] = $value; | ||
} | ||
} | ||
|
||
return $target; | ||
} |