Skip to content

Commit

Permalink
Merge pull request #3 from paysera/null-values-removal
Browse files Browse the repository at this point in the history
Ability to easily filter out keys with null values
  • Loading branch information
vbartusevicius authored Apr 9, 2020
2 parents 5182b28 + f1f6ca3 commit 62e99bb
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 2 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [1.1.0]
### Added
- `NormalizationContext::markNullValuesForRemoval` method to be called in normalizers.
If this is called, elements with `null` values will be removed from currently normalized object.

## [1.0.0]
### Changed
- `null` values are kept by `DataFilter` and will be available in resulted normalized data.
Expand All @@ -28,3 +33,4 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0


[1.0.0]: https://github.com/paysera/lib-normalization/compare/0.1.3...1.0.0
[1.1.0]: https://github.com/paysera/lib-normalization/compare/1.0.0...1.1.0
10 changes: 8 additions & 2 deletions src/DataFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,16 @@ private function filterObject($data, NormalizationContext $context)
{
$result = new stdClass();
foreach ($data as $key => $value) {
if ($context->areNullValuesRemoved() && $value === null) {
continue;
}

$key = (string)$key;
if ($context->isFieldIncluded($key)) {
$result->$key = $this->filterData($value, $context->createScopedContext($key));
if (!$context->isFieldIncluded($key)) {
continue;
}

$result->$key = $this->filterData($value, $context->createScopedContext($key));
}
return $result;
}
Expand Down
13 changes: 13 additions & 0 deletions src/NormalizationContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class NormalizationContext
private $includedFields;
private $path;
private $normalizationGroup;
private $nullValuesRemoved;

public function __construct(
CoreNormalizer $coreNormalizer,
Expand All @@ -20,6 +21,7 @@ public function __construct(
$this->setIncludedFields($includedFields);
$this->normalizationGroup = $normalizationGroup;
$this->path = [];
$this->nullValuesRemoved = false;
}

private function setIncludedFields(array $includedFields, bool $defaultFieldsIncluded = false)
Expand Down Expand Up @@ -57,6 +59,7 @@ public function normalize($data, string $fieldName, string $type = null)
public function createScopedContext(string $fieldName): self
{
$context = clone $this;
$context->nullValuesRemoved = false;

if ($this->isArrayItem($fieldName)) {
return $context;
Expand Down Expand Up @@ -91,4 +94,14 @@ private function isArrayItem(string $fieldName)
{
return $fieldName === '';
}

public function markNullValuesForRemoval()
{
$this->nullValuesRemoved = true;
}

public function areNullValuesRemoved(): bool
{
return $this->nullValuesRemoved;
}
}
24 changes: 24 additions & 0 deletions tests/CoreNormalizerFunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Paysera\Component\Normalization\Registry\GroupedNormalizerRegistryProvider;
use Paysera\Component\Normalization\Tests\Fixtures\Entity\InnerData;
use Paysera\Component\Normalization\Tests\Fixtures\Entity\MyData;
use Paysera\Component\Normalization\Tests\Fixtures\Normalizer\FilterOutNullsNormalizer;
use Paysera\Component\Normalization\Tests\Fixtures\Normalizer\InnerDataNormalizer;
use Paysera\Component\Normalization\Tests\Fixtures\Normalizer\MyDataNormalizer;
use Paysera\Component\Normalization\Tests\Fixtures\Normalizer\WrappedNormalizer;
Expand Down Expand Up @@ -114,4 +115,27 @@ public function testNormalizeWithDifferentGroups()

$this->assertEquals($arrayForA, (array)$result);
}

public function testNormalizeWithFilterOutNullsFunctionality()
{
$normalizerRegistryProvider = new GroupedNormalizerRegistryProvider();
$normalizerRegistryProvider->addNormalizer(new FilterOutNullsNormalizer(), MyData::class);
$normalizerRegistryProvider->addNormalizer(new InnerDataNormalizer());

$typeGuesser = new TypeGuesser();
$dataFilter = new DataFilter();

$coreNormalizer = new CoreNormalizer($normalizerRegistryProvider, $typeGuesser, $dataFilter);

$result = $coreNormalizer->normalize((new MyData())->setInnerData(
(new InnerData())->setProperty('inner_data')
));
$this->assertEquals(
(object)[
'inner' => (object)['inner_property' => 'inner_data', 'optional_property' => null],
'inner_list' => [],
],
$result
);
}
}
27 changes: 27 additions & 0 deletions tests/Fixtures/Normalizer/FilterOutNullsNormalizer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
declare(strict_types=1);

namespace Paysera\Component\Normalization\Tests\Fixtures\Normalizer;

use Paysera\Component\Normalization\NormalizationContext;
use Paysera\Component\Normalization\NormalizerInterface;
use Paysera\Component\Normalization\Tests\Fixtures\Entity\MyData;

class FilterOutNullsNormalizer implements NormalizerInterface
{
/**
* @param MyData $data
* @param NormalizationContext $normalizationContext
*
* @return mixed
*/
public function normalize($data, NormalizationContext $normalizationContext)
{
$normalizationContext->markNullValuesForRemoval();
return [
'property' => $data->getProperty(),
'inner' => $data->getInnerData(),
'inner_list' => $data->getInnerDataList(),
];
}
}

0 comments on commit 62e99bb

Please sign in to comment.