Skip to content

Commit

Permalink
Improve QueryFilter code
Browse files Browse the repository at this point in the history
  • Loading branch information
nyamsprod committed Dec 1, 2015
1 parent 3d35e2f commit e2b4f85
Showing 1 changed file with 43 additions and 48 deletions.
91 changes: 43 additions & 48 deletions src/Modifier/QueryFilter.php
Expand Up @@ -81,8 +81,8 @@ trait QueryFilter
*/
public function setReturnType($type)
{
$modes = [AbstractCsv::TYPE_ARRAY => 1, AbstractCsv::TYPE_ITERATOR => 1];
if (!isset($modes[$type])) {
$returnTypeList = [AbstractCsv::TYPE_ARRAY => 1, AbstractCsv::TYPE_ITERATOR => 1];
if (!isset($returnTypeList[$type])) {
throw new UnexpectedValueException('Unknown return type');
}
$this->returnType = $type;
Expand All @@ -104,18 +104,6 @@ public function stripBom($status)
return $this;
}

/**
* Tell whether we can strip or not the leading BOM sequence
*
* @return bool
*/
protected function isBomStrippable()
{
$bom = $this->getInputBom();

return !empty($bom) && $this->strip_bom;
}

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -182,33 +170,6 @@ public function addFilter(callable $callable)
return $this;
}

/**
* Return the Iterator without the BOM sequence
*
* @param Iterator $iterator
*
* @return Iterator
*/
protected function getStripBomIterator(Iterator $iterator)
{
$bom = $this->getInputBom();

$stripBom = function ($row, $index) use ($bom) {
if (0 == $index) {
$row[0] = mb_substr($row[0], mb_strlen($bom));
$enclosure = $this->getEnclosure();
//enclosure should be remove when a BOM sequence is stripped
if ($row[0][0] === $enclosure && mb_substr($row[0], -1, 1) == $enclosure) {
$row[0] = mb_substr($row[0], 1, -1);
}
}

return $row;
};

return new MapIterator($iterator, $stripBom);
}

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -265,6 +226,43 @@ protected function applyBomStripping(Iterator $iterator)
return $this->getStripBomIterator($iterator);
}

/**
* Tell whether we can strip or not the leading BOM sequence
*
* @return bool
*/
protected function isBomStrippable()
{
return !empty($this->getInputBom()) && $this->strip_bom;
}

/**
* Return the Iterator without the BOM sequence
*
* @param Iterator $iterator
*
* @return Iterator
*/
protected function getStripBomIterator(Iterator $iterator)
{
$bomLength = mb_strlen($this->getInputBom());
$enclosure = $this->getEnclosure();
$stripBom = function ($row, $index) use ($bomLength, $enclosure) {
if (0 != $index) {
return $row;
}

$row[0] = mb_substr($row[0], $bomLength);
if ($row[0][0] === $enclosure && mb_substr($row[0], -1, 1) === $enclosure) {
$row[0] = mb_substr($row[0], 1, -1);
}

return $row;
};

return new MapIterator($iterator, $stripBom);
}

/**
* Filter the Iterator
*
Expand All @@ -274,9 +272,10 @@ protected function applyBomStripping(Iterator $iterator)
*/
protected function applyIteratorFilter(Iterator $iterator)
{
foreach ($this->iterator_filters as $callable) {
$iterator = new CallbackFilterIterator($iterator, $callable);
}
$reducer = function ($iterator, $callable) {
return new CallbackFilterIterator($iterator, $callable);
};
$iterator = array_reduce($this->iterator_filters, $reducer, $iterator);
$this->iterator_filters = [];

return $iterator;
Expand All @@ -297,7 +296,6 @@ protected function applyIteratorSortBy(Iterator $iterator)

$obj = new ArrayObject(iterator_to_array($iterator));
$obj->uasort(function ($rowA, $rowB) {
$sortRes = 0;
foreach ($this->iterator_sort_by as $callable) {
if (0 !== ($sortRes = call_user_func($callable, $rowA, $rowB))) {
break;
Expand All @@ -320,9 +318,6 @@ protected function applyIteratorSortBy(Iterator $iterator)
*/
protected function applyIteratorInterval(Iterator $iterator)
{
if (0 == $this->iterator_offset && -1 == $this->iterator_limit) {
return $iterator;
}
$offset = $this->iterator_offset;
$limit = $this->iterator_limit;
$this->iterator_limit = -1;
Expand Down

0 comments on commit e2b4f85

Please sign in to comment.