Mercurial > hg > isophonics-drupal-site
comparison vendor/symfony/serializer/Encoder/CsvEncoder.php @ 17:129ea1e6d783
Update, including to Drupal core 8.6.10
author | Chris Cannam |
---|---|
date | Thu, 28 Feb 2019 13:21:36 +0000 |
parents | 1fec387a4317 |
children |
comparison
equal
deleted
inserted
replaced
16:c2387f117808 | 17:129ea1e6d783 |
---|---|
48 } | 48 } |
49 | 49 |
50 /** | 50 /** |
51 * {@inheritdoc} | 51 * {@inheritdoc} |
52 */ | 52 */ |
53 public function encode($data, $format, array $context = array()) | 53 public function encode($data, $format, array $context = []) |
54 { | 54 { |
55 $handle = fopen('php://temp,', 'w+'); | 55 $handle = fopen('php://temp,', 'w+'); |
56 | 56 |
57 if (!is_array($data)) { | 57 if (!\is_array($data)) { |
58 $data = array(array($data)); | 58 $data = [[$data]]; |
59 } elseif (empty($data)) { | 59 } elseif (empty($data)) { |
60 $data = array(array()); | 60 $data = [[]]; |
61 } else { | 61 } else { |
62 // Sequential arrays of arrays are considered as collections | 62 // Sequential arrays of arrays are considered as collections |
63 $i = 0; | 63 $i = 0; |
64 foreach ($data as $key => $value) { | 64 foreach ($data as $key => $value) { |
65 if ($i !== $key || !is_array($value)) { | 65 if ($i !== $key || !\is_array($value)) { |
66 $data = array($data); | 66 $data = [$data]; |
67 break; | 67 break; |
68 } | 68 } |
69 | 69 |
70 ++$i; | 70 ++$i; |
71 } | 71 } |
72 } | 72 } |
73 | 73 |
74 list($delimiter, $enclosure, $escapeChar, $keySeparator, $headers) = $this->getCsvOptions($context); | 74 list($delimiter, $enclosure, $escapeChar, $keySeparator, $headers) = $this->getCsvOptions($context); |
75 | 75 |
76 foreach ($data as &$value) { | 76 foreach ($data as &$value) { |
77 $flattened = array(); | 77 $flattened = []; |
78 $this->flatten($value, $flattened, $keySeparator); | 78 $this->flatten($value, $flattened, $keySeparator); |
79 $value = $flattened; | 79 $value = $flattened; |
80 } | 80 } |
81 unset($value); | 81 unset($value); |
82 | 82 |
105 } | 105 } |
106 | 106 |
107 /** | 107 /** |
108 * {@inheritdoc} | 108 * {@inheritdoc} |
109 */ | 109 */ |
110 public function decode($data, $format, array $context = array()) | 110 public function decode($data, $format, array $context = []) |
111 { | 111 { |
112 $handle = fopen('php://temp', 'r+'); | 112 $handle = fopen('php://temp', 'r+'); |
113 fwrite($handle, $data); | 113 fwrite($handle, $data); |
114 rewind($handle); | 114 rewind($handle); |
115 | 115 |
116 $headers = null; | 116 $headers = null; |
117 $nbHeaders = 0; | 117 $nbHeaders = 0; |
118 $headerCount = array(); | 118 $headerCount = []; |
119 $result = array(); | 119 $result = []; |
120 | 120 |
121 list($delimiter, $enclosure, $escapeChar, $keySeparator) = $this->getCsvOptions($context); | 121 list($delimiter, $enclosure, $escapeChar, $keySeparator) = $this->getCsvOptions($context); |
122 | 122 |
123 while (false !== ($cols = fgetcsv($handle, 0, $delimiter, $enclosure, $escapeChar))) { | 123 while (false !== ($cols = fgetcsv($handle, 0, $delimiter, $enclosure, $escapeChar))) { |
124 $nbCols = count($cols); | 124 $nbCols = \count($cols); |
125 | 125 |
126 if (null === $headers) { | 126 if (null === $headers) { |
127 $nbHeaders = $nbCols; | 127 $nbHeaders = $nbCols; |
128 | 128 |
129 foreach ($cols as $col) { | 129 foreach ($cols as $col) { |
130 $header = explode($keySeparator, $col); | 130 $header = explode($keySeparator, $col); |
131 $headers[] = $header; | 131 $headers[] = $header; |
132 $headerCount[] = count($header); | 132 $headerCount[] = \count($header); |
133 } | 133 } |
134 | 134 |
135 continue; | 135 continue; |
136 } | 136 } |
137 | 137 |
138 $item = array(); | 138 $item = []; |
139 for ($i = 0; ($i < $nbCols) && ($i < $nbHeaders); ++$i) { | 139 for ($i = 0; ($i < $nbCols) && ($i < $nbHeaders); ++$i) { |
140 $depth = $headerCount[$i]; | 140 $depth = $headerCount[$i]; |
141 $arr = &$item; | 141 $arr = &$item; |
142 for ($j = 0; $j < $depth; ++$j) { | 142 for ($j = 0; $j < $depth; ++$j) { |
143 // Handle nested arrays | 143 // Handle nested arrays |
146 | 146 |
147 continue; | 147 continue; |
148 } | 148 } |
149 | 149 |
150 if (!isset($arr[$headers[$i][$j]])) { | 150 if (!isset($arr[$headers[$i][$j]])) { |
151 $arr[$headers[$i][$j]] = array(); | 151 $arr[$headers[$i][$j]] = []; |
152 } | 152 } |
153 | 153 |
154 $arr = &$arr[$headers[$i][$j]]; | 154 $arr = &$arr[$headers[$i][$j]]; |
155 } | 155 } |
156 } | 156 } |
184 * @param string $parentKey | 184 * @param string $parentKey |
185 */ | 185 */ |
186 private function flatten(array $array, array &$result, $keySeparator, $parentKey = '') | 186 private function flatten(array $array, array &$result, $keySeparator, $parentKey = '') |
187 { | 187 { |
188 foreach ($array as $key => $value) { | 188 foreach ($array as $key => $value) { |
189 if (is_array($value)) { | 189 if (\is_array($value)) { |
190 $this->flatten($value, $result, $keySeparator, $parentKey.$key.$keySeparator); | 190 $this->flatten($value, $result, $keySeparator, $parentKey.$key.$keySeparator); |
191 } else { | 191 } else { |
192 $result[$parentKey.$key] = $value; | 192 $result[$parentKey.$key] = $value; |
193 } | 193 } |
194 } | 194 } |
198 { | 198 { |
199 $delimiter = isset($context[self::DELIMITER_KEY]) ? $context[self::DELIMITER_KEY] : $this->delimiter; | 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; | 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; | 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; | 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(); | 203 $headers = isset($context[self::HEADERS_KEY]) ? $context[self::HEADERS_KEY] : []; |
204 | 204 |
205 if (!is_array($headers)) { | 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))); | 206 throw new InvalidArgumentException(sprintf('The "%s" context variable must be an array or null, given "%s".', self::HEADERS_KEY, \gettype($headers))); |
207 } | 207 } |
208 | 208 |
209 return array($delimiter, $enclosure, $escapeChar, $keySeparator, $headers); | 209 return [$delimiter, $enclosure, $escapeChar, $keySeparator, $headers]; |
210 } | 210 } |
211 | 211 |
212 /** | 212 /** |
213 * @return string[] | 213 * @return string[] |
214 */ | 214 */ |
215 private function extractHeaders(array $data) | 215 private function extractHeaders(array $data) |
216 { | 216 { |
217 $headers = array(); | 217 $headers = []; |
218 $flippedHeaders = array(); | 218 $flippedHeaders = []; |
219 | 219 |
220 foreach ($data as $row) { | 220 foreach ($data as $row) { |
221 $previousHeader = null; | 221 $previousHeader = null; |
222 | 222 |
223 foreach ($row as $header => $_) { | 223 foreach ($row as $header => $_) { |
225 $previousHeader = $header; | 225 $previousHeader = $header; |
226 continue; | 226 continue; |
227 } | 227 } |
228 | 228 |
229 if (null === $previousHeader) { | 229 if (null === $previousHeader) { |
230 $n = count($headers); | 230 $n = \count($headers); |
231 } else { | 231 } else { |
232 $n = $flippedHeaders[$previousHeader] + 1; | 232 $n = $flippedHeaders[$previousHeader] + 1; |
233 | 233 |
234 for ($j = count($headers); $j > $n; --$j) { | 234 for ($j = \count($headers); $j > $n; --$j) { |
235 ++$flippedHeaders[$headers[$j] = $headers[$j - 1]]; | 235 ++$flippedHeaders[$headers[$j] = $headers[$j - 1]]; |
236 } | 236 } |
237 } | 237 } |
238 | 238 |
239 $headers[$n] = $header; | 239 $headers[$n] = $header; |