Skip to content

Commit

Permalink
wip insert at
Browse files Browse the repository at this point in the history
  • Loading branch information
lpheller committed Nov 6, 2023
1 parent 878dbf6 commit 358d987
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 3 deletions.
7 changes: 7 additions & 0 deletions src/Csv.php
Original file line number Diff line number Diff line change
Expand Up @@ -232,4 +232,11 @@ public function toJson()
{
return json_encode($this->toArray(), JSON_THROW_ON_ERROR);
}

public function insertAt(int $index, array $row)
{
$this->processor->insertAt($index, $row);

return $this;
}
}
17 changes: 16 additions & 1 deletion src/CsvProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function __construct(public FileHandler $fileHandler)
{
}

public function process()
public function process($insert = false)
{
if (($handle = $this->fileHandler->openFile()) === false) {
return;
Expand All @@ -42,6 +42,13 @@ public function process()
while (($row = fgetcsv($handle, null, $this->delimiter)) !== false) {
$rowNumber++;

if ($insert) {
// $this->insert($handle);
yield $handle;

continue;
}

if (in_array($rowNumber, $this->skipRows)) {
continue; // Skip rows based on specified row numbers
}
Expand Down Expand Up @@ -206,4 +213,12 @@ public function combineWithHeader($header, $row)

return $row;
}

public function insertAt($position = 0, $rowData = [])
{
$writer = new CsvWriter([]);
$writer->setFileHandler($this->fileHandler);
$writer->insertRow($position, $rowData);
// $writer->toFile($this->fileHandler->filePath);
}
}
69 changes: 69 additions & 0 deletions src/CsvWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,27 @@

namespace Heller\SimpleCsv;

use Heller\SimpleCsv\Support\FileHandler;

class CsvWriter
{
protected $data = [];

protected $headers = [];

protected $filePath;

protected $fileHandler;

public function __construct(array $data)
{
$this->data = $data;
}

public function toFile(string $filePath, $append = false)
{
$this->filePath = $filePath;

$fileMode = $append ? 'a+' : 'w';
$handle = fopen($filePath, $fileMode);
if ($handle === false) {
Expand Down Expand Up @@ -61,4 +69,65 @@ protected function normalizeRowWithHeaders($row)

return $row;
}

protected function getFileHandler()
{
if ($this->fileHandler === null) {
$this->fileHandler = new FileHandler($this->filePath);
}

return $this->fileHandler;
}

public function setFileHandler(FileHandler $fileHandler)
{
$this->fileHandler = $fileHandler;

return $this;
}

public function insertRow(int $position, array $rowData)
{
$handle = $this->getFileHandler()->openFile();

if ($handle === false) {
return;
}

// Create a temporary file to write the modified content
$tempFile = tmpfile();

// Copy data up to the specified position
for ($i = 1; $i < $position; $i++) {
$line = fgets($handle);
fwrite($tempFile, $line);
}

// Write the new row data to the temporary file
fputcsv($tempFile, $rowData);

// Copy the original row at the specified position
$line = fgets($handle);
fwrite($tempFile, $line);

// Copy the remaining data after the inserted row
while (($line = fgets($handle)) !== false) {
fwrite($tempFile, $line);
}

// Rewind both file pointers
rewind($handle);
rewind($tempFile);

// Copy the content from the temporary file back to the original file
while (($line = fgets($tempFile)) !== false) {
fwrite($handle, $line);
}

// Close both files
fclose($handle);
fclose($tempFile);

return $this;
}
}
2 changes: 1 addition & 1 deletion src/Support/FileHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public function __construct(protected string $filePath)
*/
public function openFile()
{
return str_starts_with($this->filePath, 'http') ? $this->handleFromUrl() : fopen($this->filePath, 'r');
return str_starts_with($this->filePath, 'http') ? $this->handleFromUrl() : fopen($this->filePath, 'r+');
}

protected function handleFromUrl()
Expand Down
5 changes: 4 additions & 1 deletion tests/Fixtures/data.csv
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
Foo,Bar,Baz
Foo1,Bar1,Baz1
"John Doe",[email protected]
Foo1,Bar1,Baz1
"John Doe",[email protected]
Foo2,Bar2,Baz2
7 changes: 7 additions & 0 deletions tests/Unit/CsvTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -404,3 +404,10 @@ function makeTestFile($rows = 1000)
expect($csv->count())->toBe(3);

});

test('foobar', function () {
$file = __DIR__.'/../Fixtures/data.csv';

$csv = Csv::read($file)
->insertAt(4, ['name' => 'John Doe', 'email' => '[email protected]']);
});

0 comments on commit 358d987

Please sign in to comment.