| Total Complexity | 6 |
| Total Lines | 42 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | <?php |
||
| 9 | class CsvDataset extends ArrayDataset |
||
| 10 | { |
||
| 11 | /** |
||
| 12 | * @var array |
||
| 13 | */ |
||
| 14 | protected $columnNames = []; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * @throws FileException |
||
| 18 | */ |
||
| 19 | public function __construct(string $filepath, int $features, bool $headingRow = true, string $delimiter = ',', int $maxLineLength = 0) |
||
| 20 | { |
||
| 21 | if (!file_exists($filepath)) { |
||
| 22 | throw new FileException(sprintf('File "%s" missing.', basename($filepath))); |
||
| 23 | } |
||
| 24 | |||
| 25 | $handle = fopen($filepath, 'rb'); |
||
| 26 | if ($handle === false) { |
||
| 27 | throw new FileException(sprintf('File "%s" can\'t be open.', basename($filepath))); |
||
| 28 | } |
||
| 29 | |||
| 30 | if ($headingRow) { |
||
| 31 | $data = fgetcsv($handle, $maxLineLength, $delimiter); |
||
| 32 | $this->columnNames = array_slice((array) $data, 0, $features); |
||
| 33 | } else { |
||
| 34 | $this->columnNames = range(0, $features - 1); |
||
| 35 | } |
||
| 36 | |||
| 37 | $samples = $targets = []; |
||
| 38 | while ($data = fgetcsv($handle, $maxLineLength, $delimiter)) { |
||
| 39 | $samples[] = array_slice($data, 0, $features); |
||
| 40 | $targets[] = $data[$features]; |
||
| 41 | } |
||
| 42 | |||
| 43 | fclose($handle); |
||
| 44 | |||
| 45 | parent::__construct($samples, $targets); |
||
| 46 | } |
||
| 47 | |||
| 48 | public function getColumnNames(): array |
||
| 51 | } |
||
| 52 | } |
||
| 53 |