diff --git a/src/AbstractMappingTransformer.php b/src/AbstractMappingTransformer.php index 669335e..f5fb5f4 100644 --- a/src/AbstractMappingTransformer.php +++ b/src/AbstractMappingTransformer.php @@ -91,9 +91,19 @@ private function areConditionsValidated(array $node, array $data) } // Only transform node if field exists in array to parse - if (isset($node['condition']['exists']) - && \is_null($this->getValue($data, $node['condition']['exists']))) { - return false; + if (isset($node['condition']['exists'])) { + if (is_array($node['condition']['exists'])) { + foreach ($node['condition']['exists'] as $condition) { + if (\is_null($this->getValue($data, $condition))) { + return false; + } + } + } + + if (\is_scalar($node['condition']['exists']) + && \is_null($this->getValue($data, $node['condition']['exists']))) { + return false; + } } return true; diff --git a/tests/AbstractMappingTransformerTest.php b/tests/AbstractMappingTransformerTest.php index 291c0ed..f7a43be 100644 --- a/tests/AbstractMappingTransformerTest.php +++ b/tests/AbstractMappingTransformerTest.php @@ -137,6 +137,116 @@ public function testItThrowAnException() $transformer = new ProxyDummyTransformer(); $transformer->transformFromMapping($mapping, []); } + + public function testExistsCondition() + { + $mapping = [ + 'node' => [ + 'from' => 'foo1.foo2', + 'to' => 'bar1.bar2.bar3', + 'condition' => [ + 'exists' => 'baz' + ] + ], + ]; + + $data = [ + 'foo1' => ['foo2' => 'foo'], + 'baz' => 'hello' + ]; + + $expected = [ + 'bar1' => ['bar2' => ['bar3' => 'foo']] + ]; + + $transformer = new ProxyDummyTransformer(); + $result = $transformer->transformFromMapping($mapping, $data); + + $this->assertEquals($expected, $result); + } + + public function testNotValidatedCondition() + { + $mapping = [ + 'node' => [ + 'from' => 'foo1.foo2', + 'to' => 'bar1.bar2.bar3', + 'condition' => [ + 'exists' => 'baz' + ] + ], + ]; + + $data = [ + 'foo1' => ['foo2' => 'foo'] + ]; + + $expected = []; + + $transformer = new ProxyDummyTransformer(); + $result = $transformer->transformFromMapping($mapping, $data); + + $this->assertEquals($expected, $result); + } + + public function testArrayCondition() + { + $mapping = [ + 'node' => [ + 'from' => 'foo1.foo2', + 'to' => 'bar1.bar2.bar3', + 'condition' => [ + 'exists' => [ + 'baz', + 'boz' + ] + ] + ], + ]; + + $data = [ + 'foo1' => ['foo2' => 'foo'], + 'baz' => 'hello', + 'boz' => 'bye' + ]; + + $expected = [ + 'bar1' => ['bar2' => ['bar3' => 'foo']] + ]; + + $transformer = new ProxyDummyTransformer(); + $result = $transformer->transformFromMapping($mapping, $data); + + $this->assertEquals($expected, $result); + } + + public function testNotValidatedArrayCondition() + { + $mapping = [ + 'node' => [ + 'from' => 'foo1.foo2', + 'to' => 'bar1.bar2.bar3', + 'condition' => [ + 'exists' => [ + 'baz', + 'boz' + ] + ] + ], + ]; + + $data = [ + 'foo1' => ['foo2' => 'foo'], + 'baz' => 'hello', + ]; + + $expected = []; + + $transformer = new ProxyDummyTransformer(); + $result = $transformer->transformFromMapping($mapping, $data); + + $this->assertEquals($expected, $result); + } } class ProxyDummyTransformer extends AbstractMappingTransformer