|
| 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 | +} |
0 commit comments