|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace DutchCodingCompany\CsvCollection; |
| 4 | + |
| 5 | +use Illuminate\Support\LazyCollection; |
| 6 | + |
| 7 | +class CsvCollection extends LazyCollection |
| 8 | +{ |
| 9 | + /** |
| 10 | + * The collection's default options. |
| 11 | + * |
| 12 | + * @var array |
| 13 | + */ |
| 14 | + public static array $defaults = [ |
| 15 | + 'delimiter' => ',', |
| 16 | + 'enclosure' => '"', |
| 17 | + 'escape' => '\\', |
| 18 | + 'header' => true, |
| 19 | + ]; |
| 20 | + |
| 21 | + /** |
| 22 | + * The collection's options. |
| 23 | + * |
| 24 | + * @var array |
| 25 | + */ |
| 26 | + public array $options = []; |
| 27 | + |
| 28 | + /** |
| 29 | + * Create a new csv collection instance. |
| 30 | + * |
| 31 | + * @param mixed $source |
| 32 | + * @return void |
| 33 | + */ |
| 34 | + public function __construct($source = null) |
| 35 | + { |
| 36 | + parent::__construct($source); |
| 37 | + |
| 38 | + $this->options(static::$defaults); |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Load the csv file items into a new collection. |
| 43 | + * |
| 44 | + * @param string $file |
| 45 | + * @param array $options |
| 46 | + * @return static |
| 47 | + */ |
| 48 | + public function open(string $file, array $options = []): self |
| 49 | + { |
| 50 | + $options = array_merge( |
| 51 | + $this->options, $options |
| 52 | + ); |
| 53 | + |
| 54 | + return static::make(static function () use ($file, $options) { |
| 55 | + $resource = fopen($file, 'r'); |
| 56 | + |
| 57 | + $read = static fn() => fgetcsv( |
| 58 | + $resource, 0, |
| 59 | + $options['delimiter'], |
| 60 | + $options['enclosure'], |
| 61 | + $options['escape'], |
| 62 | + ); |
| 63 | + |
| 64 | + $header = null; |
| 65 | + |
| 66 | + // Loop over the rows and yield them into a generator. |
| 67 | + while (($line = $read()) !== false) { |
| 68 | + if (! ($options['header'])) { |
| 69 | + yield $line; |
| 70 | + continue; |
| 71 | + } |
| 72 | + |
| 73 | + if (! $header) { |
| 74 | + $header = $line; |
| 75 | + continue; |
| 76 | + } |
| 77 | + |
| 78 | + yield array_combine($header, $line); |
| 79 | + } |
| 80 | + |
| 81 | + fclose($resource); |
| 82 | + }); |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Save the collection items to the csv file. |
| 87 | + * |
| 88 | + * @param string|null $file |
| 89 | + * @param array $options |
| 90 | + * @return $this |
| 91 | + */ |
| 92 | + public function save(string $file, array $options = []): self |
| 93 | + { |
| 94 | + $options = array_merge( |
| 95 | + $this->options, $options |
| 96 | + ); |
| 97 | + |
| 98 | + $resource = fopen($file, 'w'); |
| 99 | + |
| 100 | + $write = static fn(array $line) => fputcsv( |
| 101 | + $resource, $line, |
| 102 | + $options['delimiter'], |
| 103 | + $options['enclosure'], |
| 104 | + $options['escape'], |
| 105 | + ); |
| 106 | + |
| 107 | + if ($options['header']) { |
| 108 | + $write(array_keys($this->first())); |
| 109 | + } |
| 110 | + |
| 111 | + $this->each($write); |
| 112 | + |
| 113 | + fclose($resource); |
| 114 | + |
| 115 | + return $this; |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * Set the collection's options. |
| 120 | + * |
| 121 | + * @param array $options |
| 122 | + * @return static |
| 123 | + */ |
| 124 | + public function options(array $options): self |
| 125 | + { |
| 126 | + $this->options = array_merge( |
| 127 | + $this->options, $options |
| 128 | + ); |
| 129 | + |
| 130 | + return $this; |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * Set the collection's default options. |
| 135 | + * |
| 136 | + * @param array $options |
| 137 | + * @return void |
| 138 | + */ |
| 139 | + public static function defaults(array $options): void |
| 140 | + { |
| 141 | + static::$defaults = array_merge( |
| 142 | + static::$defaults, $options |
| 143 | + ); |
| 144 | + } |
| 145 | +} |
0 commit comments