'user', 'id' => '', 'username' => '', 'text' => '', 'text_without_absent' => '', 'label' => '', 'email' => '', ]; private UserRender $userRender; public function __construct(UserRender $userRender) { $this->userRender = $userRender; } public function normalize($object, $format = null, array $context = []) { /** @var User $object */ $userJobContext = array_merge( $context, ['docgen:expects' => UserJob::class, 'groups' => 'docgen:read'] ); $scopeContext = array_merge( $context, ['docgen:expects' => Scope::class, 'groups' => 'docgen:read'] ); $centerContext = array_merge( $context, ['docgen:expects' => Center::class, 'groups' => 'docgen:read'] ); $locationContext = array_merge( $context, ['docgen:expects' => Location::class, 'groups' => 'docgen:read'] ); $civilityContext = array_merge( $context, ['docgen:expects' => Civility::class, 'groups' => 'docgen:read'] ); if (null === $object && 'docgen' === $format) { return array_merge(self::NULL_USER, [ 'civility' => $this->normalizer->normalize(null, $format, $civilityContext), 'user_job' => $this->normalizer->normalize(null, $format, $userJobContext), 'main_center' => $this->normalizer->normalize(null, $format, $centerContext), 'main_scope' => $this->normalizer->normalize(null, $format, $scopeContext), 'current_location' => $this->normalizer->normalize(null, $format, $locationContext), 'main_location' => $this->normalizer->normalize(null, $format, $locationContext), ]); } $data = [ 'type' => 'user', 'id' => $object->getId(), 'username' => $object->getUsername(), 'text' => $this->userRender->renderString($object, []), 'text_without_absent' => $this->userRender->renderString($object, ['absence' => false]), 'label' => $object->getLabel(), 'email' => (string) $object->getEmail(), 'user_job' => $this->normalizer->normalize($object->getUserJob(), $format, $userJobContext), 'main_center' => $this->normalizer->normalize($object->getMainCenter(), $format, $centerContext), 'main_scope' => $this->normalizer->normalize($object->getMainScope(), $format, $scopeContext), 'isAbsent' => $object->isAbsent(), ]; if ('docgen' === $format) { $data['civility'] = $this->normalizer->normalize($object->getCivility(), $format, $civilityContext); $data['current_location'] = $this->normalizer->normalize($object->getCurrentLocation(), $format, $locationContext); $data['main_location'] = $this->normalizer->normalize($object->getMainLocation(), $format, $locationContext); } return $data; } public function supportsNormalization($data, $format = null, array $context = []): bool { if ($data instanceof User && ('json' === $format || 'docgen' === $format)) { return true; } if (null === $data && 'docgen' === $format && User::class === ($context['docgen:expects'] ?? null)) { return true; } return false; } }