Skip to content

Commit

Permalink
Add array conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
valentin-biig committed Jun 19, 2018
1 parent 73c81cf commit 0cacdb3
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 3 deletions.
16 changes: 13 additions & 3 deletions src/AbstractMappingTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
110 changes: 110 additions & 0 deletions tests/AbstractMappingTransformerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 0cacdb3

Please sign in to comment.