代码同步
This commit is contained in:
@@ -29,14 +29,25 @@ class Dot implements ArrayAccess, Countable, IteratorAggregate, JsonSerializable
|
||||
*/
|
||||
protected $items = [];
|
||||
|
||||
|
||||
/**
|
||||
* The delimiter (alternative to a '.') to be used.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $delimiter = '.';
|
||||
|
||||
|
||||
/**
|
||||
* Create a new Dot instance
|
||||
*
|
||||
* @param mixed $items
|
||||
* @param string $delimiter
|
||||
*/
|
||||
public function __construct($items = [])
|
||||
public function __construct($items = [], $delimiter = '.')
|
||||
{
|
||||
$this->items = $this->getArrayItems($items);
|
||||
$this->delimiter = strlen($delimiter) ? $delimiter : '.';
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -104,7 +115,7 @@ class Dot implements ArrayAccess, Countable, IteratorAggregate, JsonSerializable
|
||||
}
|
||||
|
||||
$items = &$this->items;
|
||||
$segments = explode('.', $key);
|
||||
$segments = explode($this->delimiter, $key);
|
||||
$lastSegment = array_pop($segments);
|
||||
|
||||
foreach ($segments as $segment) {
|
||||
@@ -148,6 +159,10 @@ class Dot implements ArrayAccess, Countable, IteratorAggregate, JsonSerializable
|
||||
$items = $this->items;
|
||||
}
|
||||
|
||||
if (!func_num_args()) {
|
||||
$delimiter = $this->delimiter;
|
||||
}
|
||||
|
||||
foreach ($items as $key => $value) {
|
||||
if (is_array($value) && !empty($value)) {
|
||||
$flatten = array_merge(
|
||||
@@ -179,13 +194,13 @@ class Dot implements ArrayAccess, Countable, IteratorAggregate, JsonSerializable
|
||||
return $this->items[$key];
|
||||
}
|
||||
|
||||
if (strpos($key, '.') === false) {
|
||||
if (strpos($key, $this->delimiter) === false) {
|
||||
return $default;
|
||||
}
|
||||
|
||||
$items = $this->items;
|
||||
|
||||
foreach (explode('.', $key) as $segment) {
|
||||
foreach (explode($this->delimiter, $key) as $segment) {
|
||||
if (!is_array($items) || !$this->exists($items, $segment)) {
|
||||
return $default;
|
||||
}
|
||||
@@ -234,7 +249,7 @@ class Dot implements ArrayAccess, Countable, IteratorAggregate, JsonSerializable
|
||||
continue;
|
||||
}
|
||||
|
||||
foreach (explode('.', $key) as $segment) {
|
||||
foreach (explode($this->delimiter, $key) as $segment) {
|
||||
if (!is_array($items) || !$this->exists($items, $segment)) {
|
||||
return false;
|
||||
}
|
||||
@@ -446,7 +461,7 @@ class Dot implements ArrayAccess, Countable, IteratorAggregate, JsonSerializable
|
||||
|
||||
$items = &$this->items;
|
||||
|
||||
foreach (explode('.', $keys) as $key) {
|
||||
foreach (explode($this->delimiter, $keys) as $key) {
|
||||
if (!isset($items[$key]) || !is_array($items[$key])) {
|
||||
$items[$key] = [];
|
||||
}
|
||||
@@ -600,6 +615,7 @@ class Dot implements ArrayAccess, Countable, IteratorAggregate, JsonSerializable
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
#[\ReturnTypeWillChange]
|
||||
public function jsonSerialize()
|
||||
{
|
||||
return $this->items;
|
||||
|
||||
@@ -11,13 +11,14 @@ use Adbar\Dot;
|
||||
|
||||
if (! function_exists('dot')) {
|
||||
/**
|
||||
* Create a new Dot object with the given items
|
||||
* Create a new Dot object with the given items and optional delimiter
|
||||
*
|
||||
* @param mixed $items
|
||||
* @param mixed $items
|
||||
* @param string $delimiter
|
||||
* @return \Adbar\Dot
|
||||
*/
|
||||
function dot($items)
|
||||
function dot($items, $delimiter = '.')
|
||||
{
|
||||
return new Dot($items);
|
||||
return new Dot($items, $delimiter);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user