summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rwxr-xr-xexamples/json.php1
-rwxr-xr-xexamples/writing.php1
-rw-r--r--src/Modifier/QueryFilter.php98
3 files changed, 51 insertions, 49 deletions
diff --git a/examples/json.php b/examples/json.php
index 17317d9..b86826e 100755
--- a/examples/json.php
+++ b/examples/json.php
@@ -7,7 +7,6 @@ require '../vendor/autoload.php';
$inputCsv = Reader::createFromPath('data/prenoms.csv');
$inputCsv->setDelimiter(';');
$inputCsv->setEncodingFrom('ISO-8859-15');
-$inputCsv->setFlags(SplFileObject::DROP_NEW_LINE|SplFileObject::READ_AHEAD|SplFileObject::SKIP_EMPTY);
$res = json_encode($inputCsv, JSON_PRETTY_PRINT|JSON_HEX_QUOT|JSON_HEX_TAG|JSON_HEX_AMP|JSON_HEX_APOS);
if (JSON_ERROR_NONE != json_last_error()) {
die(json_last_error_msg());
diff --git a/examples/writing.php b/examples/writing.php
index 239520a..7447175 100755
--- a/examples/writing.php
+++ b/examples/writing.php
@@ -7,7 +7,6 @@ require '../vendor/autoload.php';
$writer = Writer::createFromFileObject(new SplTempFileObject()); //the CSV file will be created into a temporary File
$writer->setDelimiter("\t"); //the delimiter will be the tab character
$writer->setNewline("\r\n"); //use windows line endings for compatibility with some csv libraries
-$writer->setEncodingFrom("utf-8");
$headers = ["position" , "team", "played", "goals difference", "points"];
$writer->insertOne($headers);
diff --git a/src/Modifier/QueryFilter.php b/src/Modifier/QueryFilter.php
index 50519b2..9435418 100644
--- a/src/Modifier/QueryFilter.php
+++ b/src/Modifier/QueryFilter.php
@@ -261,30 +261,6 @@ trait QueryFilter
}
/**
- * Remove the BOM sequence from the CSV
- *
- * @param Iterator $iterator
- *
- * @return Iterator
- */
- protected function applyBomStripping(Iterator $iterator)
- {
- if (!$this->strip_bom) {
- return $iterator;
- }
-
- if (!$this->isBomStrippable()) {
- $this->strip_bom = false;
-
- return $iterator;
- }
-
- $this->strip_bom = false;
-
- return $this->getStripBomIterator($iterator);
- }
-
- /**
* Return the Iterator without the BOM sequence
*
* @param Iterator $iterator
@@ -328,9 +304,11 @@ trait QueryFilter
$iterator = $this->applyBomStripping($iterator);
$iterator = $this->applyIteratorFilter($iterator);
$iterator = $this->applyIteratorSortBy($iterator);
+ $iterator = $this->applyIteratorInterval($iterator);
+
$this->returnType = AbstractCsv::TYPE_ARRAY;
- return $this->applyIteratorInterval($iterator);
+ return $iterator;
}
/**
@@ -339,6 +317,30 @@ trait QueryFilter
abstract public function getIterator();
/**
+ * Remove the BOM sequence from the CSV
+ *
+ * @param Iterator $iterator
+ *
+ * @return Iterator
+ */
+ protected function applyBomStripping(Iterator $iterator)
+ {
+ if (!$this->strip_bom) {
+ return $iterator;
+ }
+
+ if (!$this->isBomStrippable()) {
+ $this->strip_bom = false;
+
+ return $iterator;
+ }
+
+ $this->strip_bom = false;
+
+ return $this->getStripBomIterator($iterator);
+ }
+
+ /**
* Filter the Iterator
*
* @param Iterator $iterator
@@ -362,17 +364,27 @@ trait QueryFilter
*
* @return Iterator
*/
- protected function applyIteratorInterval(Iterator $iterator)
+ protected function applyIteratorSortBy(Iterator $iterator)
{
- if (0 == $this->iterator_offset && -1 == $this->iterator_limit) {
+ if (!$this->iterator_sort_by) {
return $iterator;
}
- $offset = $this->iterator_offset;
- $limit = $this->iterator_limit;
- $this->iterator_limit = -1;
- $this->iterator_offset = 0;
+ $sortFunc = function ($rowA, $rowB) {
+ $sortRes = 0;
+ foreach ($this->iterator_sort_by as $callable) {
+ if (0 !== ($sortRes = call_user_func($callable, $rowA, $rowB))) {
+ break;
+ }
+ }
- return new LimitIterator($iterator, $offset, $limit);
+ return $sortRes;
+ };
+
+ $obj = new ArrayObject(iterator_to_array($iterator));
+ $obj->uasort($sortFunc);
+ $this->clearSortBy();
+
+ return $obj->getIterator();
}
/**
@@ -382,25 +394,17 @@ trait QueryFilter
*
* @return Iterator
*/
- protected function applyIteratorSortBy(Iterator $iterator)
+ protected function applyIteratorInterval(Iterator $iterator)
{
- if (!$this->iterator_sort_by) {
+ if (0 == $this->iterator_offset && -1 == $this->iterator_limit) {
return $iterator;
}
- $obj = new ArrayObject(iterator_to_array($iterator, false));
- $obj->uasort(function ($rowA, $rowB) {
- $sortRes = 0;
- foreach ($this->iterator_sort_by as $callable) {
- if (0 !== ($sortRes = call_user_func($callable, $rowA, $rowB))) {
- break;
- }
- }
-
- return $sortRes;
- });
- $this->clearSortBy();
+ $offset = $this->iterator_offset;
+ $limit = $this->iterator_limit;
+ $this->iterator_limit = -1;
+ $this->iterator_offset = 0;
- return $obj->getIterator();
+ return new LimitIterator($iterator, $offset, $limit);
}
/**