Skip to content

Commit

Permalink
Added Arr::locate and ArrDots::locate
Browse files Browse the repository at this point in the history
  • Loading branch information
pdscopes committed May 11, 2018
1 parent f10278e commit 2b16ac0
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 1 deletion.
30 changes: 30 additions & 0 deletions src/Arr.php
Original file line number Diff line number Diff line change
Expand Up @@ -242,4 +242,34 @@ public static function find(array $haystack, $needle, $strict = false)
return $key !== false ? $haystack[$key] : null;
}
}

/**
* Given a multi-dimensional array, return the first item that has a property $property
* with value $value.
* An array of properties can be provided to perform deeper finds.
*
* @param array $array
* @param string|array $property
* @param mixed $value
* @param bool $strict
* @return mixed|null
*/
public static function locate($array, $property, $value, $strict = false)
{
$array = $array ?? [];
$columns = (array) $property;
$property = array_pop($columns);
if (!empty($columns)) {
$array = static::column($array, $columns);
}
foreach ($array as $item) {
if (!isset($item[$property])) {
continue;
}
if ((!$strict && $item[$property] == $value) || ($strict && $item[$property] === $value)) {
return $item;
}
}
return null;
}
}
18 changes: 18 additions & 0 deletions src/ArrDots.php
Original file line number Diff line number Diff line change
Expand Up @@ -308,4 +308,22 @@ public static function only($array, $keys)
$only = Arr::only($imploded, $keys);
return static::explode($only);
}

/**
* Given a multi-dimensional array, return the first item that has a property $property
* with value $value.
* An array of properties can be provided to perform deeper finds.
*
* @param array $array
* @param string $dots
* @param mixed $value
* @param bool $strict
* @return mixed|null
*
* @see \MadeSimple\Arrays\Arr::locate()
*/
public static function locate($array, $dots, $value, $strict = false)
{
return Arr::locate($array, explode('.', $dots), $value, $strict);
}
}
22 changes: 22 additions & 0 deletions tests/Unit/ArrDotsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -265,4 +265,26 @@ public function testOnly()

$this->assertEquals($partialArray, ArrDots::only($completeArray, ['two', 'deep.beta']));
}

public function testLocate()
{
$shallow = [
['locator' => '1a', 'description' => 'alpha'],
['locator' => '2b', 'description' => 'beta'],
['locator' => '3c', 'description' => 'gamma'],
];
$deep = [
['sub' => ['locator' => '1a', 'description' => 'alpha']],
['sub' => ['locator' => '2b', 'description' => 'beta']],
['sub' => ['locator' => '3c', 'description' => 'gamma']],
];

$this->assertEquals('alpha', ArrDots::locate($shallow, 'locator', '1a')['description']);
$this->assertEquals('beta', ArrDots::locate($shallow, 'locator', '2b')['description']);
$this->assertEquals('gamma', ArrDots::locate($shallow, 'locator', '3c')['description']);

$this->assertEquals('alpha', ArrDots::locate($deep, 'sub.locator', '1a')['description']);
$this->assertEquals('beta', ArrDots::locate($deep, 'sub.locator', '2b')['description']);
$this->assertEquals('gamma', ArrDots::locate($deep, 'sub.locator', '3c')['description']);
}
}
21 changes: 20 additions & 1 deletion tests/Unit/ArrTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,25 @@ public function testFindCallable()
$this->assertEquals('value 2', Arr::find($array, function ($item, $key) { return $key === 'two'; }));
$this->assertEquals('value 3', Arr::find($array, function ($item, $key) { return $key === 'three'; }));

$this->assertNull(Arr::find($array, function ($item, $key) { return $key === 'four'; }));
public function testLocate()
{
$shallow = [
['locator' => '1a', 'description' => 'alpha'],
['locator' => '2b', 'description' => 'beta'],
['locator' => '3c', 'description' => 'gamma'],
];
$deep = [
['sub' => ['locator' => '1a', 'description' => 'alpha']],
['sub' => ['locator' => '2b', 'description' => 'beta']],
['sub' => ['locator' => '3c', 'description' => 'gamma']],
];

$this->assertEquals('alpha', Arr::locate($shallow, 'locator', '1a')['description']);
$this->assertEquals('beta', Arr::locate($shallow, 'locator', '2b')['description']);
$this->assertEquals('gamma', Arr::locate($shallow, 'locator', '3c')['description']);

$this->assertEquals('alpha', Arr::locate($deep, ['sub', 'locator'], '1a')['description']);
$this->assertEquals('beta', Arr::locate($deep, ['sub', 'locator'], '2b')['description']);
$this->assertEquals('gamma', Arr::locate($deep, ['sub', 'locator'], '3c')['description']);
}
}

0 comments on commit 2b16ac0

Please sign in to comment.