'user', 'id' => '', 'username' => '', 'text' => '', 'label' => '', 'email' => '', ]; private UserRender $userRender; public function __construct(UserRender $userRender) { $this->userRender = $userRender; } public function normalize($user, $format = null, array $context = []) { /** @var User $user */ $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' => 'dogen:read'] ); if (null === $user && 'docgen' === $format) { return array_merge(self::NULL_USER, [ '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), ]); } $data = [ 'type' => 'user', 'id' => $user->getId(), 'username' => $user->getUsername(), 'text' => $this->userRender->renderString($user, []), 'label' => $user->getLabel(), 'email' => (string) $user->getEmail(), 'user_job' => $this->normalizer->normalize($user->getUserJob(), $format, $userJobContext), 'main_center' => $this->normalizer->normalize($user->getMainCenter(), $format, $centerContext), 'main_scope' => $this->normalizer->normalize($user->getMainScope(), $format, $scopeContext), ]; if ('docgen' === $format) { $data['current_location'] = $this->normalizer->normalize($user->getCurrentLocation(), $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; } }