-
-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ff7269c
commit 5ccbbc3
Showing
8 changed files
with
98 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace ElasticAdapter\Support; | ||
|
||
final class Arr | ||
{ | ||
/** | ||
* Get an item from an array using "dot" notation. | ||
* | ||
* @return mixed | ||
*/ | ||
public static function get(array $array, string $key) | ||
{ | ||
if (isset($array[$key])) { | ||
return $array[$key]; | ||
} | ||
|
||
foreach (explode('.', $key) as $segment) { | ||
if (!array_key_exists($segment, $array)) { | ||
return null; | ||
} | ||
|
||
$array = $array[$segment]; | ||
} | ||
|
||
return $array; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace ElasticAdapter\Tests\Unit\Support; | ||
|
||
use ElasticAdapter\Support\Arr; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
/** | ||
* @covers \ElasticAdapter\Support\Arr | ||
*/ | ||
final class ArrTest extends TestCase | ||
{ | ||
public function caseProvider(): array | ||
{ | ||
return [ | ||
[['product' => ['price' => 10]], 'product', ['price' => 10]], | ||
[['product' => ['price' => 10]], 'product.price', 10], | ||
[['order' => ['items' => [['name' => 'first'], ['name' => 'second']]]], 'order.items.0.name', 'first'], | ||
[['left' => 1], 'right', null], | ||
]; | ||
} | ||
|
||
/** | ||
* @param mixed $item | ||
* | ||
* @dataProvider caseProvider | ||
* @testdox Item with key $key can be retrieved from array | ||
*/ | ||
public function test_item_can_be_received_using_dot_notation(array $array, string $key, $item): void | ||
{ | ||
$this->assertSame($item, Arr::get($array, $key)); | ||
} | ||
} |