-
-
Notifications
You must be signed in to change notification settings - Fork 340
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from nyamsprod/dev
Adding tones of goodies to Reader
- Loading branch information
Showing
11 changed files
with
539 additions
and
326 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,10 +3,10 @@ | |
* Bakame.csv - A lightweight CSV Coder/Decoder library | ||
* | ||
* @author Ignace Nyamagana Butera <[email protected]> | ||
* @copyright 2013 Ignace Nyamagana Butera | ||
* @copyright 2014 Ignace Nyamagana Butera | ||
* @link https://github.com/nyamsprod/Bakame.csv | ||
* @license http://opensource.org/licenses/MIT | ||
* @version 3.2.0 | ||
* @version 3.3.0 | ||
* @package Bakame.csv | ||
* | ||
* MIT LICENSE | ||
|
@@ -34,9 +34,9 @@ | |
|
||
use SplFileInfo; | ||
use SplFileObject; | ||
use SplTempFileObject; | ||
use Traversable; | ||
use InvalidArgumentException; | ||
use Bakame\Csv\Traits\CsvControls; | ||
|
||
/** | ||
* A simple Coder/Decoder to ease CSV management in PHP 5.4+ | ||
|
@@ -47,7 +47,7 @@ | |
*/ | ||
class Codec | ||
{ | ||
use CsvControlsTrait; | ||
use CsvControls; | ||
|
||
/** | ||
* The constructor | ||
|
@@ -69,22 +69,25 @@ public function __construct($delimiter = ',', $enclosure = '"', $escape = "\\", | |
* | ||
* @param string $str the csv content string | ||
* | ||
* @return \SplTempFileObject | ||
* @return \Bakame\Csv\Reader | ||
*/ | ||
public function loadString($str) | ||
{ | ||
$file = new SplTempFileObject(); | ||
$file->fwrite($str); | ||
|
||
return new Reader($file, $this->delimiter, $this->enclosure, $this->escape, $this->flags); | ||
return Reader::createFromString( | ||
$str, | ||
$this->delimiter, | ||
$this->enclosure, | ||
$this->escape, | ||
$this->flags | ||
); | ||
} | ||
|
||
/** | ||
* Load a CSV File | ||
* | ||
* @param string $str the file path | ||
* | ||
* @return \SplFileObject | ||
* @return \Bakame\Csv\Reader | ||
*/ | ||
public function loadFile($path, $mode = 'r') | ||
{ | ||
|
@@ -104,15 +107,14 @@ public function loadFile($path, $mode = 'r') | |
* @param string|\SplFileInfo $path where to save the data (String Path or SplFileInfo Instance) | ||
* @param string $mode specifies the type of access you require to the file | ||
* | ||
* @return \SplFileObject | ||
* @return \Bakame\Csv\Reader | ||
*/ | ||
public function save($data, $path, $mode = 'w') | ||
{ | ||
$file = $this->create($path, $mode, ['r+', 'w', 'w+', 'x', 'x+', 'a', 'a+', 'c', 'c+']); | ||
$data = $this->formatData($data); | ||
array_walk($data, function ($row) use ($file) { | ||
foreach ($this->formatData($data) as $row) { | ||
$file->fputcsv($row); | ||
}); | ||
} | ||
|
||
return new Reader($file, $this->delimiter, $this->enclosure, $this->escape, $this->flags); | ||
} | ||
|
@@ -156,7 +158,7 @@ private function extractRowData($row) | |
}, $row); | ||
} | ||
|
||
return explode($this->delimiter, (string) $row); | ||
return str_getcsv((string) $row, $this->delimiter, $this->enclosure, $this->escape); | ||
} | ||
|
||
/** | ||
|
Oops, something went wrong.