Debian Codegraph @

lib/3rdParty/composer/symfony/serializer/Context/Encoder/CsvEncoderContextBuilder.php

raw symbols SCIP
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\ContextBuilderInterface;
15 use Symfony\Component\Serializer\Context\ContextBuilderTrait;
16 use Symfony\Component\Serializer\Encoder\CsvEncoder;
17 use Symfony\Component\Serializer\Exception\InvalidArgumentException;
18
19 /**
20 * A helper providing autocompletion for available CsvEncoder options.
21 *
22 * @author Mathias Arlaud <[email protected]>
23 */
24 final class CsvEncoderContextBuilder implements ContextBuilderInterface
25 {
26 use ContextBuilderTrait;
27
28 /**
29 * Configures the column delimiter character.
30 *
31 * Must be a single character.
32 *
33 * @throws InvalidArgumentException
34 */
35 public function withDelimiter(?string $delimiter): static
36 {
37 if (null !== $delimiter && 1 !== \strlen($delimiter)) {
38 throw new InvalidArgumentException(\sprintf('The "%s" delimiter must be a single character.', $delimiter));
39 }
40
41 return $this->with(CsvEncoder::DELIMITER_KEY, $delimiter);
42 }
43
44 /**
45 * Configures the field enclosure character.
46 *
47 * Must be a single character.
48 *
49 * @throws InvalidArgumentException
50 */
51 public function withEnclosure(?string $enclosure): static
52 {
53 if (null !== $enclosure && 1 !== \strlen($enclosure)) {
54 throw new InvalidArgumentException(\sprintf('The "%s" enclosure must be a single character.', $enclosure));
55 }
56
57 return $this->with(CsvEncoder::ENCLOSURE_KEY, $enclosure);
58 }
59
60 /**
61 * Configures the escape character.
62 *
63 * Must be empty or a single character.
64 *
65 * @deprecated since Symfony 7.2, to be removed in 8.0
66 *
67 * @throws InvalidArgumentException
68 */
69 public function withEscapeChar(?string $escapeChar): static
70 {
71 trigger_deprecation('symfony/serializer', '7.2', 'The "%s" method is deprecated. It will be removed in 8.0.', __METHOD__);
72
73 if (null !== $escapeChar && \strlen($escapeChar) > 1) {
74 throw new InvalidArgumentException(\sprintf('The "%s" escape character must be empty or a single character.', $escapeChar));
75 }
76
77 return $this->with(CsvEncoder::ESCAPE_CHAR_KEY, $escapeChar);
78 }
79
80 /**
81 * Configures the key separator when (un)flattening arrays.
82 */
83 public function withKeySeparator(?string $keySeparator): static
84 {
85 return $this->with(CsvEncoder::KEY_SEPARATOR_KEY, $keySeparator);
86 }
87
88 /**
89 * Configures the headers.
90 *
91 * @param list<mixed>|null $headers
92 */
93 public function withHeaders(?array $headers): static
94 {
95 return $this->with(CsvEncoder::HEADERS_KEY, $headers);
96 }
97
98 /**
99 * Configures whether formulas should be escaped.
100 */
101 public function withEscapedFormulas(?bool $escapedFormulas): static
102 {
103 return $this->with(CsvEncoder::ESCAPE_FORMULAS_KEY, $escapedFormulas);
104 }
105
106 /**
107 * Configures whether the decoded result should be considered as a collection
108 * or as a single element.
109 */
110 public function withAsCollection(?bool $asCollection): static
111 {
112 return $this->with(CsvEncoder::AS_COLLECTION_KEY, $asCollection);
113 }
114
115 /**
116 * Configures whether the input (or output) is containing (or will contain) headers.
117 */
118 public function withNoHeaders(?bool $noHeaders): static
119 {
120 return $this->with(CsvEncoder::NO_HEADERS_KEY, $noHeaders);
121 }
122
123 /**
124 * Configures the end of line characters.
125 */
126 public function withEndOfLine(?string $endOfLine): static
127 {
128 return $this->with(CsvEncoder::END_OF_LINE, $endOfLine);
129 }
130
131 /**
132 * Configures whether to add the UTF-8 Byte Order Mark (BOM)
133 * at the beginning of the encoded result or not.
134 */
135 public function withOutputUtf8Bom(?bool $outputUtf8Bom): static
136 {
137 return $this->with(CsvEncoder::OUTPUT_UTF8_BOM_KEY, $outputUtf8Bom);
138 }
139 }