Skip to content

Commit 1dee329

Browse files
committed
WIP
1 parent e05c25c commit 1dee329

21 files changed

+1472
-323
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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+
* Holds contexts needed by serialalization indexed by their FQCN.
16+
*
17+
* @author Mathias Arlaud <[email protected]>
18+
*/
19+
final class Context
20+
{
21+
/**
22+
* @template T of object
23+
*
24+
* @var array<class-string<T>, T> $optionsMap
25+
*/
26+
private array $optionsMap = [];
27+
28+
public function __construct(...$options)
29+
{
30+
$this->addOptions(...$options);
31+
}
32+
33+
public function addOptions(object ...$optionsList): self
34+
{
35+
foreach ($optionsList as $options) {
36+
$this->optionsMap[get_class($options)] = $options;
37+
}
38+
39+
return $this;
40+
}
41+
42+
/**
43+
* @param class-string $optionsClass
44+
*/
45+
public function removeOptions(string ...$optionsClassList): self
46+
{
47+
foreach ($optionsClassList as $optionsClass) {
48+
unset($this->optionsMap[$optionsClass]);
49+
}
50+
51+
return $this;
52+
}
53+
54+
/**
55+
* @param class-string $optionsClass
56+
*/
57+
public function hasOptions(string $optionsClass): bool
58+
{
59+
return isset($this->optionsMap[$optionsClass]);
60+
}
61+
62+
/**
63+
* @template T of object
64+
*
65+
* @param class-string<T>
66+
*
67+
* @return T|null
68+
*/
69+
public function getOptions(string $optionsClass): ?object
70+
{
71+
return $this->optionsMap[$optionsClass] ?? null;
72+
}
73+
}
Lines changed: 247 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,247 @@
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\Exception\InvalidArgumentException;
15+
16+
final class CsvEncoderOptions
17+
{
18+
/**
19+
* Column delimiter character.
20+
* Must be one character only.
21+
*/
22+
private ?string $delimiter = null;
23+
24+
/**
25+
* Field enclosure character.
26+
* This must be one character only.
27+
*/
28+
private ?string $enclosure = null;
29+
30+
/**
31+
* Escape character.
32+
* Must be one character only.
33+
*/
34+
private ?string $escapeChar = null;
35+
36+
/**
37+
* Whether formulas should be escaped.
38+
*/
39+
private ?string $endOfLine = null;
40+
41+
/**
42+
* Key separator when (un)flattening arrays.
43+
*/
44+
private ?string $keySeparator = null;
45+
46+
/**
47+
* CSV table headers.
48+
*
49+
* @var list<string>|null $headers
50+
*/
51+
private ?array $headers = null;
52+
53+
/**
54+
* Whether the decoded result should be considered as a collection
55+
* or as a single element.
56+
*/
57+
private ?bool $asCollection = null;
58+
59+
/**
60+
* Whether formulas should be escaped.
61+
*/
62+
private ?bool $escapeFormulas = null;
63+
64+
/**
65+
* Whether the input (or output) isn't containing (or won't contain) headers.
66+
*/
67+
private ?bool $withoutHeaders = null;
68+
69+
/**
70+
* End of line characters.
71+
*/
72+
private ?bool $outputUtf8Bom = null;
73+
74+
public function getDelimiter(): string
75+
{
76+
return $this->delimiter ?? ',';
77+
}
78+
79+
public function setDelimiter(?string $delimiter): self
80+
{
81+
if (null !== $delimiter && \strlen($delimiter) > 1) {
82+
throw new InvalidArgumentException(sprintf('The "%s" delimiter is not valid. It must be one character only.', $delimiter));
83+
}
84+
85+
$this->delimiter = $delimiter;
86+
87+
return $this;
88+
}
89+
90+
public function getEnclosure(): string
91+
{
92+
return $this->enclosure ?? '"';
93+
}
94+
95+
public function setEnclosure(?string $enclosure): self
96+
{
97+
if (null !== $enclosure && \strlen($enclosure) > 1) {
98+
throw new InvalidArgumentException(sprintf('The "%s" enclosure is not valid. It must be one character only.', $enclosure));
99+
}
100+
101+
$this->enclosure = $enclosure;
102+
103+
return $this;
104+
}
105+
106+
public function getEscapeChar(): string
107+
{
108+
return $this->escapeChar ?? '';
109+
}
110+
111+
public function setEscapeChar(?string $escapeChar): self
112+
{
113+
if (null !== $escapeChar && \strlen($escapeChar) > 1) {
114+
throw new InvalidArgumentException(sprintf('The "%s" escape character is not valid. It must be one character only.', $escapeChar));
115+
}
116+
117+
$this->escapeChar = $escapeChar;
118+
119+
return $this;
120+
}
121+
122+
public function getEndOfLine(): string
123+
{
124+
return $this->endOfLine ?? "\n";
125+
}
126+
127+
public function setEndOfLine(?string $endOfLine): self
128+
{
129+
$this->endOfLine = $endOfLine;
130+
131+
return $this;
132+
}
133+
134+
public function getKeySeparator(): string
135+
{
136+
return $this->keySeparator ?? '.';
137+
}
138+
139+
public function setKeySeparator(?string $keySeparator): self
140+
{
141+
$this->keySeparator = $keySeparator;
142+
143+
return $this;
144+
}
145+
146+
/**
147+
* @return list<string>
148+
*/
149+
public function getHeaders(): array
150+
{
151+
return $this->headers ?? [];
152+
}
153+
154+
/**
155+
* @param list<string>|null $headers
156+
*/
157+
public function setHeaders(?array $headers): self
158+
{
159+
$this->headers = $headers;
160+
161+
return $this;
162+
}
163+
164+
public function isAsCollection(): bool
165+
{
166+
return $this->asCollection ?? true;
167+
}
168+
169+
public function setAsCollection(?bool $asCollection): self
170+
{
171+
$this->asCollection = $asCollection;
172+
173+
return $this;
174+
}
175+
176+
public function isEscapeFormulas(): bool
177+
{
178+
return $this->escapeFormulas ?? false;
179+
}
180+
181+
public function setEscapeFormulas(?bool $escapeFormulas): self
182+
{
183+
$this->escapeFormulas = $escapeFormulas;
184+
185+
return $this;
186+
}
187+
188+
public function isWithoutHeaders(): bool
189+
{
190+
return $this->withoutHeaders ?? false;
191+
}
192+
193+
public function setWithoutHeaders(?bool $withoutHeaders): self
194+
{
195+
$this->withoutHeaders = $withoutHeaders;
196+
197+
return $this;
198+
}
199+
200+
public function isOutputUtf8Bom(): bool
201+
{
202+
return $this->outputUtf8Bom ?? false;
203+
}
204+
205+
public function setOutputUtf8Bom(?bool $outputUtf8Bom): self
206+
{
207+
$this->outputUtf8Bom = $outputUtf8Bom;
208+
209+
return $this;
210+
}
211+
212+
public function merge(self $other): self
213+
{
214+
$this->delimiter ??= $other->delimiter;
215+
$this->enclosure ??= $other->enclosure;
216+
$this->escapeChar ??= $other->escapeChar;
217+
$this->endOfLine ??= $other->endOfLine;
218+
$this->keySeparator ??= $other->keySeparator;
219+
$this->headers ??= $other->headers;
220+
$this->escapeFormulas ??= $other->escapeFormulas;
221+
$this->withoutHeaders ??= $other->withoutHeaders;
222+
$this->outputUtf8Bom ??= $other->outputUtf8Bom;
223+
224+
return $this;
225+
}
226+
227+
/**
228+
* @internal
229+
*
230+
* @param array<string, mixed> $legacyContext
231+
*/
232+
public static function fromLegacyContext(array $legacyContext = []): self
233+
{
234+
return (new self())
235+
->setDelimiter($legacyContext['csv_delimiter'] ?? null)
236+
->setEnclosure($legacyContext['csv_enclosure'] ?? null)
237+
->setEscapeChar($legacyContext['csv_escape_char'] ?? null)
238+
->setKeySeparator($legacyContext['csv_key_separator'] ?? null)
239+
->setHeaders($legacyContext['csv_headers'] ?? null)
240+
->setEscapeFormulas($legacyContext['csv_escape_formulas'] ?? null)
241+
->setAsCollection($legacyContext['as_collection'] ?? null)
242+
->setWithoutHeaders($legacyContext['no_headers'] ?? null)
243+
->setEndOfLine($legacyContext['csv_end_of_line'] ?? null)
244+
->setOutputUtf8Bom($legacyContext['output_utf8_bom'] ?? null)
245+
;
246+
}
247+
}

0 commit comments

Comments
 (0)