lib/3rdParty/composer/symfony/serializer/Context/Normalizer/AbstractNormalizerContextBuilder.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\Context\ContextBuilderInterface; |
| 15 | use Symfony\Component\Serializer\Context\ContextBuilderTrait; |
| 16 | use Symfony\Component\Serializer\Exception\InvalidArgumentException; |
| 17 | use Symfony\Component\Serializer\Normalizer\AbstractNormalizer; |
| 18 | |
| 19 | /** |
| 20 | * A helper providing autocompletion for available AbstractNormalizer options. |
| 21 | * |
| 22 | * @author Mathias Arlaud <[email protected]> |
| 23 | */ |
| 24 | abstract class AbstractNormalizerContextBuilder implements ContextBuilderInterface |
| 25 | { |
| 26 | use ContextBuilderTrait; |
| 27 | |
| 28 | /** |
| 29 | * Configures how many loops of circular reference to allow while normalizing. |
| 30 | * |
| 31 | * The value 1 means that when we encounter the same object a |
| 32 | * second time, we consider that a circular reference. |
| 33 | * |
| 34 | * You can raise this value for special cases, e.g. in combination with the |
| 35 | * max depth setting of the object normalizer. |
| 36 | * |
| 37 | * Must be strictly positive. |
| 38 | * |
| 39 | * @param positive-int|null $circularReferenceLimit |
| 40 | */ |
| 41 | public function withCircularReferenceLimit(?int $circularReferenceLimit): static |
| 42 | { |
| 43 | return $this->with(AbstractNormalizer::CIRCULAR_REFERENCE_LIMIT, $circularReferenceLimit); |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Configures an object to be updated instead of creating a new instance. |
| 48 | * |
| 49 | * If you have a nested structure, child objects will be overwritten with |
| 50 | * new instances unless you set AbstractObjectNormalizer::DEEP_OBJECT_TO_POPULATE to true. |
| 51 | */ |
| 52 | public function withObjectToPopulate(?object $objectToPopulate): static |
| 53 | { |
| 54 | return $this->with(AbstractNormalizer::OBJECT_TO_POPULATE, $objectToPopulate); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Configures groups containing attributes to (de)normalize. |
| 59 | * |
| 60 | * Eg: ['group1', 'group2'] |
| 61 | * |
| 62 | * @param list<string>|string|null $groups |
| 63 | */ |
| 64 | public function withGroups(array|string|null $groups): static |
| 65 | { |
| 66 | if (null === $groups) { |
| 67 | return $this->with(AbstractNormalizer::GROUPS, null); |
| 68 | } |
| 69 | |
| 70 | return $this->with(AbstractNormalizer::GROUPS, (array) $groups); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Configures attributes to (de)normalize. |
| 75 | * |
| 76 | * For nested structures, this list needs to reflect the object tree. |
| 77 | * |
| 78 | * Eg: ['foo', 'bar', 'object' => ['baz']] |
| 79 | * |
| 80 | * @param array<string|array>|null $attributes |
| 81 | * |
| 82 | * @throws InvalidArgumentException |
| 83 | */ |
| 84 | public function withAttributes(?array $attributes): static |
| 85 | { |
| 86 | $it = new \RecursiveIteratorIterator(new \RecursiveArrayIterator($attributes ?? []), \RecursiveIteratorIterator::LEAVES_ONLY); |
| 87 | |
| 88 | foreach ($it as $attribute) { |
| 89 | if (!\is_string($attribute)) { |
| 90 | throw new InvalidArgumentException(\sprintf('Each attribute must be a string, "%s" given.', get_debug_type($attribute))); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | return $this->with(AbstractNormalizer::ATTRIBUTES, $attributes); |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * If AbstractNormalizer::ATTRIBUTES are specified, and the source has fields that are not part of that list, |
| 99 | * configures whether to ignore those attributes or throw an ExtraAttributesException. |
| 100 | */ |
| 101 | public function withAllowExtraAttributes(?bool $allowExtraAttributes): static |
| 102 | { |
| 103 | return $this->with(AbstractNormalizer::ALLOW_EXTRA_ATTRIBUTES, $allowExtraAttributes); |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * @deprecated since Symfony 7.1, use withDefaultConstructorArguments(?array $defaultConstructorArguments)" instead |
| 108 | * |
| 109 | * @param array<class-string, array<string, mixed>>|null $defaultContructorArguments |
| 110 | */ |
| 111 | public function withDefaultContructorArguments(?array $defaultContructorArguments): static |
| 112 | { |
| 113 | trigger_deprecation('symfony/serializer', '7.1', 'The "%s()" method is deprecated, use "withDefaultConstructorArguments(?array $defaultConstructorArguments)" instead.', __METHOD__); |
| 114 | |
| 115 | return self::withDefaultConstructorArguments($defaultContructorArguments); |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Configures a hashmap of classes containing hashmaps of constructor argument => default value. |
| 120 | * |
| 121 | * The names need to match the parameter names in the constructor arguments. |
| 122 | * |
| 123 | * Eg: [Foo::class => ['foo' => true, 'bar' => 0]] |
| 124 | * |
| 125 | * @param array<class-string, array<string, mixed>>|null $defaultConstructorArguments |
| 126 | */ |
| 127 | public function withDefaultConstructorArguments(?array $defaultConstructorArguments): static |
| 128 | { |
| 129 | return $this->with(AbstractNormalizer::DEFAULT_CONSTRUCTOR_ARGUMENTS, $defaultConstructorArguments); |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Configures an hashmap of field name => callable to normalize this field. |
| 134 | * |
| 135 | * The callable is called if the field is encountered with the arguments: |
| 136 | * |
| 137 | * - mixed $attributeValue value of this field |
| 138 | * - object $object the whole object being normalized |
| 139 | * - string $attributeName name of the attribute being normalized |
| 140 | * - string $format the requested format |
| 141 | * - array<string, mixed> $context the serialization context |
| 142 | * |
| 143 | * @param array<string, callable>|null $callbacks |
| 144 | */ |
| 145 | public function withCallbacks(?array $callbacks): static |
| 146 | { |
| 147 | return $this->with(AbstractNormalizer::CALLBACKS, $callbacks); |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Configures an handler to call when a circular reference has been detected. |
| 152 | * |
| 153 | * If no handler is specified, a CircularReferenceException is thrown. |
| 154 | * |
| 155 | * The method will be called with ($object, $format, $context) and its |
| 156 | * return value is returned as the result of the normalize call. |
| 157 | */ |
| 158 | public function withCircularReferenceHandler(?callable $circularReferenceHandler): static |
| 159 | { |
| 160 | return $this->with(AbstractNormalizer::CIRCULAR_REFERENCE_HANDLER, $circularReferenceHandler); |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Configures attributes to be skipped when normalizing an object tree. |
| 165 | * |
| 166 | * This list is applied to each element of nested structures. |
| 167 | * |
| 168 | * Eg: ['foo', 'bar'] |
| 169 | * |
| 170 | * Note: The behaviour for nested structures is different from ATTRIBUTES |
| 171 | * for historical reason. Aligning the behaviour would be a BC break. |
| 172 | * |
| 173 | * @param list<string>|null $ignoredAttributes |
| 174 | */ |
| 175 | public function withIgnoredAttributes(?array $ignoredAttributes): static |
| 176 | { |
| 177 | return $this->with(AbstractNormalizer::IGNORED_ATTRIBUTES, $ignoredAttributes); |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * Configures requiring all properties to be listed in the input instead |
| 182 | * of falling back to null for nullable ones. |
| 183 | */ |
| 184 | public function withRequireAllProperties(?bool $requireAllProperties = true): static |
| 185 | { |
| 186 | return $this->with(AbstractNormalizer::REQUIRE_ALL_PROPERTIES, $requireAllProperties); |
| 187 | } |
| 188 | } |