summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorIgnace Nyamagana Butera <nyamsprod@gmail.com>2015-02-20 10:10:10 +0100
committerIgnace Nyamagana Butera <nyamsprod@gmail.com>2015-02-20 10:10:10 +0100
commit1ba1cdde337df1c0edfa8960cb45f077258cf8bf (patch)
tree42a674e6bbf2b578163e5f96f0982eddd01e4620 /src
parent42169da9f32996b274bc0d125974660ee999c6cf (diff)
downloadcsv-1ba1cdde337df1c0edfa8960cb45f077258cf8bf.zip
csv-1ba1cdde337df1c0edfa8960cb45f077258cf8bf.tar.gz
csv-1ba1cdde337df1c0edfa8960cb45f077258cf8bf.tar.bz2
improve rowfilter trait
Diffstat (limited to 'src')
-rw-r--r--src/Modifier/RowFilter.php38
-rw-r--r--src/Writer.php42
2 files changed, 41 insertions, 39 deletions
diff --git a/src/Modifier/RowFilter.php b/src/Modifier/RowFilter.php
index b0aec16..8a0bf1f 100644
--- a/src/Modifier/RowFilter.php
+++ b/src/Modifier/RowFilter.php
@@ -12,6 +12,8 @@
*/
namespace League\Csv\Modifier;
+use League\Csv\Exception\InvalidRowException;
+
/**
* Trait to format and validate the row before insertion
*
@@ -148,4 +150,40 @@ trait RowFilter
return $this;
}
+
+ /**
+ * Format the given row
+ *
+ * @param array|string $row
+ *
+ * @return array
+ */
+ protected function formatRow(array $row)
+ {
+ foreach ($this->formatters as $formatter) {
+ $row = $formatter($row);
+ }
+
+ return $row;
+ }
+
+ /**
+ * validate a row
+ *
+ * @param array $row
+ *
+ * @throws \League\Csv\Exception\InvalidRowException If the validation failed
+ *
+ * @return void
+ */
+ protected function validateRow(array $row)
+ {
+ foreach ($this->validators as $name => $validator) {
+ if (true !== $validator($row)) {
+ throw new InvalidRowException($name, $row, 'row validation failed');
+ }
+ }
+ }
+
+
}
diff --git a/src/Writer.php b/src/Writer.php
index 5e53033..1145604 100644
--- a/src/Writer.php
+++ b/src/Writer.php
@@ -13,7 +13,6 @@
namespace League\Csv;
use InvalidArgumentException;
-use League\Csv\Exception\InvalidRowException;
use League\Csv\Modifier;
use Traversable;
@@ -78,6 +77,9 @@ class Writer extends AbstractCsv
*/
public function insertOne($row)
{
+ if (! is_array($row)) {
+ $row = str_getcsv($row, $this->delimiter, $this->enclosure, $this->escape);
+ }
$row = $this->formatRow($row);
$this->validateRow($row);
$csv = $this->getCsv();
@@ -91,44 +93,6 @@ class Writer extends AbstractCsv
}
/**
- * Format the given row
- *
- * @param array|string $row
- *
- * @return array
- */
- protected function formatRow($row)
- {
- if (! is_array($row)) {
- $row = str_getcsv($row, $this->delimiter, $this->enclosure, $this->escape);
- }
-
- foreach ($this->formatters as $formatter) {
- $row = $formatter($row);
- }
-
- return $row;
- }
-
- /**
- * validate a row
- *
- * @param array $row
- *
- * @throws \League\Csv\Exception\InvalidRowException If the validation failed
- *
- * @return void
- */
- protected function validateRow(array $row)
- {
- foreach ($this->validators as $name => $validator) {
- if (true !== $validator($row)) {
- throw new InvalidRowException($name, $row, 'row validation failed');
- }
- }
- }
-
- /**
* set the csv container as a SplFileObject instance
* insure we use the same object for insertion to
* avoid loosing the cursor position