small code improvements

This commit is contained in:
Susann Sgorzaly
2018-02-22 21:34:39 +01:00
parent 7a3120dd28
commit 958af1027e

View File

@@ -332,13 +332,35 @@ class Csv {
* Constructor * Constructor
* Class constructor * Class constructor
* *
* @param string|null $input The CSV string or a direct filepath * @param string|null $input The CSV string or a direct filepath
* @param integer|null $offset Number of rows to ignore from the beginning of the data * @param integer|null $offset Number of rows to ignore from the beginning
* @param integer|null $limit Limits the number of returned rows to specified amount * of the data
* @param string|null $conditions Basic SQL-like conditions for row matching * @param integer|null $limit Limits the number of returned rows to
* @param null|true $keep_file_data Keep raw file data in memory after successful parsing (useful for debugging) * specified amount
* @param string|null $conditions Basic SQL-like conditions for row
* matching
* @param null|true $keep_file_data Keep raw file data in memory after
* successful parsing (useful for debugging)
*/ */
public function __construct($input = null, $offset = null, $limit = null, $conditions = null, $keep_file_data = null) { public function __construct($input = NULL, $offset = NULL, $limit = NULL, $conditions = NULL, $keep_file_data = NULL) {
$this->init($offset, $limit, $conditions, $keep_file_data);
if (!empty($input)) {
$this->parse($input);
}
}
/**
* @param integer|null $offset Number of rows to ignore from the beginning
* of the data
* @param integer|null $limit Limits the number of returned rows to
* specified amount
* @param string|null $conditions Basic SQL-like conditions for row
* matching
* @param null|true $keep_file_data Keep raw file data in memory after
* successful parsing (useful for debugging)
*/
public function init($offset = NULL, $limit = NULL, $conditions = NULL, $keep_file_data = NULL) {
if (!is_null($offset)) { if (!is_null($offset)) {
$this->offset = $offset; $this->offset = $offset;
} }
@@ -354,10 +376,6 @@ class Csv {
if (!is_null($keep_file_data)) { if (!is_null($keep_file_data)) {
$this->keep_file_data = $keep_file_data; $this->keep_file_data = $keep_file_data;
} }
if (!empty($input)) {
$this->parse($input);
}
} }
// ============================================== // ==============================================
@@ -375,37 +393,31 @@ class Csv {
* *
* @return bool True on success * @return bool True on success
*/ */
public function parse($input = null, $offset = null, $limit = null, $conditions = null) { public function parse($input = NULL, $offset = NULL, $limit = NULL, $conditions = NULL) {
if (is_null($input)) { if (!is_null($input)) {
$input = $this->file; $this->file = $input;
} }
if (!empty($input)) { if (empty($this->file)) {
if (!is_null($offset)) { // todo: but why true?
$this->offset = $offset; return true;
} }
if (!is_null($limit)) { $this->init($offset, $limit, $conditions);
$this->limit = $limit;
}
if (strlen($this->file) <= PHP_MAXPATHLEN && is_readable($this->file)) {
if (!is_null($conditions)) { $this->data = $this->parse_file($this->file);
$this->conditions = $conditions; }
} else {
$this->file_data = &$this->file;
if (strlen($input) <= PHP_MAXPATHLEN && is_readable($input)) { $this->data = $this->parse_string();
$this->data = $this->parse_file($input); }
} else {
$this->file_data = &$input; if ($this->data === false) {
$this->data = $this->parse_string(); return false;
}
if ($this->data === false) {
return false;
}
} }
return true;
} }
/** /**