|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace EpicKris\DataFormats; |
| 4 | + |
| 5 | +/** |
| 6 | + * CSV data format trait. |
| 7 | + * |
| 8 | + * @package EpicKris\DataFormats |
| 9 | + */ |
| 10 | +trait Csv |
| 11 | +{ |
| 12 | + /** |
| 13 | + * Format data to CSV string. |
| 14 | + * |
| 15 | + * @param array|object $data Data. |
| 16 | + * |
| 17 | + * @return string Returns CSV string. |
| 18 | + * @throws \InvalidArgumentException If data argument is not an array or object. |
| 19 | + */ |
| 20 | + public static function toCsv($data) |
| 21 | + { |
| 22 | + if (! is_array($data) && ! is_object($data)) { |
| 23 | + throw new \InvalidArgumentException('Data argument must be an array or object.'); |
| 24 | + } |
| 25 | + |
| 26 | + if (is_object($data)) { |
| 27 | + $data = get_object_vars($data); |
| 28 | + } |
| 29 | + |
| 30 | + $headings = array_keys($data); |
| 31 | + |
| 32 | + $output = implode(',', $headings) . '\r\n'; |
| 33 | + foreach ($data as $value) { |
| 34 | + $output .= '"' . implode('","', $value) . '"\r\n'; |
| 35 | + } |
| 36 | + |
| 37 | + return $output; |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * Format data array to CSV string. |
| 42 | + * |
| 43 | + * @param array $data Data array. |
| 44 | + * |
| 45 | + * @return string Returns CSV string. |
| 46 | + * @throws \InvalidArgumentException If data argument is not an array. |
| 47 | + */ |
| 48 | + public static function arrayToCsv($data) |
| 49 | + { |
| 50 | + if (! is_array($data)) { |
| 51 | + throw new \InvalidArgumentException('Data argument must be an array.'); |
| 52 | + } |
| 53 | + |
| 54 | + return self::toCsv($data); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Format data object to CSV string. |
| 59 | + * |
| 60 | + * @param object $data Data object. |
| 61 | + * |
| 62 | + * @return string Returns CSV string. |
| 63 | + * @throws \InvalidArgumentException If CSV argument is not an object. |
| 64 | + */ |
| 65 | + public static function objectToCsv($data) |
| 66 | + { |
| 67 | + if (! is_object($data)) { |
| 68 | + throw new \InvalidArgumentException('Data argument must be an object.'); |
| 69 | + } |
| 70 | + |
| 71 | + return self::toCsv($data); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Format CSV string to data array. |
| 76 | + * |
| 77 | + * @param string $csv CSV string. |
| 78 | + * |
| 79 | + * @return array Returns data array. |
| 80 | + * @throws \InvalidArgumentException If CSV argument is not a CSV string. |
| 81 | + */ |
| 82 | + public static function csvToArray($csv) |
| 83 | + { |
| 84 | + if (! is_string($csv)) { |
| 85 | + throw new \InvalidArgumentException('CSV argument must be a CSV string.'); |
| 86 | + } |
| 87 | + |
| 88 | + $csv = trim($csv); |
| 89 | + |
| 90 | + return str_getcsv($csv); |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Format CSV string to data object. |
| 95 | + * |
| 96 | + * @param string $csv CSV string. |
| 97 | + * |
| 98 | + * @return object Returns data object. |
| 99 | + * @throws \InvalidArgumentException If CSV argument is not a CSV string. |
| 100 | + */ |
| 101 | + public static function csvToObject($csv) |
| 102 | + { |
| 103 | + if (! is_string($csv)) { |
| 104 | + throw new \InvalidArgumentException('CSV argument must be a CSV string.'); |
| 105 | + } |
| 106 | + |
| 107 | + $csv = trim($csv); |
| 108 | + $array = str_getcsv($csv); |
| 109 | + |
| 110 | + $object = new \stdClass(); |
| 111 | + foreach ($array as $key => $value) { |
| 112 | + $object->$key = $value; |
| 113 | + } |
| 114 | + } |
| 115 | +} |
0 commit comments