Skip to content

Commit 44e5b18

Browse files
committed
Add context builers
1 parent cd2e251 commit 44e5b18

File tree

38 files changed

+2262
-7
lines changed

38 files changed

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

0 commit comments

Comments
 (0)