summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorIgnace Nyamagana Butera <nyamsprod@gmail.com>2014-08-27 11:02:21 +0200
committerIgnace Nyamagana Butera <nyamsprod@gmail.com>2014-08-27 11:02:21 +0200
commitdce2d0f3ae6b09e6e64186a566e5e55d39769362 (patch)
tree35567ccf7e146072346a75fb8956d1e8e50a88ec /src
parent85f17cf5eb111ddf61cc4f88bd5fe77eeafb462b (diff)
downloadcsv-dce2d0f3ae6b09e6e64186a566e5e55d39769362.zip
csv-dce2d0f3ae6b09e6e64186a566e5e55d39769362.tar.gz
csv-dce2d0f3ae6b09e6e64186a566e5e55d39769362.tar.bz2
internals improved
Diffstat (limited to 'src')
-rw-r--r--src/AbstractCsv.php48
-rw-r--r--src/Config/Controls.php14
-rw-r--r--src/Config/StreamFilter.php30
-rw-r--r--src/Reader.php48
-rw-r--r--src/Writer.php180
5 files changed, 160 insertions, 160 deletions
diff --git a/src/AbstractCsv.php b/src/AbstractCsv.php
index 8e39ff5..157832e 100644
--- a/src/AbstractCsv.php
+++ b/src/AbstractCsv.php
@@ -86,6 +86,28 @@ abstract class AbstractCsv implements JsonSerializable, IteratorAggregate
}
/**
+ * Return a normalize path which could be a SplFileObject
+ * or a string path
+ *
+ * @param object|string $path the filepath
+ *
+ * @return \SplFileObject|string
+ */
+ protected function normalizePath($path)
+ {
+ if ($path instanceof SplFileObject) {
+ return $path;
+ } elseif ($path instanceof SplFileInfo) {
+ return $path->getPath().'/'.$path->getBasename();
+ }
+
+ $path = (string) $path;
+ $path = trim($path);
+
+ return $path;
+ }
+
+ /**
* The destructor
*/
public function __destruct()
@@ -114,8 +136,8 @@ abstract class AbstractCsv implements JsonSerializable, IteratorAggregate
* ?>
* ```
*
- * @param \SplFileInfo|\SplFileObject|object|string $path file path
- * @param string $open_mode the file open mode flag
+ * @param object|string $path file path
+ * @param string $open_mode the file open mode flag
*
* @return static
*
@@ -185,28 +207,6 @@ abstract class AbstractCsv implements JsonSerializable, IteratorAggregate
}
/**
- * Return a normalize path which could be a SplFileObject
- * or a string path
- *
- * @param object|string $path the filepath
- *
- * @return \SplFileObject|string
- */
- protected function normalizePath($path)
- {
- if ($path instanceof SplFileObject) {
- return $path;
- } elseif ($path instanceof SplFileInfo) {
- return $path->getPath().'/'.$path->getBasename();
- }
-
- $path = (string) $path;
- $path = trim($path);
-
- return $path;
- }
-
- /**
* Create a {@link AbstractCsv} instance from another {@link AbstractCsv} object
*
* @param string $class_name the class to be instantiated
diff --git a/src/Config/Controls.php b/src/Config/Controls.php
index 731cd1e..b09713b 100644
--- a/src/Config/Controls.php
+++ b/src/Config/Controls.php
@@ -55,6 +55,13 @@ trait Controls
protected $flags = SplFileObject::READ_CSV;
/**
+ * return a SplFileOjbect
+ *
+ * @return \SplFileOjbect
+ */
+ abstract public function getIterator();
+
+ /**
* set the field delimeter
*
* @param string $delimiter
@@ -138,13 +145,6 @@ trait Controls
}
/**
- * return a SplFileOjbect
- *
- * @return \SplFileOjbect
- */
- abstract public function getIterator();
-
- /**
* set the field enclosure
*
* @param string $enclosure
diff --git a/src/Config/StreamFilter.php b/src/Config/StreamFilter.php
index 57b0ece..c8a32d5 100644
--- a/src/Config/StreamFilter.php
+++ b/src/Config/StreamFilter.php
@@ -164,21 +164,6 @@ trait StreamFilter
}
/**
- * Sanitize the stream filter name
- *
- * @param string $filter_name the stream filter name
- *
- * @return string
- */
- protected function sanitizeStreamFilter($filter_name)
- {
- $this->assertStreamable();
- $filter_name = (string) $filter_name;
-
- return trim($filter_name);
- }
-
- /**
* append a stream filter
*
* @param string $filter_name a string or an object that implements the '__toString' method
@@ -209,6 +194,21 @@ trait StreamFilter
}
/**
+ * Sanitize the stream filter name
+ *
+ * @param string $filter_name the stream filter name
+ *
+ * @return string
+ */
+ protected function sanitizeStreamFilter($filter_name)
+ {
+ $this->assertStreamable();
+ $filter_name = (string) $filter_name;
+
+ return trim($filter_name);
+ }
+
+ /**
* Detect if the stream filter is already present
*
* @param string $filter_name
diff --git a/src/Reader.php b/src/Reader.php
index 5f4ec20..9a0e810 100644
--- a/src/Reader.php
+++ b/src/Reader.php
@@ -38,30 +38,6 @@ class Reader extends AbstractCsv
protected $stream_filter_mode = STREAM_FILTER_READ;
/**
- * Intelligent Array Combine
- *
- * add or remove values from the $value array to
- * match array $keys length before using PHP array_combine function
- *
- * @param array $keys
- * @param array $value
- *
- * @return array
- */
- protected static function combineArray(array $keys, array $value)
- {
- $nbKeys = count($keys);
- $diff = $nbKeys - count($value);
- if ($diff > 0) {
- $value = array_merge($value, array_fill(0, $diff, null));
- } elseif ($diff < 0) {
- $value = array_slice($value, 0, $nbKeys);
- }
-
- return array_combine($keys, $value);
- }
-
- /**
* Return a Filtered Iterator
*
* @param callable $callable a callable function to be applied to each Iterator item
@@ -178,6 +154,30 @@ class Reader extends AbstractCsv
}
/**
+ * Intelligent Array Combine
+ *
+ * add or remove values from the $value array to
+ * match array $keys length before using PHP array_combine function
+ *
+ * @param array $keys
+ * @param array $value
+ *
+ * @return array
+ */
+ protected static function combineArray(array $keys, array $value)
+ {
+ $nbKeys = count($keys);
+ $diff = $nbKeys - count($value);
+ if ($diff > 0) {
+ $value = array_merge($value, array_fill(0, $diff, null));
+ } elseif ($diff < 0) {
+ $value = array_slice($value, 0, $nbKeys);
+ }
+
+ return array_combine($keys, $value);
+ }
+
+ /**
* Return a single column from the CSV data
*
* The callable function will be applied to each value to be return
diff --git a/src/Writer.php b/src/Writer.php
index 1612f21..7958e41 100644
--- a/src/Writer.php
+++ b/src/Writer.php
@@ -76,19 +76,10 @@ class Writer extends AbstractCsv
protected $csv;
/**
- * The destructor
- */
- public function __destruct()
- {
- $this->csv = null;
- parent::__destruct();
- }
-
- /**
* Tell the class how to handle null value
*
* @param integer $value a Writer null behavior constant
- *
+ *vi
* @return self
*
* @throws OutOfBoundsException If the Integer is not valid
@@ -114,36 +105,6 @@ class Writer extends AbstractCsv
}
/**
- * Tells whether the stream filter capabilities can be used
- *
- * @return boolean
- */
- public function isActiveStreamFilter()
- {
- return parent::isActiveStreamFilter() && is_null($this->csv);
- }
-
- /**
- * Format the row according to the null handling behavior
- *
- * @param array $row
- *
- * @return array
- */
- protected function sanitizeColumnsContent(array $row)
- {
- if (self::NULL_AS_EXCEPTION == $this->null_handling_mode) {
- return $row;
- } elseif (self::NULL_AS_EMPTY == $this->null_handling_mode) {
- return str_replace(null, '', $row);
- }
-
- return array_filter($row, function ($value) {
- return !is_null($value);
- });
- }
-
- /**
* Set Inserted row column count
*
* @param integer $value
@@ -188,24 +149,53 @@ class Writer extends AbstractCsv
}
/**
- * Check column count consistency
+ * Add multiple lines to the CSV your are generating
*
- * @param array $row the row to be added to the CSV
+ * a simple helper/Wrapper method around insertOne
*
- * @return boolean
+ * @param \Traversable|array $rows a multidimentional array or a Traversable object
+ *
+ * @return self
+ *
+ * @throws \InvalidArgumentException If the given rows format is invalid
*/
- protected function isColumnsCountConsistent(array $row)
+ public function insertAll($rows)
{
- if ($this->detect_columns_count) {
- $this->columns_count = count($row);
- $this->detect_columns_count = false;
+ if (! is_array($rows) && ! $rows instanceof Traversable) {
+ throw new InvalidArgumentException(
+ 'the provided data must be an array OR a \Traversable object'
+ );
+ }
- return true;
- } elseif (-1 == $this->columns_count) {
- return true;
+ foreach ($rows as $row) {
+ $this->insertOne($row);
}
- return count($row) == $this->columns_count;
+ return $this;
+ }
+
+ /**
+ * Add a new CSV row to the generated CSV
+ *
+ * @param string[]|string $data a string, an array or an object implementing to '__toString' method
+ *
+ * @return self
+ *
+ * @throws \InvalidArgumentException If the given row is invalid
+ */
+ public function insertOne($data)
+ {
+ $data = $this->validateRow($data);
+ $data = $this->sanitizeColumnsContent($data);
+ if (! $this->isColumnsCountConsistent($data)) {
+ throw new RuntimeException(
+ 'You are trying to add '.count($data).' columns to a CSV
+ that requires '.$this->columns_count.' columns per row.'
+ );
+ }
+ $this->getCsv()->fputcsv($data, $this->delimiter, $this->enclosure);
+
+ return $this;
}
/**
@@ -251,6 +241,47 @@ class Writer extends AbstractCsv
}
/**
+ * Format the row according to the null handling behavior
+ *
+ * @param array $row
+ *
+ * @return array
+ */
+ protected function sanitizeColumnsContent(array $row)
+ {
+ if (self::NULL_AS_EXCEPTION == $this->null_handling_mode) {
+ return $row;
+ } elseif (self::NULL_AS_EMPTY == $this->null_handling_mode) {
+ return str_replace(null, '', $row);
+ }
+
+ return array_filter($row, function ($value) {
+ return !is_null($value);
+ });
+ }
+
+ /**
+ * Check column count consistency
+ *
+ * @param array $row the row to be added to the CSV
+ *
+ * @return boolean
+ */
+ protected function isColumnsCountConsistent(array $row)
+ {
+ if ($this->detect_columns_count) {
+ $this->columns_count = count($row);
+ $this->detect_columns_count = false;
+
+ return true;
+ } elseif (-1 == $this->columns_count) {
+ return true;
+ }
+
+ return count($row) == $this->columns_count;
+ }
+
+ /**
* set the csv container as a SplFileObject instance
* insure we use the same object for insertion to
* avoid loosing the cursor position
@@ -268,52 +299,21 @@ class Writer extends AbstractCsv
}
/**
- * Add a new CSV row to the generated CSV
- *
- * @param string[]|string $data a string, an array or an object implementing to '__toString' method
- *
- * @return self
+ * Tells whether the stream filter capabilities can be used
*
- * @throws \InvalidArgumentException If the given row is invalid
+ * @return boolean
*/
- public function insertOne($data)
+ public function isActiveStreamFilter()
{
- $data = $this->validateRow($data);
- $data = $this->sanitizeColumnsContent($data);
- if (! $this->isColumnsCountConsistent($data)) {
- throw new RuntimeException(
- 'You are trying to add '.count($data).' columns to a CSV
- that requires '.$this->columns_count.' columns per row.'
- );
- }
- $this->getCsv()->fputcsv($data, $this->delimiter, $this->enclosure);
-
- return $this;
+ return parent::isActiveStreamFilter() && is_null($this->csv);
}
/**
- * Add multiple lines to the CSV your are generating
- *
- * a simple helper/Wrapper method around insertOne
- *
- * @param \Traversable|array $rows a multidimentional array or a Traversable object
- *
- * @return self
- *
- * @throws \InvalidArgumentException If the given rows format is invalid
+ * The destructor
*/
- public function insertAll($rows)
+ public function __destruct()
{
- if (! is_array($rows) && ! $rows instanceof Traversable) {
- throw new InvalidArgumentException(
- 'the provided data must be an array OR a \Traversable object'
- );
- }
-
- foreach ($rows as $row) {
- $this->insertOne($row);
- }
-
- return $this;
+ $this->csv = null;
+ parent::__destruct();
}
}