4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Serializer\Encoder;
14 use Symfony\Component\Serializer\Exception\InvalidArgumentException;
19 * @author Kévin Dunglas <dunglas@gmail.com>
20 * @author Oliver Hoff <oliver@hofff.com>
22 class CsvEncoder implements EncoderInterface, DecoderInterface
25 const DELIMITER_KEY = 'csv_delimiter';
26 const ENCLOSURE_KEY = 'csv_enclosure';
27 const ESCAPE_CHAR_KEY = 'csv_escape_char';
28 const KEY_SEPARATOR_KEY = 'csv_key_separator';
29 const HEADERS_KEY = 'csv_headers';
34 private $keySeparator;
37 * @param string $delimiter
38 * @param string $enclosure
39 * @param string $escapeChar
40 * @param string $keySeparator
42 public function __construct($delimiter = ',', $enclosure = '"', $escapeChar = '\\', $keySeparator = '.')
44 $this->delimiter = $delimiter;
45 $this->enclosure = $enclosure;
46 $this->escapeChar = $escapeChar;
47 $this->keySeparator = $keySeparator;
53 public function encode($data, $format, array $context = array())
55 $handle = fopen('php://temp,', 'w+');
57 if (!\is_array($data)) {
58 $data = array(array($data));
59 } elseif (empty($data)) {
60 $data = array(array());
62 // Sequential arrays of arrays are considered as collections
64 foreach ($data as $key => $value) {
65 if ($i !== $key || !\is_array($value)) {
74 list($delimiter, $enclosure, $escapeChar, $keySeparator, $headers) = $this->getCsvOptions($context);
76 foreach ($data as &$value) {
78 $this->flatten($value, $flattened, $keySeparator);
83 $headers = array_merge(array_values($headers), array_diff($this->extractHeaders($data), $headers));
85 fputcsv($handle, $headers, $delimiter, $enclosure, $escapeChar);
87 $headers = array_fill_keys($headers, '');
88 foreach ($data as $row) {
89 fputcsv($handle, array_replace($headers, $row), $delimiter, $enclosure, $escapeChar);
93 $value = stream_get_contents($handle);
102 public function supportsEncoding($format)
104 return self::FORMAT === $format;
110 public function decode($data, $format, array $context = array())
112 $handle = fopen('php://temp', 'r+');
113 fwrite($handle, $data);
118 $headerCount = array();
121 list($delimiter, $enclosure, $escapeChar, $keySeparator) = $this->getCsvOptions($context);
123 while (false !== ($cols = fgetcsv($handle, 0, $delimiter, $enclosure, $escapeChar))) {
124 $nbCols = \count($cols);
126 if (null === $headers) {
127 $nbHeaders = $nbCols;
129 foreach ($cols as $col) {
130 $header = explode($keySeparator, $col);
131 $headers[] = $header;
132 $headerCount[] = \count($header);
139 for ($i = 0; ($i < $nbCols) && ($i < $nbHeaders); ++$i) {
140 $depth = $headerCount[$i];
142 for ($j = 0; $j < $depth; ++$j) {
143 // Handle nested arrays
144 if ($j === ($depth - 1)) {
145 $arr[$headers[$i][$j]] = $cols[$i];
150 if (!isset($arr[$headers[$i][$j]])) {
151 $arr[$headers[$i][$j]] = array();
154 $arr = &$arr[$headers[$i][$j]];
162 if (empty($result) || isset($result[1])) {
166 // If there is only one data line in the document, return it (the line), the result is not considered as a collection
173 public function supportsDecoding($format)
175 return self::FORMAT === $format;
179 * Flattens an array and generates keys including the path.
181 * @param array $array
182 * @param array $result
183 * @param string $keySeparator
184 * @param string $parentKey
186 private function flatten(array $array, array &$result, $keySeparator, $parentKey = '')
188 foreach ($array as $key => $value) {
189 if (\is_array($value)) {
190 $this->flatten($value, $result, $keySeparator, $parentKey.$key.$keySeparator);
192 $result[$parentKey.$key] = $value;
197 private function getCsvOptions(array $context)
199 $delimiter = isset($context[self::DELIMITER_KEY]) ? $context[self::DELIMITER_KEY] : $this->delimiter;
200 $enclosure = isset($context[self::ENCLOSURE_KEY]) ? $context[self::ENCLOSURE_KEY] : $this->enclosure;
201 $escapeChar = isset($context[self::ESCAPE_CHAR_KEY]) ? $context[self::ESCAPE_CHAR_KEY] : $this->escapeChar;
202 $keySeparator = isset($context[self::KEY_SEPARATOR_KEY]) ? $context[self::KEY_SEPARATOR_KEY] : $this->keySeparator;
203 $headers = isset($context[self::HEADERS_KEY]) ? $context[self::HEADERS_KEY] : array();
205 if (!\is_array($headers)) {
206 throw new InvalidArgumentException(sprintf('The "%s" context variable must be an array or null, given "%s".', self::HEADERS_KEY, \gettype($headers)));
209 return array($delimiter, $enclosure, $escapeChar, $keySeparator, $headers);
215 private function extractHeaders(array $data)
218 $flippedHeaders = array();
220 foreach ($data as $row) {
221 $previousHeader = null;
223 foreach ($row as $header => $_) {
224 if (isset($flippedHeaders[$header])) {
225 $previousHeader = $header;
229 if (null === $previousHeader) {
230 $n = \count($headers);
232 $n = $flippedHeaders[$previousHeader] + 1;
234 for ($j = \count($headers); $j > $n; --$j) {
235 ++$flippedHeaders[$headers[$j] = $headers[$j - 1]];
239 $headers[$n] = $header;
240 $flippedHeaders[$header] = $n;
241 $previousHeader = $header;