first commit

This commit is contained in:
2024-07-15 12:33:27 +02:00
commit ce50ae282b
22084 changed files with 2623791 additions and 0 deletions

View File

@@ -0,0 +1,31 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Serializer\Context;
/**
* Common interface for context builders.
*
* @author Mathias Arlaud <mathias.arlaud@gmail.com>
* @author Robin Chalas <robin.chalas@gmail.com>
*/
interface ContextBuilderInterface
{
/**
* @param self|array<string, mixed> $context
*/
public function withContext(self|array $context): static;
/**
* @return array<string, mixed>
*/
public function toArray(): array;
}

View File

@@ -0,0 +1,54 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Serializer\Context;
/**
* @author Mathias Arlaud <mathias.arlaud@gmail.com>
*/
trait ContextBuilderTrait
{
/**
* @var array<string, mixed>
*/
private array $context = [];
protected function with(string $key, mixed $value): static
{
$instance = new static();
$instance->context = array_merge($this->context, [$key => $value]);
return $instance;
}
/**
* @param ContextBuilderInterface|array<string, mixed> $context
*/
public function withContext(ContextBuilderInterface|array $context): static
{
if ($context instanceof ContextBuilderInterface) {
$context = $context->toArray();
}
$instance = new static();
$instance->context = array_merge($this->context, $context);
return $instance;
}
/**
* @return array<string, mixed>
*/
public function toArray(): array
{
return $this->context;
}
}

View File

@@ -0,0 +1,135 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Serializer\Context\Encoder;
use Symfony\Component\Serializer\Context\ContextBuilderInterface;
use Symfony\Component\Serializer\Context\ContextBuilderTrait;
use Symfony\Component\Serializer\Encoder\CsvEncoder;
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
/**
* A helper providing autocompletion for available CsvEncoder options.
*
* @author Mathias Arlaud <mathias.arlaud@gmail.com>
*/
final class CsvEncoderContextBuilder implements ContextBuilderInterface
{
use ContextBuilderTrait;
/**
* Configures the column delimiter character.
*
* Must be a single character.
*
* @throws InvalidArgumentException
*/
public function withDelimiter(?string $delimiter): static
{
if (null !== $delimiter && 1 !== \strlen($delimiter)) {
throw new InvalidArgumentException(sprintf('The "%s" delimiter must be a single character.', $delimiter));
}
return $this->with(CsvEncoder::DELIMITER_KEY, $delimiter);
}
/**
* Configures the field enclosure character.
*
* Must be a single character.
*
* @throws InvalidArgumentException
*/
public function withEnclosure(?string $enclosure): static
{
if (null !== $enclosure && 1 !== \strlen($enclosure)) {
throw new InvalidArgumentException(sprintf('The "%s" enclosure must be a single character.', $enclosure));
}
return $this->with(CsvEncoder::ENCLOSURE_KEY, $enclosure);
}
/**
* Configures the escape character.
*
* Must be empty or a single character.
*
* @throws InvalidArgumentException
*/
public function withEscapeChar(?string $escapeChar): static
{
if (null !== $escapeChar && \strlen($escapeChar) > 1) {
throw new InvalidArgumentException(sprintf('The "%s" escape character must be empty or a single character.', $escapeChar));
}
return $this->with(CsvEncoder::ESCAPE_CHAR_KEY, $escapeChar);
}
/**
* Configures the key separator when (un)flattening arrays.
*/
public function withKeySeparator(?string $keySeparator): static
{
return $this->with(CsvEncoder::KEY_SEPARATOR_KEY, $keySeparator);
}
/**
* Configures the headers.
*
* @param list<mixed>|null $headers
*/
public function withHeaders(?array $headers): static
{
return $this->with(CsvEncoder::HEADERS_KEY, $headers);
}
/**
* Configures whether formulas should be escaped.
*/
public function withEscapedFormulas(?bool $escapedFormulas): static
{
return $this->with(CsvEncoder::ESCAPE_FORMULAS_KEY, $escapedFormulas);
}
/**
* Configures whether the decoded result should be considered as a collection
* or as a single element.
*/
public function withAsCollection(?bool $asCollection): static
{
return $this->with(CsvEncoder::AS_COLLECTION_KEY, $asCollection);
}
/**
* Configures whether the input (or output) is containing (or will contain) headers.
*/
public function withNoHeaders(?bool $noHeaders): static
{
return $this->with(CsvEncoder::NO_HEADERS_KEY, $noHeaders);
}
/**
* Configures the end of line characters.
*/
public function withEndOfLine(?string $endOfLine): static
{
return $this->with(CsvEncoder::END_OF_LINE, $endOfLine);
}
/**
* Configures whether to add the UTF-8 Byte Order Mark (BOM)
* at the beginning of the encoded result or not.
*/
public function withOutputUtf8Bom(?bool $outputUtf8Bom): static
{
return $this->with(CsvEncoder::OUTPUT_UTF8_BOM_KEY, $outputUtf8Bom);
}
}

View File

@@ -0,0 +1,72 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Serializer\Context\Encoder;
use Symfony\Component\Serializer\Context\ContextBuilderInterface;
use Symfony\Component\Serializer\Context\ContextBuilderTrait;
use Symfony\Component\Serializer\Encoder\JsonDecode;
use Symfony\Component\Serializer\Encoder\JsonEncode;
/**
* A helper providing autocompletion for available JsonEncoder options.
*
* @author Mathias Arlaud <mathias.arlaud@gmail.com>
*/
final class JsonEncoderContextBuilder implements ContextBuilderInterface
{
use ContextBuilderTrait;
/**
* Configures the json_encode flags bitmask.
*
* @see https://www.php.net/manual/en/json.constants.php
*
* @param positive-int|null $options
*/
public function withEncodeOptions(?int $options): static
{
return $this->with(JsonEncode::OPTIONS, $options);
}
/**
* Configures the json_decode flags bitmask.
*
* @see https://www.php.net/manual/en/json.constants.php
*
* @param positive-int|null $options
*/
public function withDecodeOptions(?int $options): static
{
return $this->with(JsonDecode::OPTIONS, $options);
}
/**
* Configures whether decoded objects will be given as
* associative arrays or as nested stdClass.
*/
public function withAssociative(?bool $associative): static
{
return $this->with(JsonDecode::ASSOCIATIVE, $associative);
}
/**
* Configures the maximum recursion depth.
*
* Must be strictly positive.
*
* @param positive-int|null $recursionDepth
*/
public function withRecursionDepth(?int $recursionDepth): static
{
return $this->with(JsonDecode::RECURSION_DEPTH, $recursionDepth);
}
}

View File

@@ -0,0 +1,155 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Serializer\Context\Encoder;
use Symfony\Component\Serializer\Context\ContextBuilderInterface;
use Symfony\Component\Serializer\Context\ContextBuilderTrait;
use Symfony\Component\Serializer\Encoder\XmlEncoder;
/**
* A helper providing autocompletion for available XmlEncoder options.
*
* @author Mathias Arlaud <mathias.arlaud@gmail.com>
*/
final class XmlEncoderContextBuilder implements ContextBuilderInterface
{
use ContextBuilderTrait;
/**
* Configures whether the decoded result should be considered as a collection
* or as a single element.
*/
public function withAsCollection(?bool $asCollection): static
{
return $this->with(XmlEncoder::AS_COLLECTION, $asCollection);
}
/**
* Configures node types to ignore while decoding.
*
* @see https://www.php.net/manual/en/dom.constants.php
*
* @param list<int>|null $decoderIgnoredNodeTypes
*/
public function withDecoderIgnoredNodeTypes(?array $decoderIgnoredNodeTypes): static
{
return $this->with(XmlEncoder::DECODER_IGNORED_NODE_TYPES, $decoderIgnoredNodeTypes);
}
/**
* Configures node types to ignore while encoding.
*
* @see https://www.php.net/manual/en/dom.constants.php
*
* @param list<int>|null $encoderIgnoredNodeTypes
*/
public function withEncoderIgnoredNodeTypes(?array $encoderIgnoredNodeTypes): static
{
return $this->with(XmlEncoder::ENCODER_IGNORED_NODE_TYPES, $encoderIgnoredNodeTypes);
}
/**
* Configures the DOMDocument encoding.
*
* @see https://www.php.net/manual/en/class.domdocument.php#domdocument.props.encoding
*/
public function withEncoding(?string $encoding): static
{
return $this->with(XmlEncoder::ENCODING, $encoding);
}
/**
* Configures whether to encode with indentation and extra space.
*
* @see https://php.net/manual/en/class.domdocument.php#domdocument.props.formatoutput
*/
public function withFormatOutput(?bool $formatOutput): static
{
return $this->with(XmlEncoder::FORMAT_OUTPUT, $formatOutput);
}
/**
* Configures the DOMDocument::loadXml options bitmask.
*
* @see https://www.php.net/manual/en/libxml.constants.php
*
* @param positive-int|null $loadOptions
*/
public function withLoadOptions(?int $loadOptions): static
{
return $this->with(XmlEncoder::LOAD_OPTIONS, $loadOptions);
}
/**
* Configures the DOMDocument::saveXml options bitmask.
*
* @see https://www.php.net/manual/en/libxml.constants.php
*
* @param positive-int|null $saveOptions
*/
public function withSaveOptions(?int $saveOptions): static
{
return $this->with(XmlEncoder::SAVE_OPTIONS, $saveOptions);
}
/**
* Configures whether to keep empty nodes.
*/
public function withRemoveEmptyTags(?bool $removeEmptyTags): static
{
return $this->with(XmlEncoder::REMOVE_EMPTY_TAGS, $removeEmptyTags);
}
/**
* Configures name of the root node.
*/
public function withRootNodeName(?string $rootNodeName): static
{
return $this->with(XmlEncoder::ROOT_NODE_NAME, $rootNodeName);
}
/**
* Configures whether the document will be standalone.
*
* @see https://php.net/manual/en/class.domdocument.php#domdocument.props.xmlstandalone
*/
public function withStandalone(?bool $standalone): static
{
return $this->with(XmlEncoder::STANDALONE, $standalone);
}
/**
* Configures whether casting numeric string attributes to integers or floats.
*/
public function withTypeCastAttributes(?bool $typeCastAttributes): static
{
return $this->with(XmlEncoder::TYPE_CAST_ATTRIBUTES, $typeCastAttributes);
}
/**
* Configures the version number of the document.
*
* @see https://php.net/manual/en/class.domdocument.php#domdocument.props.xmlversion
*/
public function withVersion(?string $version): static
{
return $this->with(XmlEncoder::VERSION, $version);
}
/**
* Configures whether to wrap strings within CDATA sections.
*/
public function withCdataWrapping(?bool $cdataWrapping): static
{
return $this->with(XmlEncoder::CDATA_WRAPPING, $cdataWrapping);
}
}

View File

@@ -0,0 +1,68 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Serializer\Context\Encoder;
use Symfony\Component\Serializer\Context\ContextBuilderInterface;
use Symfony\Component\Serializer\Context\ContextBuilderTrait;
use Symfony\Component\Serializer\Encoder\YamlEncoder;
/**
* A helper providing autocompletion for available YamlEncoder options.
*
* Note that the "indentation" setting is not offered in this builder because
* it can only be set during the construction of the YamlEncoder, but not per
* call.
*
* @author Mathias Arlaud <mathias.arlaud@gmail.com>
*/
final class YamlEncoderContextBuilder implements ContextBuilderInterface
{
use ContextBuilderTrait;
/**
* Configures the threshold to switch to inline YAML.
*/
public function withInlineThreshold(?int $inlineThreshold): static
{
return $this->with(YamlEncoder::YAML_INLINE, $inlineThreshold);
}
/**
* Configures the indentation level.
*
* Must be positive.
*
* @param int<0, max>|null $indentLevel
*/
public function withIndentLevel(?int $indentLevel): static
{
return $this->with(YamlEncoder::YAML_INDENT, $indentLevel);
}
/**
* Configures \Symfony\Component\Yaml\Dumper::dump flags bitmask.
*
* @see \Symfony\Component\Yaml\Yaml
*/
public function withFlags(?int $flags): static
{
return $this->with(YamlEncoder::YAML_FLAGS, $flags);
}
/**
* Configures whether to preserve empty objects "{}" or to convert them to null.
*/
public function withPreservedEmptyObjects(?bool $preserveEmptyObjects): static
{
return $this->with(YamlEncoder::PRESERVE_EMPTY_OBJECTS, $preserveEmptyObjects);
}
}

View File

@@ -0,0 +1,184 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Serializer\Context\Normalizer;
use Symfony\Component\Serializer\Context\ContextBuilderInterface;
use Symfony\Component\Serializer\Context\ContextBuilderTrait;
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
/**
* A helper providing autocompletion for available AbstractNormalizer options.
*
* @author Mathias Arlaud <mathias.arlaud@gmail.com>
*/
abstract class AbstractNormalizerContextBuilder implements ContextBuilderInterface
{
use ContextBuilderTrait;
/**
* Configures how many loops of circular reference to allow while normalizing.
*
* The value 1 means that when we encounter the same object a
* second time, we consider that a circular reference.
*
* You can raise this value for special cases, e.g. in combination with the
* max depth setting of the object normalizer.
*
* Must be strictly positive.
*
* @param positive-int|null $circularReferenceLimit
*/
public function withCircularReferenceLimit(?int $circularReferenceLimit): static
{
return $this->with(AbstractNormalizer::CIRCULAR_REFERENCE_LIMIT, $circularReferenceLimit);
}
/**
* Configures an object to be updated instead of creating a new instance.
*
* If you have a nested structure, child objects will be overwritten with
* new instances unless you set AbstractObjectNormalizer::DEEP_OBJECT_TO_POPULATE to true.
*/
public function withObjectToPopulate(?object $objectToPopulate): static
{
return $this->with(AbstractNormalizer::OBJECT_TO_POPULATE, $objectToPopulate);
}
/**
* Configures groups containing attributes to (de)normalize.
*
* Eg: ['group1', 'group2']
*
* @param list<string>|string|null $groups
*/
public function withGroups(array|string|null $groups): static
{
if (null === $groups) {
return $this->with(AbstractNormalizer::GROUPS, null);
}
return $this->with(AbstractNormalizer::GROUPS, (array) $groups);
}
/**
* Configures attributes to (de)normalize.
*
* For nested structures, this list needs to reflect the object tree.
*
* Eg: ['foo', 'bar', 'object' => ['baz']]
*
* @param array<string|array>|null $attributes
*
* @throws InvalidArgumentException
*/
public function withAttributes(?array $attributes): static
{
$it = new \RecursiveIteratorIterator(new \RecursiveArrayIterator($attributes ?? []), \RecursiveIteratorIterator::LEAVES_ONLY);
foreach ($it as $attribute) {
if (!\is_string($attribute)) {
throw new InvalidArgumentException(sprintf('Each attribute must be a string, "%s" given.', get_debug_type($attribute)));
}
}
return $this->with(AbstractNormalizer::ATTRIBUTES, $attributes);
}
/**
* If AbstractNormalizer::ATTRIBUTES are specified, and the source has fields that are not part of that list,
* configures whether to ignore those attributes or throw an ExtraAttributesException.
*/
public function withAllowExtraAttributes(?bool $allowExtraAttributes): static
{
return $this->with(AbstractNormalizer::ALLOW_EXTRA_ATTRIBUTES, $allowExtraAttributes);
}
/**
* Configures a hashmap of classes containing hashmaps of constructor argument => default value.
*
* The names need to match the parameter names in the constructor arguments.
*
* Eg: [Foo::class => ['foo' => true, 'bar' => 0]]
*
* @param array<class-string, array<string, mixed>>|null $defaultConstructorArguments
*/
public function withDefaultConstructorArguments(?array $defaultConstructorArguments): static
{
return $this->with(AbstractNormalizer::DEFAULT_CONSTRUCTOR_ARGUMENTS, $defaultConstructorArguments);
}
/**
* Deprecated in Symfony 7.1, use withDefaultConstructorArguments() instead.
*/
public function withDefaultContructorArguments(?array $defaultContructorArguments): static
{
return self::withDefaultConstructorArguments($defaultContructorArguments);
}
/**
* Configures an hashmap of field name => callable to normalize this field.
*
* The callable is called if the field is encountered with the arguments:
*
* - mixed $attributeValue value of this field
* - object $object the whole object being normalized
* - string $attributeName name of the attribute being normalized
* - string $format the requested format
* - array<string, mixed> $context the serialization context
*
* @param array<string, callable>|null $callbacks
*/
public function withCallbacks(?array $callbacks): static
{
return $this->with(AbstractNormalizer::CALLBACKS, $callbacks);
}
/**
* Configures an handler to call when a circular reference has been detected.
*
* If no handler is specified, a CircularReferenceException is thrown.
*
* The method will be called with ($object, $format, $context) and its
* return value is returned as the result of the normalize call.
*/
public function withCircularReferenceHandler(?callable $circularReferenceHandler): static
{
return $this->with(AbstractNormalizer::CIRCULAR_REFERENCE_HANDLER, $circularReferenceHandler);
}
/**
* Configures attributes to be skipped when normalizing an object tree.
*
* This list is applied to each element of nested structures.
*
* Eg: ['foo', 'bar']
*
* Note: The behaviour for nested structures is different from ATTRIBUTES
* for historical reason. Aligning the behaviour would be a BC break.
*
* @param list<string>|null $ignoredAttributes
*/
public function withIgnoredAttributes(?array $ignoredAttributes): static
{
return $this->with(AbstractNormalizer::IGNORED_ATTRIBUTES, $ignoredAttributes);
}
/**
* Configures requiring all properties to be listed in the input instead
* of falling back to null for nullable ones.
*/
public function withRequireAllProperties(?bool $requireAllProperties = true): static
{
return $this->with(AbstractNormalizer::REQUIRE_ALL_PROPERTIES, $requireAllProperties);
}
}

View File

@@ -0,0 +1,131 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Serializer\Context\Normalizer;
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
use Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer;
/**
* A helper providing autocompletion for available AbstractObjectNormalizer options.
*
* @author Mathias Arlaud <mathias.arlaud@gmail.com>
*/
abstract class AbstractObjectNormalizerContextBuilder extends AbstractNormalizerContextBuilder
{
/**
* Configures whether to respect the max depth metadata on fields.
*/
public function withEnableMaxDepth(?bool $enableMaxDepth): static
{
return $this->with(AbstractObjectNormalizer::ENABLE_MAX_DEPTH, $enableMaxDepth);
}
/**
* Configures a pattern to keep track of the current depth.
*
* Must contain exactly two string placeholders.
*
* @throws InvalidArgumentException
*/
public function withDepthKeyPattern(?string $depthKeyPattern): static
{
if (null === $depthKeyPattern) {
return $this->with(AbstractObjectNormalizer::DEPTH_KEY_PATTERN, null);
}
// This will match every occurrences of sprintf specifiers
$matches = [];
preg_match_all('/(?<!%)(?:%{2})*%(?<specifier>[a-z])/', $depthKeyPattern, $matches);
if (2 !== \count($matches['specifier']) || 's' !== $matches['specifier'][0] || 's' !== $matches['specifier'][1]) {
throw new InvalidArgumentException(sprintf('The depth key pattern "%s" is not valid. You must set exactly two string placeholders.', $depthKeyPattern));
}
return $this->with(AbstractObjectNormalizer::DEPTH_KEY_PATTERN, $depthKeyPattern);
}
/**
* Configures whether verifying types match during denormalization.
*/
public function withDisableTypeEnforcement(?bool $disableTypeEnforcement): static
{
return $this->with(AbstractObjectNormalizer::DISABLE_TYPE_ENFORCEMENT, $disableTypeEnforcement);
}
/**
* Configures whether fields with the value `null` should be output during normalization.
*/
public function withSkipNullValues(?bool $skipNullValues): static
{
return $this->with(AbstractObjectNormalizer::SKIP_NULL_VALUES, $skipNullValues);
}
/**
* Configures whether uninitialized typed class properties should be excluded during normalization.
*/
public function withSkipUninitializedValues(?bool $skipUninitializedValues): static
{
return $this->with(AbstractObjectNormalizer::SKIP_UNINITIALIZED_VALUES, $skipUninitializedValues);
}
/**
* Configures a callback to allow to set a value for an attribute when the max depth has
* been reached.
*
* If no callback is given, the attribute is skipped. If a callable is
* given, its return value is used (even if null).
*
* The arguments are:
*
* - mixed $attributeValue value of this field
* - object $object the whole object being normalized
* - string $attributeName name of the attribute being normalized
* - string $format the requested format
* - array<string, mixed> $context the serialization context
*/
public function withMaxDepthHandler(?callable $maxDepthHandler): static
{
return $this->with(AbstractObjectNormalizer::MAX_DEPTH_HANDLER, $maxDepthHandler);
}
/**
* Configures which context key are not relevant to determine which attributes
* of an object to (de)normalize.
*
* @param list<string>|null $excludeFromCacheKeys
*/
public function withExcludeFromCacheKeys(?array $excludeFromCacheKeys): static
{
return $this->with(AbstractObjectNormalizer::EXCLUDE_FROM_CACHE_KEY, $excludeFromCacheKeys);
}
/**
* Configures whether to tell the denormalizer to also populate existing objects on
* attributes of the main object.
*
* Setting this to true is only useful if you also specify the root object
* in AbstractNormalizer::OBJECT_TO_POPULATE.
*/
public function withDeepObjectToPopulate(?bool $deepObjectToPopulate): static
{
return $this->with(AbstractObjectNormalizer::DEEP_OBJECT_TO_POPULATE, $deepObjectToPopulate);
}
/**
* Configures whether an empty object should be kept as an object (in
* JSON: {}) or converted to a list (in JSON: []).
*/
public function withPreserveEmptyObjects(?bool $preserveEmptyObjects): static
{
return $this->with(AbstractObjectNormalizer::PRESERVE_EMPTY_OBJECTS, $preserveEmptyObjects);
}
}

View File

@@ -0,0 +1,35 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Serializer\Context\Normalizer;
use Symfony\Component\Serializer\Context\ContextBuilderInterface;
use Symfony\Component\Serializer\Context\ContextBuilderTrait;
use Symfony\Component\Serializer\Normalizer\BackedEnumNormalizer;
/**
* A helper providing autocompletion for available BackedEnumNormalizer options.
*
* @author Nicolas PHILIPPE <nikophil@gmail.com>
*/
final class BackedEnumNormalizerContextBuilder implements ContextBuilderInterface
{
use ContextBuilderTrait;
/**
* Configures if invalid values are allowed in denormalization.
* They will be denormalized into `null` values.
*/
public function withAllowInvalidValues(bool $allowInvalidValues): static
{
return $this->with(BackedEnumNormalizer::ALLOW_INVALID_VALUES, $allowInvalidValues);
}
}

View File

@@ -0,0 +1,71 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Serializer\Context\Normalizer;
use Symfony\Component\Serializer\Context\ContextBuilderInterface;
use Symfony\Component\Serializer\Context\ContextBuilderTrait;
use Symfony\Component\Serializer\Normalizer\ConstraintViolationListNormalizer;
/**
* A helper providing autocompletion for available ConstraintViolationList options.
*
* @author Mathias Arlaud <mathias.arlaud@gmail.com>
*/
final class ConstraintViolationListNormalizerContextBuilder implements ContextBuilderInterface
{
use ContextBuilderTrait;
/**
* Configure the instance field of normalized data.
*/
public function withInstance(mixed $instance): static
{
return $this->with(ConstraintViolationListNormalizer::INSTANCE, $instance);
}
/**
* Configure the status field of normalized data.
*/
public function withStatus(?int $status): static
{
return $this->with(ConstraintViolationListNormalizer::STATUS, $status);
}
/**
* Configure the title field of normalized data.
*/
public function withTitle(?string $title): static
{
return $this->with(ConstraintViolationListNormalizer::TITLE, $title);
}
/**
* Configure the type field of normalized data.
*/
public function withType(?string $type): static
{
return $this->with(ConstraintViolationListNormalizer::TYPE, $type);
}
/**
* Configures the payload fields which will act as an allowlist
* for the payload field of normalized data.
*
* Eg: ['foo', 'bar']
*
* @param list<string>|null $payloadFields
*/
public function withPayloadFields(?array $payloadFields): static
{
return $this->with(ConstraintViolationListNormalizer::PAYLOAD_FIELDS, $payloadFields);
}
}

View File

@@ -0,0 +1,36 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Serializer\Context\Normalizer;
use Symfony\Component\Serializer\Context\ContextBuilderInterface;
use Symfony\Component\Serializer\Context\ContextBuilderTrait;
use Symfony\Component\Serializer\Normalizer\DateIntervalNormalizer;
/**
* A helper providing autocompletion for available DateIntervalNormalizer options.
*
* @author Mathias Arlaud <mathias.arlaud@gmail.com>
*/
final class DateIntervalNormalizerContextBuilder implements ContextBuilderInterface
{
use ContextBuilderTrait;
/**
* Configures the format of the interval.
*
* @see https://php.net/manual/en/dateinterval.format.php
*/
public function withFormat(?string $format): static
{
return $this->with(DateIntervalNormalizer::FORMAT_KEY, $format);
}
}

View File

@@ -0,0 +1,64 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Serializer\Context\Normalizer;
use Symfony\Component\Serializer\Context\ContextBuilderInterface;
use Symfony\Component\Serializer\Context\ContextBuilderTrait;
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer;
/**
* A helper providing autocompletion for available DateTimeNormalizer options.
*
* @author Mathias Arlaud <mathias.arlaud@gmail.com>
*/
final class DateTimeNormalizerContextBuilder implements ContextBuilderInterface
{
use ContextBuilderTrait;
/**
* Configures the format of the date.
*
* @see https://secure.php.net/manual/en/datetime.format.php
*/
public function withFormat(?string $format): static
{
return $this->with(DateTimeNormalizer::FORMAT_KEY, $format);
}
/**
* Configures the timezone of the date.
*
* It could be either a \DateTimeZone or a string
* that will be used to construct the \DateTimeZone
*
* @see https://secure.php.net/manual/en/class.datetimezone.php
*
* @throws InvalidArgumentException
*/
public function withTimezone(\DateTimeZone|string|null $timezone): static
{
if (null === $timezone) {
return $this->with(DateTimeNormalizer::TIMEZONE_KEY, null);
}
if (\is_string($timezone)) {
try {
$timezone = new \DateTimeZone($timezone);
} catch (\Exception $e) {
throw new InvalidArgumentException(sprintf('The "%s" timezone is invalid.', $timezone), previous: $e);
}
}
return $this->with(DateTimeNormalizer::TIMEZONE_KEY, $timezone);
}
}

View File

@@ -0,0 +1,50 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Serializer\Context\Normalizer;
use Symfony\Component\Serializer\Context\ContextBuilderInterface;
use Symfony\Component\Serializer\Context\ContextBuilderTrait;
use Symfony\Component\Serializer\Normalizer\FormErrorNormalizer;
/**
* A helper providing autocompletion for available FormErrorNormalizer options.
*
* @author Mathias Arlaud <mathias.arlaud@gmail.com>
*/
final class FormErrorNormalizerContextBuilder implements ContextBuilderInterface
{
use ContextBuilderTrait;
/**
* Configures the title of the normalized data.
*/
public function withTitle(?string $title): static
{
return $this->with(FormErrorNormalizer::TITLE, $title);
}
/**
* Configures the type of the normalized data.
*/
public function withType(?string $type): static
{
return $this->with(FormErrorNormalizer::TYPE, $type);
}
/**
* Configures the code of the normalized data.
*/
public function withStatusCode(?int $statusCode): static
{
return $this->with(FormErrorNormalizer::CODE, $statusCode);
}
}

View File

@@ -0,0 +1,21 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Serializer\Context\Normalizer;
/**
* A helper providing autocompletion for available GetSetMethodNormalizer options.
*
* @author Mathias Arlaud <mathias.arlaud@gmail.com>
*/
final class GetSetMethodNormalizerContextBuilder extends AbstractObjectNormalizerContextBuilder
{
}

View File

@@ -0,0 +1,21 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Serializer\Context\Normalizer;
/**
* A helper providing autocompletion for available JsonSerializableNormalizer options.
*
* @author Mathias Arlaud <mathias.arlaud@gmail.com>
*/
final class JsonSerializableNormalizerContextBuilder extends AbstractNormalizerContextBuilder
{
}

View File

@@ -0,0 +1,21 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Serializer\Context\Normalizer;
/**
* A helper providing autocompletion for available ObjectNormalizer options.
*
* @author Mathias Arlaud <mathias.arlaud@gmail.com>
*/
final class ObjectNormalizerContextBuilder extends AbstractObjectNormalizerContextBuilder
{
}

View File

@@ -0,0 +1,50 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Serializer\Context\Normalizer;
use Symfony\Component\Serializer\Context\ContextBuilderInterface;
use Symfony\Component\Serializer\Context\ContextBuilderTrait;
use Symfony\Component\Serializer\Normalizer\ProblemNormalizer;
/**
* A helper providing autocompletion for available ProblemNormalizer options.
*
* @author Mathias Arlaud <mathias.arlaud@gmail.com>
*/
final class ProblemNormalizerContextBuilder implements ContextBuilderInterface
{
use ContextBuilderTrait;
/**
* Configure the title field of normalized data.
*/
public function withTitle(?string $title): static
{
return $this->with(ProblemNormalizer::TITLE, $title);
}
/**
* Configure the type field of normalized data.
*/
public function withType(?string $type): static
{
return $this->with(ProblemNormalizer::TYPE, $type);
}
/**
* Configure the status field of normalized data.
*/
public function withStatusCode(int|string|null $statusCode): static
{
return $this->with(ProblemNormalizer::STATUS, $statusCode);
}
}

View File

@@ -0,0 +1,30 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Serializer\Context\Normalizer;
use Symfony\Component\Serializer\Normalizer\PropertyNormalizer;
/**
* A helper providing autocompletion for available PropertyNormalizer options.
*
* @author Mathias Arlaud <mathias.arlaud@gmail.com>
*/
final class PropertyNormalizerContextBuilder extends AbstractObjectNormalizerContextBuilder
{
/**
* Configures whether fields should be output based on visibility.
*/
public function withNormalizeVisibility(int $normalizeVisibility): static
{
return $this->with(PropertyNormalizer::NORMALIZE_VISIBILITY, $normalizeVisibility);
}
}

View File

@@ -0,0 +1,41 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Serializer\Context\Normalizer;
use Symfony\Component\Serializer\Context\ContextBuilderInterface;
use Symfony\Component\Serializer\Context\ContextBuilderTrait;
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
use Symfony\Component\Serializer\Normalizer\UidNormalizer;
/**
* A helper providing autocompletion for available UidNormalizer options.
*
* @author Mathias Arlaud <mathias.arlaud@gmail.com>
*/
final class UidNormalizerContextBuilder implements ContextBuilderInterface
{
use ContextBuilderTrait;
/**
* Configures the uuid format for normalization.
*
* @throws InvalidArgumentException
*/
public function withNormalizationFormat(?string $normalizationFormat): static
{
if (null !== $normalizationFormat && !\in_array($normalizationFormat, UidNormalizer::NORMALIZATION_FORMATS, true)) {
throw new InvalidArgumentException(sprintf('The "%s" normalization format is not valid.', $normalizationFormat));
}
return $this->with(UidNormalizer::NORMALIZATION_FORMAT_KEY, $normalizationFormat);
}
}

View File

@@ -0,0 +1,53 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Serializer\Context\Normalizer;
use Symfony\Component\PropertyAccess\Exception\InvalidPropertyPathException;
use Symfony\Component\PropertyAccess\PropertyPath;
use Symfony\Component\Serializer\Context\ContextBuilderInterface;
use Symfony\Component\Serializer\Context\ContextBuilderTrait;
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
use Symfony\Component\Serializer\Normalizer\UnwrappingDenormalizer;
/**
* A helper providing autocompletion for available UnwrappingDenormalizer options.
*
* @author Mathias Arlaud <mathias.arlaud@gmail.com>
*/
final class UnwrappingDenormalizerContextBuilder implements ContextBuilderInterface
{
use ContextBuilderTrait;
/**
* Configures the path of wrapped data during denormalization.
*
* Eg: [foo].bar[bar]
*
* @see https://symfony.com/doc/current/components/property_access.html
*
* @throws InvalidArgumentException
*/
public function withUnwrapPath(?string $unwrapPath): static
{
if (null === $unwrapPath) {
return $this->with(UnwrappingDenormalizer::UNWRAP_PATH, null);
}
try {
new PropertyPath($unwrapPath);
} catch (InvalidPropertyPathException $e) {
throw new InvalidArgumentException(sprintf('The "%s" property path is not valid.', $unwrapPath), previous: $e);
}
return $this->with(UnwrappingDenormalizer::UNWRAP_PATH, $unwrapPath);
}
}

View File

@@ -0,0 +1,39 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Serializer\Context;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
use Symfony\Component\Serializer\Serializer;
/**
* A helper providing autocompletion for available Serializer options.
*
* @author Mathias Arlaud <mathias.arlaud@gmail.com>
*/
final class SerializerContextBuilder implements ContextBuilderInterface
{
use ContextBuilderTrait;
/**
* Configures whether an empty array should be transformed to an
* object (in JSON: {}) or to a list (in JSON: []).
*/
public function withEmptyArrayAsObject(?bool $emptyArrayAsObject): static
{
return $this->with(Serializer::EMPTY_ARRAY_AS_OBJECT, $emptyArrayAsObject);
}
public function withCollectDenormalizationErrors(?bool $collectDenormalizationErrors): static
{
return $this->with(DenormalizerInterface::COLLECT_DENORMALIZATION_ERRORS, $collectDenormalizationErrors);
}
}