summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorIgnace Nyamagana Butera <nyamsprod@gmail.com>2015-06-04 14:26:54 +0200
committerIgnace Nyamagana Butera <nyamsprod@gmail.com>2015-06-04 14:26:54 +0200
commitd9c9759cd22bef4b2d1801617856d8fdcd4eece0 (patch)
tree0c91501b0eecc4594f75ccc6edc1ee9fb6792bc3
parent9723e60cd358772267ced6bb78206b35b4432501 (diff)
downloadcsv-d9c9759cd22bef4b2d1801617856d8fdcd4eece0.zip
csv-d9c9759cd22bef4b2d1801617856d8fdcd4eece0.tar.gz
csv-d9c9759cd22bef4b2d1801617856d8fdcd4eece0.tar.bz2
make scrutinizer happy
-rw-r--r--src/Config/Output.php12
-rw-r--r--src/Modifier/QueryFilter.php46
-rw-r--r--src/Modifier/StreamFilter.php79
3 files changed, 83 insertions, 54 deletions
diff --git a/src/Config/Output.php b/src/Config/Output.php
index aeab560..6a43dbc 100644
--- a/src/Config/Output.php
+++ b/src/Config/Output.php
@@ -129,18 +129,14 @@ trait Output
{
if (! $this->input_bom) {
$bom = [
- self::BOM_UTF32_BE,
- self::BOM_UTF32_LE,
- self::BOM_UTF16_BE,
- self::BOM_UTF16_LE,
- self::BOM_UTF8,
+ self::BOM_UTF32_BE, self::BOM_UTF32_LE,
+ self::BOM_UTF16_BE, self::BOM_UTF16_LE, self::BOM_UTF8,
];
$csv = $this->getIterator();
$csv->setFlags(SplFileObject::READ_CSV);
$csv->rewind();
$line = $csv->fgets();
-
- $res = array_filter($bom, function ($sequence) use ($line) {
+ $res = array_filter($bom, function ($sequence) use ($line) {
return strpos($line, $sequence) === 0;
});
@@ -187,7 +183,7 @@ trait Output
$csv = $this->getIterator();
$csv->rewind();
$csv->setFlags(SplFileObject::READ_CSV);
- if (! empty($bom) && ! empty($input_bom)) {
+ if (! empty($bom)) {
$csv->fseek(mb_strlen($input_bom));
}
echo $bom;
diff --git a/src/Modifier/QueryFilter.php b/src/Modifier/QueryFilter.php
index 547f6f2..dc7af91 100644
--- a/src/Modifier/QueryFilter.php
+++ b/src/Modifier/QueryFilter.php
@@ -12,7 +12,7 @@
*/
namespace League\Csv\Modifier;
-use ArrayIterator;
+use ArrayObject;
use CallbackFilterIterator;
use InvalidArgumentException;
use Iterator;
@@ -256,8 +256,21 @@ trait QueryFilter
}
$this->strip_bom = false;
+
+ return $this->getStripBomIterator($iterator);
+ }
+
+ /**
+ * Return the Iterator without the BOM sequence
+ *
+ * @param Iterator $iterator
+ *
+ * @return Iterator
+ */
+ protected function getStripBomIterator($iterator)
+ {
$bom = $this->getInputBom();
- return new MapIterator($iterator, function ($row, $index) use ($bom) {
+ $callback = function ($row, $index) use ($bom) {
if (0 == $index) {
$row[0] = mb_substr($row[0], mb_strlen($bom));
$enclosure = $this->getEnclosure();
@@ -268,7 +281,9 @@ trait QueryFilter
}
return $row;
- });
+ };
+
+ return new MapIterator($iterator, $callback);
}
/**
@@ -306,9 +321,8 @@ trait QueryFilter
return $iterator;
}
$offset = $this->iterator_offset;
- $limit = $this->iterator_limit;
-
- $this->iterator_limit = -1;
+ $limit = $this->iterator_limit;
+ $this->iterator_limit = -1;
$this->iterator_offset = 0;
return new LimitIterator($iterator, $offset, $limit);
@@ -326,21 +340,19 @@ trait QueryFilter
if (! $this->iterator_sort_by) {
return $iterator;
}
- $nb_callbacks = count($this->iterator_sort_by);
- $this->iterator_sort_by = array_values($this->iterator_sort_by);
- $res = iterator_to_array($iterator, false);
- uasort($res, function ($rowA, $rowB) use ($nb_callbacks) {
- $res = 0;
- $index = 0;
- while ($index < $nb_callbacks && 0 === $res) {
- $res = $this->iterator_sort_by[$index]($rowA, $rowB);
- ++$index;
+ $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 = $callable($rowA, $rowB))) {
+ break;
+ }
}
- return $res;
+ return $sortRes;
});
$this->clearSortBy();
- return new ArrayIterator($res);
+ return $obj->getIterator();
}
}
diff --git a/src/Modifier/StreamFilter.php b/src/Modifier/StreamFilter.php
index 76321c4..ebea922 100644
--- a/src/Modifier/StreamFilter.php
+++ b/src/Modifier/StreamFilter.php
@@ -48,6 +48,13 @@ trait StreamFilter
protected $stream_uri;
/**
+ * PHP Stream Filter Regex
+ *
+ * @var string
+ */
+ protected $stream_regex = ',^php://filter/(?P<mode>:?read=|write=)?(?P<filters>.*?)/resource=(?P<resource>.*)$,i';
+
+ /**
* Internal path setter
*
* The path must be an SplFileInfo object
@@ -58,43 +65,43 @@ trait StreamFilter
*/
protected function initStreamFilter($path)
{
+ $this->stream_filters = [];
if (! is_string($path)) {
- $this->stream_uri = null;
- $this->stream_filters = [];
+ $this->stream_uri = null;
return;
}
- $this->extractStreamSettings($path);
+ if (! preg_match($this->stream_regex, $path, $matches)) {
+ $this->stream_uri = $path;
+
+ return;
+ }
+ $this->stream_uri = $matches['resource'];
+ $this->stream_filters = explode('|', $matches['filters']);
+ $this->stream_filter_mode = $this->fetchStreamModeAsInt($matches['mode']);
}
/**
- * Extract Available stream settings from $path
+ * Get the stream mode
+ *
+ * @param string $mode
*
- * @param string $path the file path
+ * @return int
*/
- protected function extractStreamSettings($path)
+ protected function fetchStreamModeAsInt($mode)
{
- if (! preg_match(
- ',^php://filter/(?P<mode>:?read=|write=)?(?P<filters>.*?)/resource=(?P<resource>.*)$,i',
- $path,
- $matches
- )) {
- $this->stream_uri = $path;
- $this->stream_filters = [];
-
- return;
+ $mode = strtolower($mode);
+ $mode = rtrim($mode, '=');
+ if ('write' == $mode) {
+ return STREAM_FILTER_WRITE;
}
- $matches['mode'] = strtolower($matches['mode']);
- $mode = STREAM_FILTER_ALL;
- if ('write=' == $matches['mode']) {
- $mode = STREAM_FILTER_WRITE;
- } elseif ('read=' == $matches['mode']) {
- $mode = STREAM_FILTER_READ;
+
+ if ('read' == $mode) {
+ return STREAM_FILTER_READ;
}
- $this->stream_filter_mode = $mode;
- $this->stream_uri = $matches['resource'];
- $this->stream_filters = explode('|', $matches['filters']);
+
+ return STREAM_FILTER_ALL;
}
/**
@@ -258,13 +265,27 @@ trait StreamFilter
return $this->stream_uri;
}
- $prefix = '';
+ return 'php://filter/'
+ .$this->getStreamFilterPrefix()
+ .implode('|', $this->stream_filters)
+ .'/resource='.$this->stream_uri;
+ }
+
+ /**
+ * Return PHP stream filter prefix
+ *
+ * @return string
+ */
+ protected function getStreamFilterPrefix()
+ {
if (STREAM_FILTER_READ == $this->stream_filter_mode) {
- $prefix = 'read=';
- } elseif (STREAM_FILTER_WRITE == $this->stream_filter_mode) {
- $prefix = 'write=';
+ return 'read=';
+ }
+
+ if (STREAM_FILTER_WRITE == $this->stream_filter_mode) {
+ return 'write=';
}
- return 'php://filter/'.$prefix.implode('|', $this->stream_filters).'/resource='.$this->stream_uri;
+ return '';
}
}