diff --git a/src/Resources/AbstractUnzerResource.php b/src/Resources/AbstractUnzerResource.php index 7d9e3ada..59a3750d 100644 --- a/src/Resources/AbstractUnzerResource.php +++ b/src/Resources/AbstractUnzerResource.php @@ -9,6 +9,7 @@ namespace UnzerSDK\Resources; use DateTime; +use ReflectionClass; use ReflectionException; use ReflectionProperty; use RuntimeException; @@ -495,10 +496,21 @@ public function getExternalId(): ?string */ private function exposeProperties(): array { - $properties = get_object_vars($this); - foreach ($properties as $property => $value) { - if (self::propertyShouldBeSkipped($property, $value)) { - unset($properties[$property]); + $reflectionClass = new ReflectionClass(static::class); + $reflectionProperties = $reflectionClass->getProperties(ReflectionProperty::IS_PROTECTED); // only send protected properties + + $properties = []; + foreach ($reflectionProperties as $propertyObject) { + $property = $propertyObject->getName(); + $value = $propertyObject->getValue($this); + + // do not send properties that are set to null + if ($value === null) { + continue; + } + + // do not send id property if it is empty + if ($property === 'id' && empty($value)) { continue; } @@ -516,7 +528,6 @@ private function exposeProperties(): array // handle additional values if ($property === 'additionalAttributes') { if (!is_array($value) || empty($value)) { - unset($properties[$property]); continue; } $value = $this->exposeAdditionalAttributes($value); diff --git a/test/unit/Resources/AbstractUnzerResourceTest.php b/test/unit/Resources/AbstractUnzerResourceTest.php index 2212a5c1..5e6b0194 100644 --- a/test/unit/Resources/AbstractUnzerResourceTest.php +++ b/test/unit/Resources/AbstractUnzerResourceTest.php @@ -414,6 +414,19 @@ public function additionalAttributesShouldBeSettable(): void $this->assertEquals(1234.567, $paypage->expose()['additionalAttributes']['effectiveInterestRate']); } + /** + * @test + */ + public function parentPrivatePropertiesShouldNotCauseAnException(): void + { + $metadata = new Metadata(); + $metadata->setParentResource(new Unzer('s-priv-123')); + $metadata->setSpecialParams(['something' => 'special']); + + $result = $metadata->jsonSerialize(); + $this->assertEquals('{"something":"special"}', $result); + } + // /**