Skip to content

Commit

Permalink
Implement CSV add method
Browse files Browse the repository at this point in the history
  • Loading branch information
g105b committed Jan 6, 2015
1 parent d71c472 commit 14850ef
Showing 1 changed file with 54 additions and 8 deletions.
62 changes: 54 additions & 8 deletions src/Data/Csv.php
Expand Up @@ -10,10 +10,12 @@

use \Gt\Core\Path;
use \League\Csv\Reader;
use \League\Csv\Writer;

class Csv { //implements \Iterator {
class Csv {

private $reader;
private $file;
private $headers;

public function __construct($path) {
Expand All @@ -25,8 +27,8 @@ public function __construct($path) {
throw new DataSourceNotFoundException($path);
}

$file = new \SplFileObject($path);
$this->reader = Reader::createFromPath($file);
$this->file = new \SplFileObject($path, "r+");
$this->reader = Reader::createFromPath($this->file);
$this->headers = $this->reader->fetchOne();
}

Expand All @@ -48,6 +50,27 @@ public function getAll() {
return $result;
}

/**
*
*/
public function getLast() {
$all = $this->reader->fetchAll();
$last = [];
$i = count($all) - 1;

while(empty($last) || empty($last[0]) ){
$last = $all[$i];
$i--;
}

foreach ($last as $key => $value) {
$last[$this->headers[$key]] = $value;
unset($last[$key]);
}

return $last;
}

/**
* Return a dataset where the matching column key is the provided value.
*
Expand All @@ -71,12 +94,17 @@ public function findBy($key, $value, $strict = false) {
});

$this->reader->addFilter(function($row, $index) use($columnCount) {
// Only return rows with valid columns.
for($i = 0; $i < $columnCount; $i++) {
if(!isset($row[$i])) {
return false;
}
// Only return rows with at least the forename column.
$forenameIndex = array_search("Forename", $this->headers);
if(!isset($row[$forenameIndex])) {
return false;
}
// // Only return rows with valid columns.
// for($i = 0; $i < $columnCount; $i++) {
// if(!isset($row[$i])) {
// return false;
// }
// }

return true;
});
Expand Down Expand Up @@ -104,4 +132,22 @@ public function findBy($key, $value, $strict = false) {
return $result;
}

/**
*
*/
public function add($data) {
// $writer = $this->reader->newWriter();
$this->file->fseek(0, SEEK_END);

// while($this->file->fread(1) !== "\n") {
// $this->file->fseek(-1);
// }

// $this->file->fwrite("\n");
$this->file->fputcsv($data);
// var_dump($this->file, $this->reader);die();
// $writer->insertOne($data);
// var_dump($writer, $data);die("writer");
}

}#

0 comments on commit 14850ef

Please sign in to comment.