Skip to content

Commit

Permalink
Leave nulls in normalized data (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
vbartusevicius authored and mariusbalcytis committed Dec 19, 2019
1 parent d77395c commit b633853
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 9 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
Always type-hint `Paysera\Component\Normalization\NormalizerRegistryInterface`;
- Unused constant `Paysera\Component\Normalization\NormalizerRegistryInterface::DENORMALIZER_TYPE_ARRAY`.

## [1.0.0]
### Changed
- `null` values are kept by `DataFilter` and will be available in resulted normalized data.

[Unreleased]: https://github.com/paysera/lib-normalization/compare/0.1.0...HEAD
4 changes: 2 additions & 2 deletions src/DataFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public function filterData($data, NormalizationContext $context)
return $this->filterArray($data, $context);
} elseif ($data instanceof stdClass) {
return $this->filterObject($data, $context);
} elseif (is_scalar($data)) {
} elseif (is_scalar($data) || $data === null) {
return $data;
}

Expand All @@ -38,7 +38,7 @@ private function filterObject($data, NormalizationContext $context)
$result = new stdClass();
foreach ($data as $key => $value) {
$key = (string)$key;
if ($value !== null && $context->isFieldIncluded($key)) {
if ($context->isFieldIncluded($key)) {
$result->$key = $this->filterData($value, $context->createScopedContext($key));
}
}
Expand Down
14 changes: 7 additions & 7 deletions tests/CoreNormalizerFunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ public function testNormalize()
$this->assertEquals(
(object)[
'property' => 'my_data',
'inner' => (object)['inner_property' => 'inner_data'],
'inner' => (object)['inner_property' => 'inner_data', 'optional_property' => null],
'inner_list' => [
(object)['inner_property' => 'inner_data1'],
(object)['inner_property' => 'inner_data2'],
(object)['inner_property' => 'inner_data1', 'optional_property' => null],
(object)['inner_property' => 'inner_data2', 'optional_property' => null],
],
],
$result
Expand All @@ -64,15 +64,15 @@ public function testNormalize()
'inner' => (object)['inner_property' => 'inner_data', 'optional_property' => 'optional_value'],
'inner_list' => [
(object)['inner_property' => 'inner_data1', 'optional_property' => 'optional_value1'],
(object)['inner_property' => 'inner_data2'],
(object)['inner_property' => 'inner_data2', 'optional_property' => null],
],
], $result);

$result = $coreNormalizer->normalize((new MyData())->setProperty('my_data'));
$this->assertEquals((object)['property' => 'my_data', 'inner_list' => []], $result);
$this->assertEquals((object)['property' => 'my_data', 'inner_list' => [], 'inner' => null], $result);

$result = $coreNormalizer->normalize((new MyData()));
$this->assertEquals((object)['inner_list' => []], $result);
$this->assertEquals((object)['inner_list' => [], 'inner' => null, 'property' => null], $result);

$result = $coreNormalizer->normalize([1, null]);
$this->assertEquals([1, null], $result);
Expand All @@ -88,7 +88,7 @@ public function testNormalize()
));
$this->assertEquals((object)[
'inner_list' => [
(object)['inner_property' => 'inner_data1'],
(object)['inner_property' => 'inner_data1', 'optional_property' => null],
],
], $result);
}
Expand Down
1 change: 1 addition & 0 deletions tests/DataFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public function providerForFilterDataWithoutFiltering()
['{"a":"b","1":2}', ['a' => 'b', 1 => 2]],
['[1,2]', [0 => 1, 1 => 2]],
['{"0":1,"2":2}', [0 => 1, 2 => 2]],
['{"0":null,"2":2}', [0 => null, 2 => 2]],
];
}
}

0 comments on commit b633853

Please sign in to comment.