Closed
Description
Description
Feature:
Max depth can be defined set to 0.
Usecase:
A user
object that has the property parentUser
. That parent user should be represented as string rather than an object as it would be with maxDepth=1
on the first level.
Example
class UserNormalizer implements ContextAwareNormalizerInterface, NormalizerAwareInterface
{
[...]
public function normalize($object, $format = null, array $context = [])
{
[...]
$maxDepthHandler = static fn ($innerObject): string => $innerObject ? $innerObject->toString() : '';
$context[AbstractObjectNormalizer::MAX_DEPTH_HANDLER] = $maxDepthHandler;
return $this->normalizer->normalize($object, $format, $context);
}
[...]
}
Before:
class User
{
/**
* @MaxDepth(1)
*/
private ?User $parentUser = null;
}
Result:
[
'parentUser' => [
'parentUser' => 'string'
]
]
After:
class User
{
/**
* @MaxDepth(0)
*/
private ?User $parentUser = null;
}
Result:
[
'parentUser' => 'string'
]