Skip to content
Open
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Fixed

- `Data::get()` now returns `null` for existing null-valued keys instead of the supplied default.

## [3.0.3] - 2024-07-08

### Fixed
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ $data->get('some.path.that.does.not.exist', 'some-default-value');

// throws a MissingPathException because no default was given
$data->get('some.path.that.does.not.exist');

// null (the default is only used when the path does not exist)
$data = new Data(['setting' => null]);
$data->get('setting', 'fallback');
```

A more concrete example:
Expand Down
2 changes: 1 addition & 1 deletion src/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public function get(string $key, $default = null)
$currentValue = $currentValue[$currentKey];
}

return $currentValue === null ? $default : $currentValue;
return $currentValue;
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/DataInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ public function remove(string $key): void;
* Get the raw value for a key
*
* If the key does not exist, an optional default value can be returned instead.
* If no default is provided then an exception will be thrown instead.
* If the key exists and its value is null, null is returned.
* If the key does not exist and no default is provided, an exception is thrown.
*
* @param string $key
* @param mixed $default
Expand Down
15 changes: 15 additions & 0 deletions tests/DataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,21 @@ public function testGetWhenValueDoesNotExist()
$data->get('foo.bar');
}

public function testGetReturnsNullForExistingNullValueWhenDefaultIsGiven()
{
$data = new Data([
'setting' => null,
'nested' => [
'setting' => null,
],
]);

$this->assertTrue($data->has('setting'));
$this->assertNull($data->get('setting', 'fallback'));
$this->assertNull($data->get('nested.setting', 'fallback'));
$this->assertSame('fallback', $data->get('missing', 'fallback'));
}

public function testHas()
{
$data = new Data($this->getSampleData());
Expand Down