Skip to content

Commit 07495db

Browse files
committed
Init
1 parent 1f0503c commit 07495db

File tree

2 files changed

+37
-23
lines changed

2 files changed

+37
-23
lines changed

src/ArrayReader.php

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Selective\ArrayReader;
44

5+
use Cake\Chronos\Chronos;
56
use Exception;
67
use InvalidArgumentException;
78

@@ -269,23 +270,18 @@ public function findChronos(string $key, Chronos $default = null)
269270
*/
270271
public function find(string $path, $default = null)
271272
{
272-
if (strpos($path, '.') === false) {
273-
return $this->data[$path] ?? $default;
274-
}
273+
$pathKeys = explode('.', $path);
275274

276-
$parts = explode('.', $path);
277-
$value = &$this->data;
275+
$arrayCopyOrValue = $this->data;
278276

279-
foreach ($parts as $key) {
280-
if (isset($value[$key])) {
281-
$value = &$value[$key];
282-
} else {
277+
foreach ($pathKeys as $pathKey) {
278+
if (!isset($arrayCopyOrValue[$pathKey])) {
283279
return $default;
284280
}
281+
$arrayCopyOrValue = $arrayCopyOrValue[$pathKey];
285282
}
286-
$value = &$this->$value;
287283

288-
return $value;
284+
return $arrayCopyOrValue;
289285
}
290286

291287
/**
@@ -311,19 +307,15 @@ public function all(): array
311307
*/
312308
public function exists(string $path): bool
313309
{
314-
if (strpos($path, '.') === false) {
315-
return array_key_exists($path, $this->data);
316-
}
310+
$pathKeys = explode('.', $path);
317311

318-
$parts = explode('.', $path);
312+
$arrayCopyOrValue = $this->data;
319313

320-
$temp = &$this->data;
321-
foreach ($parts as $key) {
322-
if (array_key_exists($key, $temp)) {
323-
$temp = &$this->data[$key];
324-
} else {
314+
foreach ($pathKeys as $pathKey) {
315+
if (!array_key_exists($pathKey, $arrayCopyOrValue)) {
325316
return false;
326317
}
318+
$arrayCopyOrValue = $arrayCopyOrValue[$pathKey];
327319
}
328320

329321
return true;

tests/ArrayReaderTest.php

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,33 @@ class ArrayReaderTest extends TestCase
1313
/**
1414
* Test.
1515
*
16+
* @dataProvider providerGetString
17+
*
18+
* @param mixed $data The data
19+
* @param string $key The lookup key
20+
* @param mixed $default The default value
21+
* @param mixed $expected The expected value
22+
*
1623
* @return void
1724
*/
18-
public function testGetString()
25+
public function testGetString($data, string $key, $default, $expected)
26+
{
27+
$reader = new ArrayReader($data);
28+
static::assertSame($expected, $reader->getString($key, $default));
29+
}
30+
31+
/**
32+
* Provider.
33+
*
34+
* @return array[] The test data
35+
*/
36+
public function providerGetString(): array
1937
{
20-
$reader = new ArrayReader(['key' => 'value']);
21-
static::assertSame('value', $reader->getString('key'));
38+
return [
39+
[['key' => 'value'], 'key', null, 'value'],
40+
[['key' => 'value'], 'nope', 'default', 'default'],
41+
[['key' => ['key2' => 'value']], 'key.key2', null, 'value'],
42+
[['key' => ['key2' => 'value']], 'key.nope', 'default', 'default'],
43+
];
2244
}
2345
}

0 commit comments

Comments
 (0)