Skip to content

Commit

Permalink
Fixed returning default value with one array item
Browse files Browse the repository at this point in the history
  • Loading branch information
roquie committed Jan 20, 2023
1 parent 3610636 commit 3fc74de
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 12 deletions.
33 changes: 21 additions & 12 deletions src/Dot.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

/** @noinspection PhpFullyQualifiedNameUsageInspection */

declare(strict_types=1);

namespace Spacetab\Obelix;
Expand Down Expand Up @@ -32,7 +34,7 @@ final class Dot
*/
public function __construct($items = [])
{
if (is_array($items)) {
if (\is_array($items)) {
$this->items = $items;
} elseif (/** @infection-ignore-all */ $items instanceof self) {
$this->items = $items->toArray();
Expand Down Expand Up @@ -83,8 +85,8 @@ public function get(string $path, $default = null): ResultSet
$pathway = [];
$flatArray = null;

$segments = explode($this->delimiter, $path);
$countSegments = count($segments);
$segments = \explode($this->delimiter, $path);
$countSegments = \count($segments);

$it = new RecursiveIteratorIterator(new RecursiveArrayIterator($this->items), RecursiveIteratorIterator::SELF_FIRST);

Expand All @@ -97,22 +99,29 @@ public function get(string $path, $default = null): ResultSet

if ($this->isUserPathEqualsRealPath($segments, $pathway)) {
$flatArray[
implode($this->delimiter, array_slice($pathway, 0, $it->getDepth() + 1))
\implode($this->delimiter, \array_slice($pathway, 0, $it->getDepth() + 1))
] = $value;
}
}

$value = $flatArray === null ? $default : array_values($flatArray);
$flatArray = $flatArray ?? [$path => $default];
if ($flatArray === null) {
$map = [$path => $default];

// @infection-ignore-all
self::$cache[$path] = [$default, $map];

return new ResultSet($default, $map);
}

$val = \array_values($flatArray);

if (is_countable($value) && count($value) === 1) {
// @phpstan-ignore-next-line
$value = $value[0];
if (\is_countable($val) && \count($val) === 1) {
$val = $val[0];
}

self::$cache[$path] = [$value, $flatArray];
self::$cache[$path] = [$val, $flatArray];

return new ResultSet($value, $flatArray);
return new ResultSet($val, $flatArray);
}

/**
Expand All @@ -135,7 +144,7 @@ private function isUserPathEqualsRealPath(array $user, array $real): bool
$val = $real[$i] ?? false;

// to work with integer indexes in string path (for cases like "foo.0")
if (ctype_digit($item)) {
if (\ctype_digit($item)) {
$item = (int) $item;
}

Expand Down
16 changes: 16 additions & 0 deletions tests/DotTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,22 @@ public function testGetItemsFromSimpleArrayAndSingleValue(): void
], $result->getMap());
}

public function testGetDefaultValueWhenValueIsEqualsSingleItemSlice(): void
{
$array = [
'foo' => [
'bar' => [
'baz' => 1
]
]
];

$dot = new Obelix\Dot($array);
$result = $dot->get('test.path', ['defaultValue']);

$this->assertSame(['defaultValue'], $result->getValue());
}

public function testGetItemsFromSimpleArrayAndItReturnsCorrectAssociativeArray(): void
{
$array = [
Expand Down

0 comments on commit 3fc74de

Please sign in to comment.