Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion src/Codeliner/ArrayReader/ArrayReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/

Expand Down Expand Up @@ -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
*/
Expand Down
52 changes: 50 additions & 2 deletions tests/Codeliner/ArrayReaderTest/ArrayReaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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']
];
}
}