Skip to content

Commit

Permalink
Merge pull request #38 from otsch/bugfix/handle-callback-bindings
Browse files Browse the repository at this point in the history
Fix handling CallbackBindings
  • Loading branch information
fetzi authored Oct 15, 2020
2 parents 3e7d354 + 4d470f6 commit 8c115df
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 5 deletions.
8 changes: 6 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +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).

## [4.0.0] - 2002-10-06
## [4.0.1] - 2020-10-15
### Fixed
- Still handle CallbackBinding when property name doesn't match a JSON fieldname.

## [4.0.0] - 2020-10-06
### Added
- support for magic class properties
- auto casing for json field - class properties mapping
Expand Down Expand Up @@ -40,4 +44,4 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [2.2.0] - 2019-01-11
### Added
- `JsonDecoder` instance as second parameter to callback function signature for CallbackBindings
- `JsonDecoder` instance as second parameter to callback function signature for CallbackBindings
25 changes: 22 additions & 3 deletions src/ClassBindings.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Exception;
use Karriere\JsonDecoder\Bindings\AliasBinding;
use Karriere\JsonDecoder\Bindings\CallbackBinding;
use Karriere\JsonDecoder\Bindings\RawBinding;
use Karriere\JsonDecoder\Exceptions\InvalidBindingException;
use Karriere\JsonDecoder\Exceptions\JsonValueException;
Expand All @@ -16,6 +17,11 @@ class ClassBindings
*/
private $bindings = [];

/**
* @var array
*/
private $callbackBindings = [];

/**
* @var JsonDecoder
*/
Expand All @@ -32,10 +38,14 @@ public function __construct(JsonDecoder $jsonDecoder)
* @param mixed $instance
*
* @return mixed
*
* @throws JsonValueException
*/
public function decode(array $data, $instance)
{
foreach (array_keys($data) as $fieldName) {
$jsonFieldNames = array_keys($data);

foreach ($jsonFieldNames as $fieldName) {
if ($this->hasBinding($fieldName)) {
$binding = $this->bindings[$fieldName];
$property = Property::create($instance, $this->bindings[$fieldName]->property());
Expand All @@ -56,6 +66,13 @@ public function decode(array $data, $instance)
}
}

foreach ($this->callbackBindings as $propertyName => $binding) {
if (!in_array($propertyName, $jsonFieldNames)) {
$property = Property::create($instance, $propertyName);
$this->handleBinding($binding, $property, $data);
}
}

return $instance;
}

Expand All @@ -68,9 +85,11 @@ public function register($binding)
{
if (!$binding instanceof Binding) {
throw new InvalidBindingException();
} elseif ($binding instanceof CallbackBinding) {
$this->callbackBindings[$binding->property()] = $binding;
} else {
$this->bindings[$binding->jsonField()] = $binding;
}

$this->bindings[$binding->jsonField()] = $binding;
}

/**
Expand Down
14 changes: 14 additions & 0 deletions tests/ClassBindingsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Karriere\JsonDecoder\Tests;

use Karriere\JsonDecoder\Binding;
use Karriere\JsonDecoder\Bindings\CallbackBinding;
use Karriere\JsonDecoder\Bindings\FieldBinding;
use Karriere\JsonDecoder\ClassBindings;
use Karriere\JsonDecoder\Exceptions\InvalidBindingException;
Expand Down Expand Up @@ -55,4 +56,17 @@ public function bind($jsonDecoder, $jsonData, $property)

$classBindings->decode(['firstname' => 'John'], new Person());
}

/** @test */
public function it_executes_callback_bindings_when_property_name_is_not_contained_in_json_fields()
{
$classBindings = new ClassBindings(new JsonDecoder());
$classBindings->register(new CallbackBinding('somePropertyName', function () {
return 'yes';
}));

$person = $classBindings->decode(['firstname' => 'John'], new Person());

$this->assertEquals('yes', $person->somePropertyName);
}
}

0 comments on commit 8c115df

Please sign in to comment.