summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorignace nyamagana butera <nyamsprod@gmail.com>2015-11-26 17:32:40 +0100
committerignace nyamagana butera <nyamsprod@gmail.com>2015-11-26 17:32:40 +0100
commit0274c0fd79e0ba4ed98de3b0c4fc383aae014e63 (patch)
treeb84a19f1cbc5a8b9e51dcb449562fb3b6063b46a /src
parent57d94eeeb939c91a6fd035235b7711e61d9f5c08 (diff)
downloadcsv-0274c0fd79e0ba4ed98de3b0c4fc383aae014e63.zip
csv-0274c0fd79e0ba4ed98de3b0c4fc383aae014e63.tar.gz
csv-0274c0fd79e0ba4ed98de3b0c4fc383aae014e63.tar.bz2
Improve internal code
Diffstat (limited to 'src')
-rw-r--r--src/Modifier/RowFilter.php20
-rw-r--r--src/Modifier/StreamFilter.php4
-rw-r--r--src/Reader.php4
3 files changed, 18 insertions, 10 deletions
diff --git a/src/Modifier/RowFilter.php b/src/Modifier/RowFilter.php
index 1a72f81..7bf3592 100644
--- a/src/Modifier/RowFilter.php
+++ b/src/Modifier/RowFilter.php
@@ -100,12 +100,19 @@ trait RowFilter
*/
public function addValidator(callable $callable, $name)
{
+ $name = $this->validateString($name);
+
$this->validators[$name] = $callable;
return $this;
}
/**
+ * @inheritdoc
+ */
+ abstract protected function validateString($str);
+
+ /**
* Remove a validator from the collection
*
* @param string $name the validator name
@@ -114,9 +121,8 @@ trait RowFilter
*/
public function removeValidator($name)
{
- if (array_key_exists($name, $this->validators)) {
- unset($this->validators[$name]);
- }
+ $name = $this->validateString($name);
+ unset($this->validators[$name]);
return $this;
}
@@ -130,7 +136,9 @@ trait RowFilter
*/
public function hasValidator($name)
{
- return array_key_exists($name, $this->validators);
+ $name = $this->validateString($name);
+
+ return isset($this->validators[$name]);
}
/**
@@ -148,7 +156,7 @@ trait RowFilter
/**
* Format the given row
*
- * @param array|string $row
+ * @param array $row
*
* @return array
*/
@@ -162,7 +170,7 @@ trait RowFilter
}
/**
- * validate a row
+ * Validate a row
*
* @param array $row
*
diff --git a/src/Modifier/StreamFilter.php b/src/Modifier/StreamFilter.php
index 513a631..caf0c76 100644
--- a/src/Modifier/StreamFilter.php
+++ b/src/Modifier/StreamFilter.php
@@ -71,13 +71,13 @@ trait StreamFilter
protected function initStreamFilter($path)
{
$this->stream_filters = [];
- if (! is_string($path)) {
+ if (!is_string($path)) {
$this->stream_uri = null;
return;
}
- if (! preg_match($this->stream_regex, $path, $matches)) {
+ if (!preg_match($this->stream_regex, $path, $matches)) {
$this->stream_uri = $path;
return;
diff --git a/src/Reader.php b/src/Reader.php
index f71c43f..140d959 100644
--- a/src/Reader.php
+++ b/src/Reader.php
@@ -150,7 +150,7 @@ class Reader extends AbstractCsv
$columnIndex = $this->filterInteger($columnIndex, 0, 'the column index must be a positive integer or 0');
$filterColumn = function ($row) use ($columnIndex) {
- return array_key_exists($columnIndex, $row);
+ return isset($row[$columnIndex]);
};
$selectColumn = function ($row) use ($columnIndex) {
@@ -186,7 +186,7 @@ class Reader extends AbstractCsv
$offsetColumnIndex = $this->filterInteger($offsetColumnIndex, 0, 'the offset column index must be a positive integer or 0');
$valueColumnIndex = $this->filterInteger($valueColumnIndex, 0, 'the value column index must be a positive integer or 0');
$filterPairs = function ($row) use ($offsetColumnIndex, $valueColumnIndex) {
- return array_key_exists($offsetColumnIndex, $row) && array_key_exists($valueColumnIndex, $row);
+ return isset($row[$offsetColumnIndex], $row[$valueColumnIndex]);
};
$selectPairs = function ($row) use ($offsetColumnIndex, $valueColumnIndex) {
return [$row[$offsetColumnIndex], $row[$valueColumnIndex]];