function test_value() { $this->assertEquals('foo', wpdevsclub_value('foo')); $this->assertEquals('foo', wpdevsclub_value(function () { return 'foo'; })); }
/** * Get() function callback for key dot notation walker * * @since 1.0.0 * * @param array $array * @param string $key * @param bool $break * @param array $args * @return bool */ protected static function callback_get(array &$array, $key, &$break = false, $args = array()) { if (!is_array($array) || !array_key_exists($key, $array)) { $break = true; return wpdevsclub_value($args['default']); } $array = $array[$key]; return $array; }
/** * Get an item from an array or object using "dot" notation. * * @param mixed $target * @param string $key * @param mixed $default * @return mixed */ function wpdevsclub_data_get($target, $key, $default = null) { if (is_null($key)) { return $target; } foreach (explode('.', $key) as $segment) { if (is_array($target)) { if (!array_key_exists($segment, $target)) { return wpdevsclub_value($default); } $target = $target[$segment]; } elseif ($target instanceof ArrayAccess) { if (!isset($target[$segment])) { return wpdevsclub_value($default); } $target = $target[$segment]; } elseif (is_object($target)) { if (!isset($target->{$segment})) { return wpdevsclub_value($default); } $target = $target->{$segment}; } else { return wpdevsclub_value($default); } } return $target; }