Skip to content

Commit 3e851e8

Browse files
committed
Register CallbackBindings separately
As CallbackBindings aren't bound to a JSON fieldname for flexibility, store them in a separate property on registration instead of filtering them from the main bindings array.
1 parent c436bcd commit 3e851e8

File tree

1 file changed

+12
-15
lines changed

1 file changed

+12
-15
lines changed

src/ClassBindings.php

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ class ClassBindings
1717
*/
1818
private $bindings = [];
1919

20+
/**
21+
* @var array
22+
*/
23+
private $callbackBindings = [];
24+
2025
/**
2126
* @var JsonDecoder
2227
*/
@@ -61,9 +66,9 @@ public function decode(array $data, $instance)
6166
}
6267
}
6368

64-
foreach ($this->callbackBindings() as $binding) {
65-
if (!in_array($binding->property(), $jsonFieldNames)) {
66-
$property = Property::create($instance, $binding->property());
69+
foreach ($this->callbackBindings as $propertyName => $binding) {
70+
if (!in_array($propertyName, $jsonFieldNames)) {
71+
$property = Property::create($instance, $propertyName);
6772
$this->handleBinding($binding, $property, $data);
6873
}
6974
}
@@ -80,9 +85,11 @@ public function register($binding)
8085
{
8186
if (!$binding instanceof Binding) {
8287
throw new InvalidBindingException();
88+
} elseif ($binding instanceof CallbackBinding) {
89+
$this->callbackBindings[$binding->property()] = $binding;
90+
} else {
91+
$this->bindings[$binding->jsonField()] = $binding;
8392
}
84-
85-
$this->bindings[$binding->jsonField()] = $binding;
8693
}
8794

8895
/**
@@ -190,14 +197,4 @@ private function kebapToCamelCase(string $input)
190197

191198
return $output;
192199
}
193-
194-
/**
195-
* @return CallbackBinding[]
196-
*/
197-
private function callbackBindings(): array
198-
{
199-
return array_filter($this->bindings, function ($binding) {
200-
return $binding instanceof CallbackBinding;
201-
});
202-
}
203200
}

0 commit comments

Comments
 (0)