diff --git a/src/Codeliner/ArrayReader/ArrayReader.php b/src/Codeliner/ArrayReader/ArrayReader.php index 465b78d..0df4f72 100644 --- a/src/Codeliner/ArrayReader/ArrayReader.php +++ b/src/Codeliner/ArrayReader/ArrayReader.php @@ -5,7 +5,7 @@ * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. - * + * * Date: 08.03.14 - 21:28 */ @@ -120,6 +120,22 @@ public function arrayValue($aPath, array $default = array()) return $value; } + /** + * @param string $aPath + * @param null $default + * @return mixed + */ + public function mixedValue($aPath, $default = null) + { + $value = $this->getValueFromPath($aPath); + + if (is_null($value)) { + return $default; + } + + return $value; + } + /** * @return array */ diff --git a/tests/Codeliner/ArrayReaderTest/ArrayReaderTest.php b/tests/Codeliner/ArrayReaderTest/ArrayReaderTest.php index 27b03d7..c7e0298 100644 --- a/tests/Codeliner/ArrayReaderTest/ArrayReaderTest.php +++ b/tests/Codeliner/ArrayReaderTest/ArrayReaderTest.php @@ -5,7 +5,7 @@ * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. - * + * * Date: 08.03.14 - 21:30 */ namespace Codeliner\ArrayReaderTest; @@ -359,6 +359,54 @@ public function is_escaped_dot_ignored_in_path_detection() $this->assertSame('value', $arrayReader->stringValue('hash.with\.dot\.key.nested', 'default')); } + + /** + * @test + * @dataProvider provideForMixedTest + */ + public function is_mixed_value($value) + { + $arrayReader = new ArrayReader( + array( + 'hash' => array( + 'with' => array( + 'nested' => $value + ) + ) + ) + ); + + $this->assertSame($value, $arrayReader->mixedValue('hash.with.nested')); + } + + /** + * @test + * @dataProvider provideForMixedTest + */ + public function is_default_mixed_returned_when_path_not_exists($value) + { + $arrayReader = new ArrayReader( + array( + 'hash' => array( + 'with' => array( + 'nested' => $value + ) + ) + ) + ); + + $this->assertSame(null, $arrayReader->mixedValue('hash.with.unknown', null)); + } + + public function provideForMixedTest() + { + return [ + [null], + [[1,2,3]], + [new \stdClass()], + [1], + ['string'] + ]; + } } - \ No newline at end of file