Mercurial > hg > isophonics-drupal-site
annotate vendor/consolidation/annotated-command/src/Parser/Internal/CsvUtils.php @ 19:fa3358dc1485 tip
Add ndrum files
author | Chris Cannam |
---|---|
date | Wed, 28 Aug 2019 13:14:47 +0100 |
parents | 4c8ae668cc8c |
children |
rev | line source |
---|---|
Chris@0 | 1 <?php |
Chris@0 | 2 namespace Consolidation\AnnotatedCommand\Parser\Internal; |
Chris@0 | 3 |
Chris@0 | 4 /** |
Chris@0 | 5 * Methods to convert to / from a csv string. |
Chris@0 | 6 */ |
Chris@0 | 7 class CsvUtils |
Chris@0 | 8 { |
Chris@0 | 9 /** |
Chris@0 | 10 * Ensure that the provided data is a string. |
Chris@0 | 11 * |
Chris@0 | 12 * @param string|array $data The data to convert to a string. |
Chris@0 | 13 * @return string |
Chris@0 | 14 */ |
Chris@0 | 15 public static function toString($data) |
Chris@0 | 16 { |
Chris@0 | 17 if (is_array($data)) { |
Chris@0 | 18 return static::csvEscape($data); |
Chris@0 | 19 } |
Chris@0 | 20 return $data; |
Chris@0 | 21 } |
Chris@0 | 22 |
Chris@0 | 23 /** |
Chris@0 | 24 * Convert a string to a csv. |
Chris@0 | 25 */ |
Chris@0 | 26 public static function csvEscape(array $data, $delimiter = ',') |
Chris@0 | 27 { |
Chris@0 | 28 $buffer = fopen('php://temp', 'r+'); |
Chris@0 | 29 fputcsv($buffer, $data, $delimiter); |
Chris@0 | 30 rewind($buffer); |
Chris@0 | 31 $csv = fgets($buffer); |
Chris@0 | 32 fclose($buffer); |
Chris@0 | 33 return rtrim($csv); |
Chris@0 | 34 } |
Chris@0 | 35 |
Chris@0 | 36 /** |
Chris@0 | 37 * Return a specific named annotation for this command. |
Chris@0 | 38 * |
Chris@0 | 39 * @param string|array $data The data to convert to an array. |
Chris@0 | 40 * @return array |
Chris@0 | 41 */ |
Chris@0 | 42 public static function toList($data) |
Chris@0 | 43 { |
Chris@0 | 44 if (!is_array($data)) { |
Chris@0 | 45 return str_getcsv($data); |
Chris@0 | 46 } |
Chris@0 | 47 return $data; |
Chris@0 | 48 } |
Chris@0 | 49 } |