Skip to content

Commit

Permalink
Update functions to be namespaced (supported in PHP 5.6+)
Browse files Browse the repository at this point in the history
  • Loading branch information
wpscholar committed Jan 6, 2022
1 parent 59e97ee commit 28ebc09
Showing 1 changed file with 65 additions and 72 deletions.
137 changes: 65 additions & 72 deletions includes/functions.php
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;
}

0 comments on commit 28ebc09

Please sign in to comment.