Skip to content

Commit e91531d

Browse files
committed
fix bug with null values for array binding fields
1 parent f95ccb6 commit e91531d

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

src/Bindings/ArrayBinding.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,13 @@ public function bind($jsonDecoder, $jsonData, $propertyAccessor)
6060
$data = $jsonData[$this->jsonField];
6161
$values = [];
6262

63-
foreach ($data as $item) {
64-
$values[] = $jsonDecoder->decodeArray($item, $this->type);
65-
}
63+
if (is_array($data)) {
64+
foreach ($data as $item) {
65+
$values[] = $jsonDecoder->decodeArray($item, $this->type);
66+
}
6667

67-
$propertyAccessor->set($values);
68+
$propertyAccessor->set($values);
69+
}
6870
}
6971

7072
/**

tests/specs/Bindings/ArrayBindingSpec.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Karriere\JsonDecoder\JsonDecoder;
88
use Karriere\JsonDecoder\PropertyAccessor;
99
use PhpSpec\ObjectBehavior;
10+
use Prophecy\Argument;
1011

1112
class ArrayBindingSpec extends ObjectBehavior
1213
{
@@ -36,6 +37,13 @@ public function it_should_decode_the_array(JsonDecoder $jsonDecoder, PropertyAcc
3637

3738
$this->bind($jsonDecoder, ['field' => [[], []]], $propertyAccessor);
3839
}
40+
41+
public function it_should_ignore_null_values(JsonDecoder $jsonDecoder, PropertyAccessor $propertyAccessor)
42+
{
43+
$jsonDecoder->decodeArray(Argument::any(), Argument::any())->shouldNotBeCalled();
44+
45+
$this->bind($jsonDecoder, ['field' => null], $propertyAccessor);
46+
}
3947
}
4048

4149
class Sample

0 commit comments

Comments
 (0)