Skip to content

Commit caa0b45

Browse files
committed
Add find method
The `find` method accepts an array of keys, and iterates over the returning the value of the first key that matches. This allows for some itneresting behavior, such as defacto aliases for values or setting built-in defaults.
1 parent 081f353 commit caa0b45

File tree

3 files changed

+79
-3
lines changed

3 files changed

+79
-3
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,4 @@ If you're interested in the history and motivation of this project, it's an atte
2020
- Some way to define some properties about arguments:
2121
- Argument order on output--necessary for methods like `pass` to work, possibly for other reasons.
2222
- Expected arguments--again necessary for `pass`, but likely useful in other contexts too.
23+
- Allow for "fallback" keys when using `find` method.

src/Brief.php

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ protected static function normalizeInput($items)
3737
return get_object_vars($items);
3838
}
3939

40-
if (null === $items || is_bool($items)) {
41-
return [];
42-
}
40+
if (null === $items || is_bool($items)) {
41+
return [];
42+
}
4343

4444
if ( ! is_array($items)) {
4545
throw new WrongArgumentTypeException("Did not pass array or iterable object.");
@@ -322,4 +322,33 @@ public function passArray(callable $callable, $keyed = true)
322322

323323
return call_user_func($callable, $arg);
324324
}
325+
326+
/**
327+
* Pass an array of keys, and return the value for the first one that matches.
328+
*
329+
* @param array $keys
330+
*
331+
* @return bool
332+
*/
333+
public function find($keys)
334+
{
335+
// Be friendly
336+
if (is_string($keys)) {
337+
return $this->getArgument($keys);
338+
}
339+
340+
// ...Otherwise, it has to be an array
341+
if ( ! is_array($keys)) {
342+
return false;
343+
}
344+
345+
// Prevent infinite recursion
346+
if (empty($keys)) {
347+
return false;
348+
}
349+
350+
$get = array_shift($keys);
351+
352+
return $this->getArgument($get) ?: $this->find($keys);
353+
}
325354
}

tests/BriefTest.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,4 +155,50 @@ public function testIfItemIsSetInBrief(): void
155155
$this->assertTrue(isset($Brief->key1));
156156
$this->assertFalse(isset($Brief->key3));
157157
}
158+
159+
public function testCanFindSingleKey(): void
160+
{
161+
$Brief = Brief::make([
162+
'key1' => 'value1',
163+
'key2' => 'value2',
164+
]);
165+
$this->assertEquals('value1', $Brief->find(['key1']));
166+
}
167+
168+
public function testCanFindUnderstandString(): void
169+
{
170+
$Brief = Brief::make([
171+
'key1' => 'value1',
172+
'key2' => 'value2',
173+
]);
174+
$this->assertEquals('value1', $Brief->find('key1'));
175+
}
176+
177+
public function testFindReturnsFalseIfNotGivenAnArrayOrString(): void
178+
{
179+
$Brief = Brief::make([
180+
'key1' => 'value1',
181+
'key2' => 'value2',
182+
]);
183+
$this->assertFalse($Brief->find(2));
184+
$this->assertFalse($Brief->find(new Brief([])));
185+
}
186+
187+
public function testCanFindFallbackKey(): void
188+
{
189+
$Brief = Brief::make([
190+
'key1' => 'value1',
191+
'key2' => 'value2',
192+
]);
193+
$this->assertEquals('value2', $Brief->find(['nonexistant', 'key2']));
194+
}
195+
196+
public function testReturnFalseIfCannotFindAnyKey(): void
197+
{
198+
$Brief = Brief::make([
199+
'key1' => 'value1',
200+
'key2' => 'value2',
201+
]);
202+
$this->assertFalse($Brief->find(['a', 'b', 'c', 'd']));
203+
}
158204
}

0 commit comments

Comments
 (0)