69 lines
2.1 KiB
PHP
69 lines
2.1 KiB
PHP
<?php
|
|
namespace Grav\Common\Data;
|
|
|
|
trait DataMutatorTrait
|
|
{
|
|
|
|
/**
|
|
* Get value by using dot notation for nested arrays/objects.
|
|
*
|
|
* @example $value = $data->get('this.is.my.nested.variable');
|
|
*
|
|
* @param string $name Dot separated path to the requested value.
|
|
* @param mixed $default Default value (or null).
|
|
* @param string $separator Separator, defaults to '.'
|
|
* @return mixed Value.
|
|
*/
|
|
public function get($name, $default = null, $separator = '.')
|
|
{
|
|
$path = explode($separator, $name);
|
|
$current = $this->items;
|
|
foreach ($path as $field) {
|
|
if (is_object($current) && isset($current->{$field})) {
|
|
$current = $current->{$field};
|
|
} elseif (is_array($current) && isset($current[$field])) {
|
|
$current = $current[$field];
|
|
} else {
|
|
return $default;
|
|
}
|
|
}
|
|
|
|
return $current;
|
|
}
|
|
|
|
/**
|
|
* Set value by using dot notation for nested arrays/objects.
|
|
*
|
|
* @example $value = $data->set('this.is.my.nested.variable', true);
|
|
*
|
|
* @param string $name Dot separated path to the requested value.
|
|
* @param mixed $value New value.
|
|
* @param string $separator Separator, defaults to '.'
|
|
*/
|
|
public function set($name, $value, $separator = '.')
|
|
{
|
|
$path = explode($separator, $name);
|
|
$current = &$this->items;
|
|
foreach ($path as $field) {
|
|
if (is_object($current)) {
|
|
// Handle objects.
|
|
if (!isset($current->{$field})) {
|
|
$current->{$field} = array();
|
|
}
|
|
$current = &$current->{$field};
|
|
} else {
|
|
// Handle arrays and scalars.
|
|
if (!is_array($current)) {
|
|
$current = array($field => array());
|
|
} elseif (!isset($current[$field])) {
|
|
$current[$field] = array();
|
|
}
|
|
$current = &$current[$field];
|
|
}
|
|
}
|
|
|
|
$current = $value;
|
|
}
|
|
|
|
}
|