Skip to content

Commit e3ca7d3

Browse files
committed
make it possible to pass array of keys to get()
1 parent c744fe6 commit e3ca7d3

File tree

3 files changed

+149
-2
lines changed

3 files changed

+149
-2
lines changed

src/ArrayUtil.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ public static function get(array $data, $key, $default = null)
3737
return $data;
3838
}
3939

40+
if (is_array($key)) {
41+
return static::getArray($data, $key, $default);
42+
}
43+
4044
foreach (explode('.', $key) as $segment) {
4145
if (!is_array($data)) {
4246
return $default;
@@ -52,6 +56,17 @@ public static function get(array $data, $key, $default = null)
5256
return $data;
5357
}
5458

59+
protected static function getArray(array $input, $keys, $default = null)
60+
{
61+
$output = [];
62+
63+
foreach ($keys as $key) {
64+
static::set($output, $key, static::get($input, $key, $default));
65+
}
66+
67+
return $output;
68+
}
69+
5570
/**
5671
* Determine if an array has a given key.
5772
*

src/SettingStore.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ abstract class SettingStore
3535
/**
3636
* Get a specific key from the settings data.
3737
*
38-
* @param string $key
39-
* @param mixed $default Optional default value.
38+
* @param string|array $key
39+
* @param mixed $default Optional default value.
4040
*
4141
* @return mixed
4242
*/

tests/ArrayUtilTest.php

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
<?php
2+
3+
use anlutro\LaravelSettings\ArrayUtil;
4+
5+
class ArrayUtilTest extends PHPUnit_Framework_TestCase
6+
{
7+
/**
8+
* @test
9+
* @dataProvider getGetData
10+
*/
11+
public function getReturnsCorrectValue(array $data, $key, $expected)
12+
{
13+
$this->assertEquals($expected, ArrayUtil::get($data, $key));
14+
}
15+
16+
public function getGetData()
17+
{
18+
return [
19+
[[], 'foo', null],
20+
[['foo' => 'bar'], 'foo', 'bar'],
21+
[['foo' => 'bar'], 'bar', null],
22+
[['foo' => 'bar'], 'foo.bar', null],
23+
[['foo' => ['bar' => 'baz']], 'foo.bar', 'baz'],
24+
[['foo' => ['bar' => 'baz']], 'foo.baz', null],
25+
[['foo' => ['bar' => 'baz']], 'foo', ['bar' => 'baz']],
26+
[
27+
['foo' => 'bar', 'bar' => 'baz'],
28+
['foo', 'bar'],
29+
['foo' => 'bar', 'bar' => 'baz']
30+
],
31+
[
32+
['foo' => ['bar' => 'baz'], 'bar' => 'baz'],
33+
['foo.bar', 'bar'],
34+
['foo' => ['bar' => 'baz'], 'bar' => 'baz'],
35+
],
36+
[
37+
['foo' => ['bar' => 'baz'], 'bar' => 'baz'],
38+
['foo.bar'],
39+
['foo' => ['bar' => 'baz']],
40+
],
41+
[
42+
['foo' => ['bar' => 'baz'], 'bar' => 'baz'],
43+
['foo.bar', 'baz'],
44+
['foo' => ['bar' => 'baz'], 'baz' => null],
45+
],
46+
];
47+
}
48+
49+
/**
50+
* @test
51+
* @dataProvider getSetData
52+
*/
53+
public function setSetsCorrectKeyToValue(array $input, $key, $value, array $expected)
54+
{
55+
ArrayUtil::set($input, $key, $value);
56+
$this->assertEquals($expected, $input);
57+
}
58+
59+
public function getSetData()
60+
{
61+
return [
62+
[
63+
['foo' => 'bar'],
64+
'foo',
65+
'baz',
66+
['foo' => 'baz'],
67+
],
68+
[
69+
[],
70+
'foo',
71+
'bar',
72+
['foo' => 'bar'],
73+
],
74+
[
75+
[],
76+
'foo.bar',
77+
'baz',
78+
['foo' => ['bar' => 'baz']],
79+
],
80+
[
81+
['foo' => ['bar' => 'baz']],
82+
'foo.baz',
83+
'foo',
84+
['foo' => ['bar' => 'baz', 'baz' => 'foo']],
85+
],
86+
[
87+
['foo' => ['bar' => 'baz']],
88+
'foo.baz.bar',
89+
'baz',
90+
['foo' => ['bar' => 'baz', 'baz' => ['bar' => 'baz']]],
91+
],
92+
[
93+
[],
94+
'foo.bar.baz',
95+
'foo',
96+
['foo' => ['bar' => ['baz' => 'foo']]],
97+
],
98+
];
99+
}
100+
101+
/** @test */
102+
public function setThrowsExceptionOnNonArraySegment()
103+
{
104+
$data = ['foo' => 'bar'];
105+
$this->setExpectedException('UnexpectedValueException', 'Non-array segment encountered');
106+
ArrayUtil::set($data, 'foo.bar', 'baz');
107+
}
108+
109+
/**
110+
* @test
111+
* @dataProvider getHasData
112+
*/
113+
public function hasReturnsCorrectly(array $input, $key, $expected)
114+
{
115+
$this->assertEquals($expected, ArrayUtil::has($input, $key));
116+
}
117+
118+
public function getHasData()
119+
{
120+
return [
121+
[[], 'foo', false],
122+
[['foo' => 'bar'], 'foo', true],
123+
[['foo' => 'bar'], 'bar', false],
124+
[['foo' => 'bar'], 'foo.bar', false],
125+
[['foo' => ['bar' => 'baz']], 'foo.bar', true],
126+
[['foo' => ['bar' => 'baz']], 'foo.baz', false],
127+
[['foo' => ['bar' => 'baz']], 'foo', true],
128+
[['foo' => null], 'foo', true],
129+
[['foo' => ['bar' => null]], 'foo.bar', true],
130+
];
131+
}
132+
}

0 commit comments

Comments
 (0)