44
55use Exception ;
66use Karriere \JsonDecoder \Bindings \AliasBinding ;
7+ use Karriere \JsonDecoder \Bindings \CallbackBinding ;
78use Karriere \JsonDecoder \Bindings \RawBinding ;
89use Karriere \JsonDecoder \Exceptions \InvalidBindingException ;
910use Karriere \JsonDecoder \Exceptions \JsonValueException ;
@@ -16,6 +17,11 @@ class ClassBindings
1617 */
1718 private $ bindings = [];
1819
20+ /**
21+ * @var array
22+ */
23+ private $ callbackBindings = [];
24+
1925 /**
2026 * @var JsonDecoder
2127 */
@@ -32,10 +38,14 @@ public function __construct(JsonDecoder $jsonDecoder)
3238 * @param mixed $instance
3339 *
3440 * @return mixed
41+ *
42+ * @throws JsonValueException
3543 */
3644 public function decode (array $ data , $ instance )
3745 {
38- foreach (array_keys ($ data ) as $ fieldName ) {
46+ $ jsonFieldNames = array_keys ($ data );
47+
48+ foreach ($ jsonFieldNames as $ fieldName ) {
3949 if ($ this ->hasBinding ($ fieldName )) {
4050 $ binding = $ this ->bindings [$ fieldName ];
4151 $ property = Property::create ($ instance , $ this ->bindings [$ fieldName ]->property ());
@@ -56,6 +66,13 @@ public function decode(array $data, $instance)
5666 }
5767 }
5868
69+ foreach ($ this ->callbackBindings as $ propertyName => $ binding ) {
70+ if (!in_array ($ propertyName , $ jsonFieldNames )) {
71+ $ property = Property::create ($ instance , $ propertyName );
72+ $ this ->handleBinding ($ binding , $ property , $ data );
73+ }
74+ }
75+
5976 return $ instance ;
6077 }
6178
@@ -68,9 +85,11 @@ public function register($binding)
6885 {
6986 if (!$ binding instanceof Binding) {
7087 throw new InvalidBindingException ();
88+ } elseif ($ binding instanceof CallbackBinding) {
89+ $ this ->callbackBindings [$ binding ->property ()] = $ binding ;
90+ } else {
91+ $ this ->bindings [$ binding ->jsonField ()] = $ binding ;
7192 }
72-
73- $ this ->bindings [$ binding ->jsonField ()] = $ binding ;
7493 }
7594
7695 /**
0 commit comments