Skip to content

Commit 1a6e2e3

Browse files
committed
Add context builers
1 parent cd2e251 commit 1a6e2e3

38 files changed

+2394
-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 <fabien@symfony.com>
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 <mathias.arlaud@gmail.com>
16+
*/
17+
abstract class ContextBuilder
18+
{
19+
private array $context;
20+
21+
public final function __construct(ContextBuilder|array $context = [])
22+
{
23+
if ($context instanceof ContextBuilder) {
24+
$context = $context->toArray();
25+
}
26+
27+
$this->context = $context;
28+
}
29+
30+
protected final 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 <fabien@symfony.com>
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\ContextBuilder;
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 <mathias.arlaud@gmail.com>
22+
*/
23+
class CsvEncoderContextBuilder extends ContextBuilder
24+
{
25+
/**
26+
* Configures the column delimiter character.
27+
*
28+
* Must be one character only.
29+
*
30+
* @throws InvalidArgumentException
31+
*/
32+
public function withDelimiter(?string $delimiter): static
33+
{
34+
if (null !== $delimiter && \strlen($delimiter) > 1) {
35+
throw new InvalidArgumentException(sprintf('The "%s" delimiter must be one character only.', $delimiter));
36+
}
37+
38+
return $this->with(CsvEncoder::DELIMITER_KEY, $delimiter);
39+
}
40+
41+
/**
42+
* Configures the field enclosure character.
43+
*
44+
* Must be one character only.
45+
*
46+
* @throws InvalidArgumentException
47+
*/
48+
public function withEnclosure(?string $enclosure): static
49+
{
50+
if (null !== $enclosure && \strlen($enclosure) > 1) {
51+
throw new InvalidArgumentException(sprintf('The "%s" enclosure must be one character only.', $enclosure));
52+
}
53+
54+
return $this->with(CsvEncoder::ENCLOSURE_KEY, $enclosure);
55+
}
56+
57+
/**
58+
* Configures the escape character.
59+
*
60+
* Must be one character only.
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 one character only.', $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 begining 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: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
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\ContextBuilder;
15+
use Symfony\Component\Serializer\Encoder\JsonDecode;
16+
use Symfony\Component\Serializer\Encoder\JsonEncode;
17+
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
18+
19+
/**
20+
* A helper providing autocompletion for available JsonEncoder options.
21+
*
22+
* @author Mathias Arlaud <mathias.arlaud@gmail.com>
23+
*/
24+
class JsonEncoderContextBuilder extends ContextBuilder
25+
{
26+
/**
27+
* Configures the json_encode flags bitmask.
28+
*
29+
* @see https://www.php.net/manual/en/json.constants.php
30+
*/
31+
public function withEncodeOptions(?int $options): static
32+
{
33+
return $this->with(JsonEncode::OPTIONS, $options);
34+
}
35+
36+
/**
37+
* Configures the json_decode flags bitmask.
38+
*
39+
* @see https://www.php.net/manual/en/json.constants.php
40+
*/
41+
public function withDecodeOptions(?int $options): static
42+
{
43+
return $this->with(JsonDecode::OPTIONS, $options);
44+
}
45+
46+
/**
47+
* Configures whether decoded objects will be given as
48+
* associative arrays or as nested stdClass.
49+
*/
50+
public function withAssociative(?bool $associative): static
51+
{
52+
return $this->with(JsonDecode::ASSOCIATIVE, $associative);
53+
}
54+
55+
/**
56+
* Configures the maximum recursion depth.
57+
*
58+
* Must be strictly positive.
59+
*/
60+
public function withRecursionDepth(?int $recursionDepth): static
61+
{
62+
if (null !== $recursionDepth && $recursionDepth <= 0) {
63+
throw new InvalidArgumentException(sprintf('The recursion depth must be strictly positive, "%d" given.', $recursionDepth));
64+
}
65+
66+
return $this->with(JsonDecode::RECURSION_DEPTH, $recursionDepth);
67+
}
68+
}

0 commit comments

Comments
 (0)