parent
c8ef4daf90
commit
f918c5bfd8
@ -1,129 +0,0 @@ |
||||
<?php |
||||
|
||||
/** |
||||
* Write objects to a stream in csv format based on map. |
||||
* |
||||
* Usage |
||||
* |
||||
* $object->property_name_1 = 'name 1'; |
||||
* $object->property_name_2 = 'name 2'; |
||||
* |
||||
* $map = array( 'property_name_1' => 'Header title 1', |
||||
* 'property_name_2' => 'Header title 2'); |
||||
* |
||||
* $writer = CsvObjectWriter::create($map, 'temp'); |
||||
* $writer->add($object); |
||||
* |
||||
* Output |
||||
* |
||||
* "Header title 1";"Header title 2" |
||||
* "name 1";"name 2" |
||||
* |
||||
* @license /licence.txt |
||||
* @author Laurent Opprecht <laurent@opprecht.info> |
||||
*/ |
||||
class CsvObjectWriter extends CsvWriter |
||||
{ |
||||
|
||||
/** |
||||
* |
||||
* @param string|object $stream |
||||
* @return CsvWriter |
||||
*/ |
||||
static function create($stream, $map = '*', $delimiter = ';', $enclosure = '"') |
||||
{ |
||||
return new self($stream, $map = '*', $map, $delimiter, $enclosure); |
||||
} |
||||
|
||||
protected $map = '*'; |
||||
protected $headers_written = false; |
||||
|
||||
function __construct($stream, $map = '*', $delimiter = ';', $enclosure = '"') |
||||
{ |
||||
parent::__construct($stream, $delimiter, $enclosure); |
||||
$this->map = $map; |
||||
} |
||||
|
||||
public function get_map() |
||||
{ |
||||
return $this->map; |
||||
} |
||||
|
||||
/** |
||||
* |
||||
* @param object $item |
||||
* @return boolean |
||||
*/ |
||||
public function put($item) |
||||
{ |
||||
$data = $this->convert($item); |
||||
if (empty($data)) { |
||||
return false; |
||||
} |
||||
$this->writer_headers(); |
||||
parent::put($data); |
||||
return true; |
||||
} |
||||
|
||||
/** |
||||
* Convert object to array of data |
||||
* @param object $object |
||||
* @return array |
||||
*/ |
||||
protected function convert($item) |
||||
{ |
||||
$result = array(); |
||||
$map = $this->map; |
||||
if ($map == '*') { |
||||
return (array) $item; |
||||
} |
||||
foreach ($map as $key => $value) { |
||||
$result[$key] = isset($item->{$key}) ? $item->{$key} : ''; |
||||
} |
||||
return $result; |
||||
} |
||||
|
||||
/** |
||||
* |
||||
* @param array $items |
||||
*/ |
||||
public function add_all($items) |
||||
{ |
||||
foreach ($items as $item) { |
||||
$this->add($item); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* |
||||
* @param array|object $item |
||||
*/ |
||||
public function add($item) |
||||
{ |
||||
if (is_array($item)) { |
||||
$this->add_all($item); |
||||
return; |
||||
} |
||||
$this->put($item); |
||||
} |
||||
|
||||
protected function writer_headers() |
||||
{ |
||||
if ($this->headers_written) { |
||||
return; |
||||
} |
||||
$this->headers_written = true; |
||||
|
||||
$map = $this->map; |
||||
if (!is_array($map)) { |
||||
return; |
||||
} |
||||
|
||||
$headers = array(); |
||||
foreach ($map as $key => $value) { |
||||
$headers[] = $value; |
||||
} |
||||
parent::put($headers); |
||||
} |
||||
|
||||
} |
@ -1,170 +0,0 @@ |
||||
<?php |
||||
|
||||
/** |
||||
* Read cvs data from a stream - string/FileReader. |
||||
* |
||||
* Returns data as associative arrays (headers are the keys of the array). |
||||
* Skip blank lines ?? is it such a good idea? |
||||
* |
||||
* Usage: |
||||
* |
||||
* $reader = CsvReader::create('path'); |
||||
* foreach($reader as $items){ |
||||
* foreach($items as $key=>$value){ |
||||
* echo "$key : $value"; |
||||
* } |
||||
* } |
||||
* |
||||
* |
||||
* |
||||
* @copyright (c) 2012 University of Geneva |
||||
* @license GNU General Public License - http://www.gnu.org/copyleft/gpl.html |
||||
* @author Laurent Opprecht <laurent@opprecht.info> |
||||
*/ |
||||
class CsvReader implements Iterator |
||||
{ |
||||
|
||||
/** |
||||
* |
||||
* @param string|FileReader $stream |
||||
* @param string $delimiter |
||||
* @param string $enclosure |
||||
* @return CsvReader |
||||
*/ |
||||
static function create($stream, $delimiter = ';', $enclosure = '"') |
||||
{ |
||||
return new self($stream, $delimiter, $enclosure); |
||||
} |
||||
|
||||
protected $stream = null; |
||||
protected $headers = array(); |
||||
protected $delimiter = ''; |
||||
protected $enclosure = ''; |
||||
protected $current = false; |
||||
protected $index = -1; |
||||
|
||||
function __construct($stream, $delimiter = ';', $enclosure = '"') |
||||
{ |
||||
$this->stream = $stream; |
||||
$this->delimiter = $delimiter ? substr($delimiter, 0, 1) : ';'; |
||||
$this->enclosure = $enclosure ? substr($enclosure, 0, 1) : '"'; |
||||
} |
||||
|
||||
function get_delimiter() |
||||
{ |
||||
return $this->delimiter; |
||||
} |
||||
|
||||
function get_enclosure() |
||||
{ |
||||
return $this->enclosure; |
||||
} |
||||
|
||||
function headers() |
||||
{ |
||||
return $this->headers; |
||||
} |
||||
|
||||
/** |
||||
* @return FileReader |
||||
*/ |
||||
function stream() |
||||
{ |
||||
if (is_string($this->stream)) { |
||||
$this->stream = new FileReader($this->stream); |
||||
} |
||||
return $this->stream; |
||||
} |
||||
|
||||
protected function decode($line) |
||||
{ |
||||
if (empty($line)) { |
||||
return array(); |
||||
} |
||||
$data = api_str_getcsv($line, $this->get_delimiter(), $this->get_enclosure()); |
||||
if ($this->headers) { |
||||
$result = array(); |
||||
foreach ($data as $index => $value) { |
||||
$key = isset($this->headers[$index]) ? $this->headers[$index] : false; |
||||
if ($key) { |
||||
$result[$key] = $value; |
||||
} else { |
||||
$result[] = $value; |
||||
} |
||||
} |
||||
} else { |
||||
$result = $data; |
||||
} |
||||
return $result; |
||||
} |
||||
|
||||
/** |
||||
* Returns the next non empty line |
||||
* |
||||
* @return boolean|string |
||||
*/ |
||||
protected function next_line() |
||||
{ |
||||
while (true) { |
||||
$line = $this->stream()->next(); |
||||
if ($line === false) { |
||||
return false; |
||||
} else if ($line) { |
||||
return $line; |
||||
} |
||||
} |
||||
return false; |
||||
} |
||||
|
||||
public function current() |
||||
{ |
||||
return $this->current; |
||||
} |
||||
|
||||
public function key() |
||||
{ |
||||
return $this->index; |
||||
} |
||||
|
||||
public function next() |
||||
{ |
||||
if (empty($this->headers)) { |
||||
$line = $this->next_line(); |
||||
$this->headers = $this->decode($line); |
||||
} |
||||
$line = $this->next_line(); |
||||
if ($line) { |
||||
$this->current = $this->decode($line); |
||||
$this->index++; |
||||
} else { |
||||
$this->current = false; |
||||
} |
||||
return $this->current; |
||||
} |
||||
|
||||
public function rewind() |
||||
{ |
||||
$this->stream()->rewind(); |
||||
$line = $this->stream()->current(); |
||||
if (empty($line)) { |
||||
$line = $this->next_line(); |
||||
} |
||||
$this->headers = $this->decode($line); |
||||
$this->index = -1; |
||||
$this->next(); |
||||
} |
||||
|
||||
public function valid() |
||||
{ |
||||
return $this->current !== false; |
||||
} |
||||
|
||||
function __clone() |
||||
{ |
||||
$this->stream()->rewind(); |
||||
$this->current = false; |
||||
$this->index = -1; |
||||
$this->headers = array(); |
||||
} |
||||
|
||||
} |
@ -1,100 +0,0 @@ |
||||
<?php |
||||
|
||||
/** |
||||
* Write array data to a stream in CSV format. Usage: |
||||
* |
||||
* $writer = CsvWriter::create('path'); |
||||
* |
||||
* $writer->put($headers); |
||||
* $writer->put($line_1); |
||||
* $writer->put($line_2); |
||||
* |
||||
* @copyright (c) 2012 University of Geneva |
||||
* @license GNU General Public License - http://www.gnu.org/copyleft/gpl.html |
||||
* @author Laurent Opprecht <laurent@opprecht.info> |
||||
*/ |
||||
class CsvWriter |
||||
{ |
||||
|
||||
/** |
||||
* |
||||
* @param string|object $stream |
||||
* @return CsvWriter |
||||
*/ |
||||
static function create($stream, $delimiter = ';', $enclosure = '"') |
||||
{ |
||||
return new self($stream, $delimiter, $enclosure); |
||||
} |
||||
|
||||
protected $stream = null; |
||||
protected $delimiter = ''; |
||||
protected $enclosure = ''; |
||||
|
||||
function __construct($stream, $delimiter = ';', $enclosure = '"') |
||||
{ |
||||
$this->stream = $stream; |
||||
$this->delimiter = $delimiter ? substr($delimiter, 0, 1) : ';';; |
||||
$this->enclosure = $enclosure ? substr($enclosure, 0, 1) : '"';; |
||||
} |
||||
|
||||
function get_delimiter() |
||||
{ |
||||
return $this->delimiter; |
||||
} |
||||
|
||||
function get_enclosure() |
||||
{ |
||||
return $this->enclosure; |
||||
} |
||||
|
||||
function get_stream(){ |
||||
return $this->stream; |
||||
} |
||||
|
||||
/** |
||||
* |
||||
* @return FileWriter |
||||
*/ |
||||
protected function stream() |
||||
{ |
||||
if (is_string($this->stream)) { |
||||
$this->stream = new FileWriter($this->stream); |
||||
} |
||||
return $this->stream; |
||||
} |
||||
|
||||
function write($items) |
||||
{ |
||||
$items = is_array($items) ? $items : func_get_args(); |
||||
$this->put($items); |
||||
} |
||||
|
||||
function writeln($items) |
||||
{ |
||||
$items = is_array($items) ? $items : func_get_args(); |
||||
$this->put($items); |
||||
} |
||||
|
||||
function put($items) |
||||
{ |
||||
$items = is_array($items) ? $items : func_get_args(); |
||||
$enclosure = $this->enclosure; |
||||
$fields = array(); |
||||
foreach ($items as $item) { |
||||
$fields[] = $enclosure . str_replace($enclosure, $enclosure . $enclosure, $item) . $enclosure; |
||||
} |
||||
|
||||
$delimiter = $this->delimiter; |
||||
$line = implode($delimiter, $fields); |
||||
$this->stream()->writeln($line); |
||||
} |
||||
|
||||
function close() |
||||
{ |
||||
if (is_object($this->stream)) { |
||||
$this->stream->close(); |
||||
} |
||||
$this->stream = null; |
||||
} |
||||
|
||||
} |
@ -1,182 +0,0 @@ |
||||
<?php |
||||
|
||||
/** |
||||
* Read text from a file. Reader is line oriented and not char oriented. |
||||
* The default converter converts from the file encoding - auto-detected - to |
||||
* system encoding. |
||||
* |
||||
* Usage: |
||||
* |
||||
* $file = FileReader::create('path'); |
||||
* foreach($file as $line) |
||||
* { |
||||
* ... |
||||
* } |
||||
* |
||||
* @copyright (c) 2012 University of Geneva |
||||
* @license GNU General Public License - http://www.gnu.org/copyleft/gpl.html |
||||
* @author Laurent Opprecht <laurent@opprecht.info> |
||||
*/ |
||||
class FileReader implements Iterator |
||||
{ |
||||
|
||||
const EOL = "\n"; |
||||
|
||||
/** |
||||
* |
||||
* @param string $path |
||||
* @return FileReader |
||||
*/ |
||||
static function create($path, $converter = null) |
||||
{ |
||||
return new self($path, $converter); |
||||
} |
||||
|
||||
/** |
||||
* Returns the file encoding |
||||
* |
||||
* @return Encoding |
||||
*/ |
||||
static function detect_encoding($path) |
||||
{ |
||||
$abstract = array(); |
||||
// We assume that 200 lines are enough for encoding detection. |
||||
// here we must get at the raw data so we don't use other functions |
||||
// it's not possible to read x chars as this would not be safe with utf |
||||
// (chars may be split in the middle) |
||||
$handle = fopen($path, 'r'); |
||||
|
||||
$i = 0; |
||||
while (($line = fgets($handle)) !== false && $i < 200) { |
||||
$i++; |
||||
$abstract[] = $line; |
||||
} |
||||
fclose($handle); |
||||
$abstract = implode($abstract); |
||||
return Encoding::detect_encoding($abstract); |
||||
} |
||||
|
||||
protected $path = ''; |
||||
protected $handle = null; |
||||
protected $current = false; |
||||
protected $index = -1; |
||||
protected $converter = null; |
||||
|
||||
function __construct($path, $converter = null) |
||||
{ |
||||
if (empty($converter)) { |
||||
$encoding = self::detect_encoding($path); |
||||
$converter = $encoding->decoder(); |
||||
} |
||||
$this->path = $path; |
||||
$this->converter = $converter; |
||||
} |
||||
|
||||
/** |
||||
* |
||||
* @return Converter |
||||
*/ |
||||
function get_converter() |
||||
{ |
||||
return $this->converter; |
||||
} |
||||
|
||||
function handle() |
||||
{ |
||||
if (is_null($this->handle)) { |
||||
$this->handle = fopen($this->path, 'r'); |
||||
} |
||||
return $this->handle; |
||||
} |
||||
|
||||
/** |
||||
* Read at most $count lines. |
||||
* |
||||
* @param int $count |
||||
* @return array |
||||
*/ |
||||
function read_lines($count) |
||||
{ |
||||
$result; |
||||
$i = 0; |
||||
foreach ($this as $line) { |
||||
if ($i >= $count) { |
||||
return $result; |
||||
} |
||||
$i++; |
||||
$result[] = $line; |
||||
} |
||||
return $result; |
||||
} |
||||
|
||||
function read_line() |
||||
{ |
||||
return $this->next(); |
||||
} |
||||
|
||||
function close() |
||||
{ |
||||
if (is_resource($this->handle)) { |
||||
fclose($this->handle); |
||||
} |
||||
$this->handle = null; |
||||
} |
||||
|
||||
protected function convert($text) |
||||
{ |
||||
return $this->converter->convert($text); |
||||
} |
||||
|
||||
public function current() |
||||
{ |
||||
return $this->current; |
||||
} |
||||
|
||||
public function key() |
||||
{ |
||||
return $this->index; |
||||
} |
||||
|
||||
public function next() |
||||
{ |
||||
$handle = $this->handle(); |
||||
if($handle === false) |
||||
{ |
||||
$this->current = false; |
||||
return false; |
||||
} |
||||
$line = fgets($handle); |
||||
if ($line !== false) { |
||||
$line = rtrim($line, "\r\n"); |
||||
$line = $this->convert($line); |
||||
$this->index++; |
||||
} |
||||
$this->current = $line; |
||||
return $this->current; |
||||
} |
||||
|
||||
public function rewind() |
||||
{ |
||||
$this->converter->reset(); |
||||
if ($handle = $this->handle()) { |
||||
rewind($handle); |
||||
} |
||||
$this->current = false; |
||||
$this->index = -1; |
||||
$this->next(); |
||||
} |
||||
|
||||
public function valid() |
||||
{ |
||||
return $this->current !== false; |
||||
} |
||||
|
||||
function __clone() |
||||
{ |
||||
$this->handle = null; |
||||
$this->current = false; |
||||
$this->index = -1; |
||||
$this->converter->reset(); |
||||
} |
||||
|
||||
} |
@ -1,81 +0,0 @@ |
||||
<?php |
||||
|
||||
/** |
||||
* Write data to file. Default to UTF8 encoding. |
||||
* |
||||
* @copyright (c) 2012 University of Geneva |
||||
* @license GNU General Public License - http://www.gnu.org/copyleft/gpl.html |
||||
* @author Laurent Opprecht <laurent@opprecht.info> |
||||
*/ |
||||
class FileWriter |
||||
{ |
||||
|
||||
/** |
||||
* |
||||
* @param string $path |
||||
* @param Converter $converter |
||||
* @return FileWriter |
||||
*/ |
||||
static function create($path, $converter = null) |
||||
{ |
||||
return new self($path, $converter); |
||||
} |
||||
|
||||
const EOL = "\n"; |
||||
|
||||
protected $path = ''; |
||||
protected $handle = null; |
||||
protected $converter = null; |
||||
|
||||
/** |
||||
* |
||||
* @param string $path |
||||
* @param Encoding $encoding |
||||
*/ |
||||
function __construct($path, $converter = null) |
||||
{ |
||||
$this->path = $path; |
||||
$this->converter = $converter ? $converter : Encoding::utf8()->encoder(); |
||||
} |
||||
|
||||
/** |
||||
* |
||||
* @return Converter |
||||
*/ |
||||
function get_converter() |
||||
{ |
||||
return $this->converter; |
||||
} |
||||
|
||||
protected function handle() |
||||
{ |
||||
if (is_null($this->handle)) { |
||||
$this->handle = fopen($this->path, 'a+'); |
||||
} |
||||
return $this->handle; |
||||
} |
||||
|
||||
function write($text) |
||||
{ |
||||
fwrite($this->handle(), $this->convert($text)); |
||||
} |
||||
|
||||
function writeln($text) |
||||
{ |
||||
fwrite($this->handle(), $this->convert($text) . self::EOL); |
||||
} |
||||
|
||||
function close() |
||||
{ |
||||
if (is_resource($this->handle)) { |
||||
fclose($this->handle); |
||||
} |
||||
$this->handle = null; |
||||
} |
||||
|
||||
protected function convert($text) |
||||
{ |
||||
return $this->converter->convert($text); |
||||
} |
||||
|
||||
} |
Loading…
Reference in new issue