Debian Codegraph @

lib/3rdParty/composer/symfony/serializer/Context/Normalizer/AbstractObjectNormalizerContextBuilder.php

raw symbols SCIP
1 <?php
2
3 /*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <[email protected]>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12 namespace Symfony\Component\Serializer\Context\Normalizer;
13
14 use Symfony\Component\Serializer\Exception\InvalidArgumentException;
15 use Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer;
16
17 /**
18 * A helper providing autocompletion for available AbstractObjectNormalizer options.
19 *
20 * @author Mathias Arlaud <[email protected]>
21 */
22 abstract class AbstractObjectNormalizerContextBuilder extends AbstractNormalizerContextBuilder
23 {
24 /**
25 * Configures whether to respect the max depth metadata on fields.
26 */
27 public function withEnableMaxDepth(?bool $enableMaxDepth): static
28 {
29 return $this->with(AbstractObjectNormalizer::ENABLE_MAX_DEPTH, $enableMaxDepth);
30 }
31
32 /**
33 * Configures a pattern to keep track of the current depth.
34 *
35 * Must contain exactly two string placeholders.
36 *
37 * @throws InvalidArgumentException
38 */
39 public function withDepthKeyPattern(?string $depthKeyPattern): static
40 {
41 if (null === $depthKeyPattern) {
42 return $this->with(AbstractObjectNormalizer::DEPTH_KEY_PATTERN, null);
43 }
44
45 // This will match every occurrences of sprintf specifiers
46 $matches = [];
47 preg_match_all('/(?<!%)(?:%{2})*%(?<specifier>[a-z])/', $depthKeyPattern, $matches);
48
49 if (2 !== \count($matches['specifier']) || 's' !== $matches['specifier'][0] || 's' !== $matches['specifier'][1]) {
50 throw new InvalidArgumentException(\sprintf('The depth key pattern "%s" is not valid. You must set exactly two string placeholders.', $depthKeyPattern));
51 }
52
53 return $this->with(AbstractObjectNormalizer::DEPTH_KEY_PATTERN, $depthKeyPattern);
54 }
55
56 /**
57 * Configures whether verifying types match during denormalization.
58 */
59 public function withDisableTypeEnforcement(?bool $disableTypeEnforcement): static
60 {
61 return $this->with(AbstractObjectNormalizer::DISABLE_TYPE_ENFORCEMENT, $disableTypeEnforcement);
62 }
63
64 /**
65 * Configures whether fields with the value `null` should be output during normalization.
66 */
67 public function withSkipNullValues(?bool $skipNullValues): static
68 {
69 return $this->with(AbstractObjectNormalizer::SKIP_NULL_VALUES, $skipNullValues);
70 }
71
72 /**
73 * Configures whether uninitialized typed class properties should be excluded during normalization.
74 */
75 public function withSkipUninitializedValues(?bool $skipUninitializedValues): static
76 {
77 return $this->with(AbstractObjectNormalizer::SKIP_UNINITIALIZED_VALUES, $skipUninitializedValues);
78 }
79
80 /**
81 * Configures a callback to allow to set a value for an attribute when the max depth has
82 * been reached.
83 *
84 * If no callback is given, the attribute is skipped. If a callable is
85 * given, its return value is used (even if null).
86 *
87 * The arguments are:
88 *
89 * - mixed $attributeValue value of this field
90 * - object $object the whole object being normalized
91 * - string $attributeName name of the attribute being normalized
92 * - string $format the requested format
93 * - array<string, mixed> $context the serialization context
94 */
95 public function withMaxDepthHandler(?callable $maxDepthHandler): static
96 {
97 return $this->with(AbstractObjectNormalizer::MAX_DEPTH_HANDLER, $maxDepthHandler);
98 }
99
100 /**
101 * Configures which context key are not relevant to determine which attributes
102 * of an object to (de)normalize.
103 *
104 * @param list<string>|null $excludeFromCacheKeys
105 */
106 public function withExcludeFromCacheKeys(?array $excludeFromCacheKeys): static
107 {
108 return $this->with(AbstractObjectNormalizer::EXCLUDE_FROM_CACHE_KEY, $excludeFromCacheKeys);
109 }
110
111 /**
112 * Configures whether to tell the denormalizer to also populate existing objects on
113 * attributes of the main object.
114 *
115 * Setting this to true is only useful if you also specify the root object
116 * in AbstractNormalizer::OBJECT_TO_POPULATE.
117 */
118 public function withDeepObjectToPopulate(?bool $deepObjectToPopulate): static
119 {
120 return $this->with(AbstractObjectNormalizer::DEEP_OBJECT_TO_POPULATE, $deepObjectToPopulate);
121 }
122
123 /**
124 * Configures whether an empty object should be kept as an object (in
125 * JSON: {}) or converted to a list (in JSON: []).
126 */
127 public function withPreserveEmptyObjects(?bool $preserveEmptyObjects): static
128 {
129 return $this->with(AbstractObjectNormalizer::PRESERVE_EMPTY_OBJECTS, $preserveEmptyObjects);
130 }
131 }