DateTimeInterface::class, 'validTo' => DateTimeInterface::class, 'confidential', ]; private AddressRender $addressRender; public function __construct(AddressRender $addressRender) { $this->addressRender = $addressRender; } /** * @param Address $address * @param string|null $format */ public function normalize($address, $format = null, array $context = []) { if ($address instanceof Address) { $data = [ 'address_id' => $address->getId(), 'text' => $this->addressRender->renderString($address, []), 'street' => $address->getStreet(), 'streetNumber' => $address->getStreetNumber(), 'postcode' => [ 'id' => $address->getPostCode()->getId(), 'name' => $address->getPostCode()->getName(), 'code' => $address->getPostCode()->getCode(), ], 'country' => [ 'id' => $address->getPostCode()->getCountry()->getId(), 'name' => $address->getPostCode()->getCountry()->getName(), 'code' => $address->getPostCode()->getCountry()->getCountryCode(), ], 'floor' => $address->getFloor(), 'corridor' => $address->getCorridor(), 'steps' => $address->getSteps(), 'flat' => $address->getFlat(), 'buildingName' => $address->getBuildingName(), 'distribution' => $address->getDistribution(), 'extra' => $address->getExtra(), 'confidential' => $address->getConfidential(), 'lines' => $this->addressRender->renderLines($address), ]; if ('json' === $format) { $data['addressReference'] = $this->normalizer->normalize( $address->getAddressReference(), $format, [AbstractNormalizer::GROUPS => ['read']] ); $data['validFrom'] = $address->getValidFrom(); $data['validTo'] = $address->getValidTo(); } elseif ('docgen' === $format) { $dateContext = array_merge($context, ['docgen:expects' => DateTimeInterface::class]); $data['validFrom'] = $this->normalizer->normalize($address->getValidFrom(), $format, $dateContext); $data['validTo'] = $this->normalizer->normalize($address->getValidTo(), $format, $dateContext); } return $data; } if (null === $address) { $helper = new NormalizeNullValueHelper($this->normalizer); return array_merge( $helper->normalize(self::NULL_VALUE, $format, $context), [ 'postcode' => $helper->normalize(self::NULL_POSTCODE_COUNTRY, $format, $context), 'country' => $helper->normalize(self::NULL_POSTCODE_COUNTRY, $format, $context), ] ); } throw new UnexpectedValueException(); } public function supportsNormalization($data, $format = null, array $context = []): bool { if ('json' === $format) { return $data instanceof Address; } if ('docgen' === $format) { return $data instanceof Address || (null === $data && Address::class === ($context['docgen:expects'] ?? null)); } return false; } }