Skip to content

Commit

Permalink
fix: correct normalization in multipart data
Browse files Browse the repository at this point in the history
  • Loading branch information
darkdarin committed Nov 29, 2023
1 parent 2c953d2 commit 6c367a0
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions src/TransportClient/TransportClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,11 @@ protected function makeMultipartRequest(
if ($fieldName === $multipartField && $value instanceof StreamInterface) {
$builder->addResource($fieldName, $value);
} elseif ($value !== null) {
$normalizedValue = $this->normalizeValue($value, $builder);
$normalizedValue = $this->serializer->normalize(
$this->normalizeValue($value, $builder),
'json'
);

if (is_array($normalizedValue) || is_object($normalizedValue)) {
$builder->addResource(
$fieldName,
Expand Down Expand Up @@ -178,26 +182,32 @@ private function normalizeValue(mixed $value, MultipartStreamBuilder $builder):
return 'attach://' . $fileName;
}

if (is_scalar($value)) {
if (
is_scalar($value)
|| $value instanceof \UnitEnum
|| $value instanceof \DateTimeInterface
) {
return $value;
}

if (is_object($value) && !$value instanceof \UnitEnum && !$value instanceof Carbon) {
if (is_object($value)) {
$properties = [];
$reflection = new \ReflectionClass($value);
foreach ($reflection->getProperties(\ReflectionProperty::IS_PUBLIC) as $property) {
$properties[$property->getName()] = $this->normalizeValue($property->getValue($value), $builder);
}
$value = new ($reflection->getName())(...$properties);
return new ($reflection->getName())(...$properties);
}

if (is_array($value)) {
foreach ($value as $fieldName => $fieldValue) {
$normalizedValue = $this->normalizeValue($fieldValue, $builder);
$value[$fieldName] = $normalizedValue;
}

return $value;
}

return $this->serializer->normalize($value, 'json');
return $value;
}
}

0 comments on commit 6c367a0

Please sign in to comment.