Skip to content

Commit a2bf307

Browse files
committed
Add context builers
1 parent 0d6e859 commit a2bf307

File tree

39 files changed

+2366
-7
lines changed

39 files changed

+2366
-7
lines changed

src/Symfony/Component/Serializer/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
6.1
5+
---
6+
7+
* Add the ability to create contexts using context builders.
8+
49
6.0
510
---
611

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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;
13+
14+
/**
15+
* @author Mathias Arlaud <[email protected]>
16+
*/
17+
trait ContextBuilderTrait
18+
{
19+
/**
20+
* @var array<string, mixed>
21+
*/
22+
protected array $context;
23+
24+
public function __construct(array $context = [])
25+
{
26+
$this->context = $context;
27+
}
28+
29+
protected function with(string $key, mixed $value): static
30+
{
31+
return new static(array_merge($this->context, [$key => $value]));
32+
}
33+
34+
/**
35+
* @return array<string, mixed>
36+
*/
37+
public function toArray(): array
38+
{
39+
return $this->context;
40+
}
41+
}
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
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\Encoder;
13+
14+
use Symfony\Component\Serializer\Context\ContextBuilderTrait;
15+
use Symfony\Component\Serializer\Encoder\CsvEncoder;
16+
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
17+
18+
/**
19+
* A helper providing autocompletion for available CsvEncoder options.
20+
*
21+
* @author Mathias Arlaud <[email protected]>
22+
*/
23+
final class CsvEncoderContextBuilder
24+
{
25+
use ContextBuilderTrait;
26+
27+
/**
28+
* Configures the column delimiter character.
29+
*
30+
* Must be a single character.
31+
*
32+
* @param non-empty-string|null $delimiter
33+
*
34+
* @throws InvalidArgumentException
35+
*/
36+
public function withDelimiter(?string $delimiter): static
37+
{
38+
if (null !== $delimiter && 1 !== \strlen($delimiter)) {
39+
throw new InvalidArgumentException(sprintf('The "%s" delimiter must be a single character.', $delimiter));
40+
}
41+
42+
return $this->with(CsvEncoder::DELIMITER_KEY, $delimiter);
43+
}
44+
45+
/**
46+
* Configures the field enclosure character.
47+
*
48+
* Must be a single character.
49+
*
50+
* @param non-empty-string|null $enclosure
51+
*
52+
* @throws InvalidArgumentException
53+
*/
54+
public function withEnclosure(?string $enclosure): static
55+
{
56+
if (null !== $enclosure && 1 !== \strlen($enclosure)) {
57+
throw new InvalidArgumentException(sprintf('The "%s" enclosure must be a single character.', $enclosure));
58+
}
59+
60+
return $this->with(CsvEncoder::ENCLOSURE_KEY, $enclosure);
61+
}
62+
63+
/**
64+
* Configures the escape character.
65+
*
66+
* Must be empty or a single character.
67+
*
68+
* @throws InvalidArgumentException
69+
*/
70+
public function withEscapeChar(?string $escapeChar): static
71+
{
72+
if (null !== $escapeChar && \strlen($escapeChar) > 1) {
73+
throw new InvalidArgumentException(sprintf('The "%s" escape character must be empty or a single character.', $escapeChar));
74+
}
75+
76+
return $this->with(CsvEncoder::ESCAPE_CHAR_KEY, $escapeChar);
77+
}
78+
79+
/**
80+
* Configures the key separator when (un)flattening arrays.
81+
*/
82+
public function withKeySeparator(?string $keySeparator): static
83+
{
84+
return $this->with(CsvEncoder::KEY_SEPARATOR_KEY, $keySeparator);
85+
}
86+
87+
/**
88+
* Configures the headers.
89+
*
90+
* @param list<mixed>|null $headers
91+
*/
92+
public function withHeaders(?array $headers): static
93+
{
94+
return $this->with(CsvEncoder::HEADERS_KEY, $headers);
95+
}
96+
97+
/**
98+
* Configures whether formulas should be escaped.
99+
*/
100+
public function withEscapedFormulas(?bool $escapedFormulas): static
101+
{
102+
return $this->with(CsvEncoder::ESCAPE_FORMULAS_KEY, $escapedFormulas);
103+
}
104+
105+
/**
106+
* Configures whether the decoded result should be considered as a collection
107+
* or as a single element.
108+
*/
109+
public function withAsCollection(?bool $asCollection): static
110+
{
111+
return $this->with(CsvEncoder::AS_COLLECTION_KEY, $asCollection);
112+
}
113+
114+
/**
115+
* Configures whether the input (or output) is containing (or will contain) headers.
116+
*/
117+
public function withNoHeaders(?bool $noHeaders): static
118+
{
119+
return $this->with(CsvEncoder::NO_HEADERS_KEY, $noHeaders);
120+
}
121+
122+
/**
123+
* Configures the end of line characters.
124+
*/
125+
public function withEndOfLine(?string $endOfLine): static
126+
{
127+
return $this->with(CsvEncoder::END_OF_LINE, $endOfLine);
128+
}
129+
130+
/**
131+
* Configures whether to add the UTF-8 Byte Order Mark (BOM)
132+
* at the beginning of the encoded result or not.
133+
*/
134+
public function withOutputUtf8Bom(?bool $outputUtf8Bom): static
135+
{
136+
return $this->with(CsvEncoder::OUTPUT_UTF8_BOM_KEY, $outputUtf8Bom);
137+
}
138+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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\Encoder;
13+
14+
use Symfony\Component\Serializer\Context\ContextBuilderTrait;
15+
use Symfony\Component\Serializer\Encoder\JsonDecode;
16+
use Symfony\Component\Serializer\Encoder\JsonEncode;
17+
18+
/**
19+
* A helper providing autocompletion for available JsonEncoder options.
20+
*
21+
* @author Mathias Arlaud <[email protected]>
22+
*/
23+
final class JsonEncoderContextBuilder
24+
{
25+
use ContextBuilderTrait;
26+
27+
/**
28+
* Configures the json_encode flags bitmask.
29+
*
30+
* @see https://www.php.net/manual/en/json.constants.php
31+
*
32+
* @param positive-int|null $options
33+
*/
34+
public function withEncodeOptions(?int $options): static
35+
{
36+
return $this->with(JsonEncode::OPTIONS, $options);
37+
}
38+
39+
/**
40+
* Configures the json_decode flags bitmask.
41+
*
42+
* @see https://www.php.net/manual/en/json.constants.php
43+
*
44+
* @param positive-int|null $options
45+
*/
46+
public function withDecodeOptions(?int $options): static
47+
{
48+
return $this->with(JsonDecode::OPTIONS, $options);
49+
}
50+
51+
/**
52+
* Configures whether decoded objects will be given as
53+
* associative arrays or as nested stdClass.
54+
*/
55+
public function withAssociative(?bool $associative): static
56+
{
57+
return $this->with(JsonDecode::ASSOCIATIVE, $associative);
58+
}
59+
60+
/**
61+
* Configures the maximum recursion depth.
62+
*
63+
* Must be strictly positive.
64+
*
65+
* @param positive-int|null $recursionDepth
66+
*/
67+
public function withRecursionDepth(?int $recursionDepth): static
68+
{
69+
return $this->with(JsonDecode::RECURSION_DEPTH, $recursionDepth);
70+
}
71+
}
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
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\Encoder;
13+
14+
use Symfony\Component\Serializer\Context\ContextBuilderTrait;
15+
use Symfony\Component\Serializer\Encoder\XmlEncoder;
16+
17+
/**
18+
* A helper providing autocompletion for available XmlEncoder options.
19+
*
20+
* @author Mathias Arlaud <[email protected]>
21+
*/
22+
final class XmlEncoderContextBuilder
23+
{
24+
use ContextBuilderTrait;
25+
26+
/**
27+
* Configures whether the decoded result should be considered as a collection
28+
* or as a single element.
29+
*/
30+
public function withAsCollection(?bool $asCollection): static
31+
{
32+
return $this->with(XmlEncoder::AS_COLLECTION, $asCollection);
33+
}
34+
35+
/**
36+
* Configures node types to ignore while decoding.
37+
*
38+
* @see https://www.php.net/manual/en/dom.constants.php
39+
*
40+
* @param list<int>|null $decoderIgnoredNodeTypes
41+
*/
42+
public function withDecoderIgnoredNodeTypes(?array $decoderIgnoredNodeTypes): static
43+
{
44+
return $this->with(XmlEncoder::DECODER_IGNORED_NODE_TYPES, $decoderIgnoredNodeTypes);
45+
}
46+
47+
/**
48+
* Configures node types to ignore while encoding.
49+
*
50+
* @see https://www.php.net/manual/en/dom.constants.php
51+
*
52+
* @param list<int>|null $encoderIgnoredNodeTypes
53+
*/
54+
public function withEncoderIgnoredNodeTypes(?array $encoderIgnoredNodeTypes): static
55+
{
56+
return $this->with(XmlEncoder::ENCODER_IGNORED_NODE_TYPES, $encoderIgnoredNodeTypes);
57+
}
58+
59+
/**
60+
* Configures the DOMDocument encoding.
61+
*
62+
* @see https://www.php.net/manual/en/class.domdocument.php#domdocument.props.encoding
63+
*/
64+
public function withEncoding(?string $encoding): static
65+
{
66+
return $this->with(XmlEncoder::ENCODING, $encoding);
67+
}
68+
69+
/**
70+
* Configures whether to encode with indentation and extra space.
71+
*
72+
* @see https://php.net/manual/en/class.domdocument.php#domdocument.props.formatoutput
73+
*/
74+
public function withFormatOutput(?bool $formatOutput): static
75+
{
76+
return $this->with(XmlEncoder::FORMAT_OUTPUT, $formatOutput);
77+
}
78+
79+
/**
80+
* Configures the DOMDocument::loadXml options bitmask.
81+
*
82+
* @see https://www.php.net/manual/en/libxml.constants.php
83+
*
84+
* @param positive-int|null $loadOptions
85+
*/
86+
public function withLoadOptions(?int $loadOptions): static
87+
{
88+
return $this->with(XmlEncoder::LOAD_OPTIONS, $loadOptions);
89+
}
90+
91+
/**
92+
* Configures whether to keep empty nodes.
93+
*/
94+
public function withRemoveEmptyTags(?bool $removeEmptyTags): static
95+
{
96+
return $this->with(XmlEncoder::REMOVE_EMPTY_TAGS, $removeEmptyTags);
97+
}
98+
99+
/**
100+
* Configures name of the root node.
101+
*/
102+
public function withRootNodeName(?string $rootNodeName): static
103+
{
104+
return $this->with(XmlEncoder::ROOT_NODE_NAME, $rootNodeName);
105+
}
106+
107+
/**
108+
* Configures whether the document will be standalone.
109+
*
110+
* @see https://php.net/manual/en/class.domdocument.php#domdocument.props.xmlstandalone
111+
*/
112+
public function withStandalone(?bool $standalone): static
113+
{
114+
return $this->with(XmlEncoder::STANDALONE, $standalone);
115+
}
116+
117+
/**
118+
* Configures whether casting numeric string attributes to integers or floats.
119+
*/
120+
public function withTypeCastAttributes(?bool $typeCastAttributes): static
121+
{
122+
return $this->with(XmlEncoder::TYPE_CAST_ATTRIBUTES, $typeCastAttributes);
123+
}
124+
125+
/**
126+
* Configures the version number of the document.
127+
*
128+
* @see https://php.net/manual/en/class.domdocument.php#domdocument.props.xmlversion
129+
*/
130+
public function withVersion(?string $version): static
131+
{
132+
return $this->with(XmlEncoder::VERSION, $version);
133+
}
134+
}

0 commit comments

Comments
 (0)