-
-
Notifications
You must be signed in to change notification settings - Fork 80
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
Showing
6 changed files
with
61 additions
and
22 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
namespace App\Test\Traits; | ||
|
||
/** | ||
* Array Test Trait. | ||
*/ | ||
trait ArrayTestTrait | ||
{ | ||
/** | ||
* Read array value with dot notation. | ||
* | ||
* @param array $data The array | ||
* @param string $path The path | ||
* @param mixed $default The default return value# | ||
* | ||
* @return mixed The value from the array or the default value | ||
*/ | ||
protected function getArrayValue(array $data, string $path, mixed $default = null): mixed | ||
{ | ||
$currentValue = $data; | ||
$keyPaths = (array)explode('.', $path); | ||
|
||
foreach ($keyPaths as $currentKey) { | ||
if (isset($currentValue->$currentKey)) { | ||
$currentValue = $currentValue->$currentKey; | ||
continue; | ||
} | ||
if (isset($currentValue[$currentKey])) { | ||
$currentValue = $currentValue[$currentKey]; | ||
continue; | ||
} | ||
|
||
return $default; | ||
} | ||
|
||
// @phpstan-ignore-next-line | ||
return $currentValue === null ? $default : $currentValue; | ||
} | ||
} |
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