Skip to content

Commit

Permalink
Merge pull request #48 from LinioIT/fix/array-key-exists-handle-null
Browse files Browse the repository at this point in the history
Error when walking null values
  • Loading branch information
klaussilveira authored Jan 29, 2020
2 parents 8fb518a + 66b89cc commit 9024a78
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 4 deletions.
5 changes: 3 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
language: php

php:
- 7.0

- 7.1
- 7.2

cache:
directories:
- vendor
Expand Down
6 changes: 5 additions & 1 deletion src/Instantiator/PropertyInstantiator.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@

class PropertyInstantiator implements InstantiatorInterface
{
public function instantiate(string $class, array $data)
public function instantiate(string $class, ?array $data)
{
if ($data === null) {
return null;
}

$object = new $class();

foreach ($data as $key => $value) {
Expand Down
6 changes: 5 additions & 1 deletion src/Node/BaseNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -225,12 +225,16 @@ public function getValue(string $field, $value)

public function walk($input)
{
$result = [];
if (!is_array($input)) {
return $input;
}

if (!$this->hasChildren()) {
return $input;
}

$result = [];

foreach ($this->getChildren() as $field => $config) {
if (!array_key_exists($field, $input)) {
if ($config->isRequired()) {
Expand Down
56 changes: 56 additions & 0 deletions tests/InputHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,28 @@ public function define(): void
}
}

class TestNullableInputHandler extends InputHandler
{
public function define(): void
{
$this->add('name', 'string');
$this->add('address', 'string', ['allow_null' => true]);
}
}

class TestNullableRecursiveInputHandler extends InputHandler
{
public function define(): void
{
$this->add('type', 'string');
$this->add('data', \stdClass::class, [
'handler' => new TestNullableInputHandler(),
'instantiator' => new PropertyInstantiator(),
'allow_null' => true,
]);
}
}

class InputHandlerTest extends TestCase
{
public function testIsHandlingBasicInput(): void
Expand Down Expand Up @@ -483,6 +505,40 @@ public function testDatetimeInvalidDatetimeInput($datetime): void
$inputHandler->bind($input);
$this->assertFalse($inputHandler->isValid());
}

public function testIsHandlingInputWithNullValues(): void
{
$input = [
'type' => 'buyers',
'data' => [
'name' => 'John Doe',
'address' => null,
],
];

$inputHandler = new TestNullableRecursiveInputHandler();
$inputHandler->bind($input);

$this->assertTrue($inputHandler->isValid());

$data = $inputHandler->getData('data');

$this->assertNull($data->address);

$input = [
'type' => 'buyers',
'data' => null,
];

$inputHandler = new TestNullableRecursiveInputHandler();
$inputHandler->bind($input);

$this->assertTrue($inputHandler->isValid());

$data = $inputHandler->getData('data');

$this->assertNull($data);
}
}

class TestConstraintOverrideType extends InputHandler
Expand Down

0 comments on commit 9024a78

Please sign in to comment.