summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorignace nyamagana butera <nyamsprod@gmail.com>2015-11-26 15:02:49 +0100
committerignace nyamagana butera <nyamsprod@gmail.com>2015-11-26 15:02:49 +0100
commit54e3ce2d8623e6b7f27a2918a494e3cb0990b00f (patch)
tree5eb00576a335372cdf0a01ee33df16da506a94fe /src
parent225712ec5272526aaca729f85d44030269d51849 (diff)
downloadcsv-54e3ce2d8623e6b7f27a2918a494e3cb0990b00f.zip
csv-54e3ce2d8623e6b7f27a2918a494e3cb0990b00f.tar.gz
csv-54e3ce2d8623e6b7f27a2918a494e3cb0990b00f.tar.bz2
Improve Internal code
Diffstat (limited to 'src')
-rw-r--r--src/Config/Controls.php9
-rw-r--r--src/Exception/InvalidRowException.php4
-rw-r--r--src/Modifier/QueryFilter.php32
-rw-r--r--src/Reader.php51
-rw-r--r--src/Writer.php5
5 files changed, 39 insertions, 62 deletions
diff --git a/src/Config/Controls.php b/src/Config/Controls.php
index 9ab7c80..b54d74f 100644
--- a/src/Config/Controls.php
+++ b/src/Config/Controls.php
@@ -48,7 +48,7 @@ trait Controls
protected $escape = '\\';
/**
- * the \SplFileObject flags holder
+ * the SplFileObject flags holder
*
* @var int
*/
@@ -111,16 +111,11 @@ trait Controls
* @param string[] $delimiters the delimiters to consider
* @param int $nb_rows Detection is made using $nb_rows of the CSV
*
- * @throws InvalidArgumentException If $nb_rows value is invalid
- *
* @return array
*/
public function fetchDelimitersOccurrence(array $delimiters, $nb_rows = 1)
{
- if (!($nb_rows = filter_var($nb_rows, FILTER_VALIDATE_INT, ['options' => ['min_range' => 1]]))) {
- throw new InvalidArgumentException('The number of rows to consider must be a valid positive integer');
- }
-
+ $nb_rows = $this->filterInteger($nb_rows, 1, 'The number of rows to consider must be a valid positive integer');
$filterRow = function ($row) {
return is_array($row) && count($row) > 1;
};
diff --git a/src/Exception/InvalidRowException.php b/src/Exception/InvalidRowException.php
index ef19830..896ca24 100644
--- a/src/Exception/InvalidRowException.php
+++ b/src/Exception/InvalidRowException.php
@@ -12,6 +12,8 @@
*/
namespace League\Csv\Exception;
+use InvalidArgumentException;
+
/**
* Thrown when a data is not validated prior to insertion
*
@@ -19,7 +21,7 @@ namespace League\Csv\Exception;
* @since 7.0.0
*
*/
-class InvalidRowException extends \InvalidArgumentException
+class InvalidRowException extends InvalidArgumentException
{
/**
* Validator which did not validated the data
diff --git a/src/Modifier/QueryFilter.php b/src/Modifier/QueryFilter.php
index 3fdb6e5..39eecaf 100644
--- a/src/Modifier/QueryFilter.php
+++ b/src/Modifier/QueryFilter.php
@@ -275,7 +275,7 @@ trait QueryFilter
*
* @param Iterator $iterator
*
- * @return \Iterator
+ * @return Iterator
*/
protected function applyBomStripping(Iterator $iterator)
{
@@ -327,9 +327,9 @@ trait QueryFilter
/**
* Filter the Iterator
*
- * @param \Iterator $iterator
+ * @param Iterator $iterator
*
- * @return \Iterator
+ * @return Iterator
*/
protected function applyIteratorFilter(Iterator $iterator)
{
@@ -344,9 +344,9 @@ trait QueryFilter
/**
* Sort the Iterator
*
- * @param \Iterator $iterator
+ * @param Iterator $iterator
*
- * @return \Iterator
+ * @return Iterator
*/
protected function applyIteratorInterval(Iterator $iterator)
{
@@ -364,9 +364,9 @@ trait QueryFilter
/**
* Sort the Iterator
*
- * @param \Iterator $iterator
+ * @param Iterator $iterator
*
- * @return \Iterator
+ * @return Iterator
*/
protected function applyIteratorSortBy(Iterator $iterator)
{
@@ -388,4 +388,22 @@ trait QueryFilter
return $obj->getIterator();
}
+
+ /**
+ * Convert the Iterator into an array depending on the selected return type
+ *
+ * @param int $type
+ * @param Iterator $iterator
+ * @param bool $use_keys Whether to use the iterator element keys as index
+ *
+ * @return Iterator|array
+ */
+ protected function applyReturnType($type, Iterator $iterator, $use_keys = true)
+ {
+ if (AbstractCsv::TYPE_ARRAY == $type) {
+ return iterator_to_array($iterator, $use_keys);
+ }
+
+ return $iterator;
+ }
}
diff --git a/src/Reader.php b/src/Reader.php
index 9c0dc1e..161ffa9 100644
--- a/src/Reader.php
+++ b/src/Reader.php
@@ -87,24 +87,6 @@ class Reader extends AbstractCsv
}
/**
- * Convert the Iterator into an array depending on the selected return type
- *
- * @param int $type
- * @param Iterator $iterator
- * @param bool $use_keys Whether to use the iterator element keys as index
- *
- * @return Iterator|array
- */
- protected function applyReturnType($type, Iterator $iterator, $use_keys = true)
- {
- if (self::TYPE_ARRAY == $type) {
- return iterator_to_array($iterator, $use_keys);
- }
-
- return $iterator;
- }
-
- /**
* Applies a callback function on the CSV
*
* The callback function must return TRUE in order to continue
@@ -158,18 +140,14 @@ class Reader extends AbstractCsv
*
* By default if no column index is provided the first column of the CSV is selected
*
- * @param int $columnIndex field Index
- * @param callable|null $callable a callable function to apply to each selected CSV rows column value.
- * The callable takes up to three parameter
- * - the column value
- * - the row index
- * - the CSV Iterator
+ * @param int $columnIndex CSV column index
+ * @param callable|null $callable A callable to be applied to each of the value to be returned.
*
* @return Iterator|array
*/
public function fetchColumn($columnIndex = 0, callable $callable = null)
{
- $this->assertValidColumnIndex($columnIndex);
+ $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);
@@ -188,20 +166,6 @@ class Reader extends AbstractCsv
}
/**
- * Validate a CSV row index
- *
- * @param int $index
- *
- * @throws InvalidArgumentException If the column index is not a positive integer or 0
- */
- protected function assertValidColumnIndex($index)
- {
- if (false === filter_var($index, FILTER_VALIDATE_INT, ['options' => ['min_range' => 0]])) {
- throw new InvalidArgumentException('the column index must be a positive integer or 0');
- }
- }
-
- /**
* Retrive CSV data as pairs
*
* Fetches an associative array of all rows as key-value pairs (first
@@ -219,8 +183,8 @@ class Reader extends AbstractCsv
*/
public function fetchPairs($offsetColumnIndex = 0, $valueColumnIndex = 1, callable $callable = null)
{
- $this->assertValidColumnIndex($offsetColumnIndex);
- $this->assertValidColumnIndex($valueColumnIndex);
+ $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);
};
@@ -300,10 +264,7 @@ class Reader extends AbstractCsv
return $offset_or_keys;
}
- if (false === filter_var($offset_or_keys, FILTER_VALIDATE_INT, ['options' => ['min_range' => 0]])) {
- throw new InvalidArgumentException('the row index must be a positive integer, 0 or a non empty array');
- }
-
+ $$offset_or_keys = $this->filterInteger($offset_or_keys, 0, 'the row index must be a positive integer, 0 or a non empty array');
$keys = $this->getRow($offset_or_keys);
$this->assertValidAssocKeys($keys);
$filterOutRow = function ($row, $rowIndex) use ($offset_or_keys) {
diff --git a/src/Writer.php b/src/Writer.php
index 879ef99..8fd94f8 100644
--- a/src/Writer.php
+++ b/src/Writer.php
@@ -15,6 +15,7 @@ namespace League\Csv;
use InvalidArgumentException;
use League\Csv\Modifier\RowFilter;
use ReflectionMethod;
+use SplFileObject;
use Traversable;
/**
@@ -36,7 +37,7 @@ class Writer extends AbstractCsv
/**
* The CSV object holder
*
- * @var \SplFileObject
+ * @var SplFileObject
*/
protected $csv;
@@ -89,7 +90,7 @@ class Writer extends AbstractCsv
{
if (!is_array($rows) && !$rows instanceof Traversable) {
throw new InvalidArgumentException(
- 'the provided data must be an array OR a \Traversable object'
+ 'the provided data must be an array OR a `Traversable` object'
);
}