summaryrefslogtreecommitdiffstats
path: root/src/Config
diff options
context:
space:
mode:
Diffstat (limited to 'src/Config')
-rw-r--r--src/Config/Controls.php97
-rw-r--r--src/Config/Output.php2
2 files changed, 64 insertions, 35 deletions
diff --git a/src/Config/Controls.php b/src/Config/Controls.php
index 94ef968..99149b7 100644
--- a/src/Config/Controls.php
+++ b/src/Config/Controls.php
@@ -4,7 +4,7 @@
*
* @license http://opensource.org/licenses/MIT
* @link https://github.com/thephpleague/csv/
-* @version 7.1.1
+* @version 7.2.0
* @package League.csv
*
* For the full copyright and license information, please view the LICENSE
@@ -62,13 +62,6 @@ trait Controls
protected $newline = "\n";
/**
- * Returns the CSV Iterator
- *
- * @return \Iterator
- */
- abstract public function getIterator();
-
- /**
* Sets the field delimiter
*
* @param string $delimiter
@@ -98,57 +91,93 @@ trait Controls
}
/**
- * Detects the actual number of row according to a delimiter
+ * Detects the CSV file delimiters
*
- * @param string $delimiter a CSV delimiter
- * @param int $nb_rows the number of row to consider
+ * Returns a associative array where each key represents
+ * the number of occurences and each value a delimiter with the
+ * given occurence
*
- * @return int
+ * This method returns incorrect informations when two delimiters
+ * have the same occurrence count
+ *
+ * DEPRECATION WARNING! This method will be removed in the next major point release
+ *
+ * @deprecated deprecated since version 7.2
+ *
+ * @param int $nb_rows
+ * @param string[] $delimiters additional delimiters
+ *
+ * @return string[]
*/
- protected function fetchRowsCountByDelimiter($delimiter, $nb_rows = 1)
+ public function detectDelimiterList($nb_rows = 1, array $delimiters = [])
{
- $iterator = $this->getIterator();
- $iterator->setCsvControl($delimiter, $this->enclosure, $this->escape);
- $iterator = new LimitIterator($iterator, 0, $nb_rows);
- $iterator = new CallbackFilterIterator($iterator, function ($row) {
- return is_array($row) && count($row) > 1;
- });
+ $delimiters = array_merge([$this->delimiter, ',', ';', "\t"], $delimiters);
+ $stats = $this->fetchDelimitersOccurrence($delimiters, $nb_rows);
- return count(iterator_to_array($iterator, false), COUNT_RECURSIVE);
+ return array_flip(array_filter($stats));
}
/**
- * Detects the CSV file delimiters
+ * Detect Delimiters occurences in the CSV
*
- * @param int $nb_rows
- * @param string[] $delimiters additional delimiters
+ * Returns a associative array where each key represents
+ * a valid delimiter and each value the number of occurences
+ *
+ * @param string[] $delimiters the delimiters to consider
+ * @param int $nb_rows Detection is made using $nb_rows of the CSV
*
* @throws InvalidArgumentException If $nb_rows value is invalid
*
- * @return string[]
+ * @return array
*/
- public function detectDelimiterList($nb_rows = 1, array $delimiters = [])
+ public function fetchDelimitersOccurrence(array $delimiters, $nb_rows = 1)
{
$nb_rows = filter_var($nb_rows, FILTER_VALIDATE_INT, ['options' => ['min_range' => 1]]);
- if (! $nb_rows) {
- throw new InvalidArgumentException('`$nb_rows` must be a valid positive integer');
+ if (!$nb_rows) {
+ throw new InvalidArgumentException('The number of rows to consider must be a valid positive integer');
}
-
$delimiters = array_filter($delimiters, function ($str) {
return 1 == mb_strlen($str);
});
- $delimiters = array_unique(array_merge([$this->delimiter, ',', ';', "\t"], $delimiters));
- $res = array_fill_keys($delimiters, 0);
- array_walk($res, function (&$value, $delim) use ($nb_rows) {
- $value = $this->fetchRowsCountByDelimiter($delim, $nb_rows);
- });
+ $delimiters = array_unique($delimiters);
+ $res = [];
+ foreach ($delimiters as $delim) {
+ $res[$delim] = $this->fetchRowsCountByDelimiter($delim, $nb_rows);
+ }
arsort($res, SORT_NUMERIC);
- return array_filter($res);
+ return $res;
+ }
+
+ /**
+ * Detects the actual number of row according to a delimiter
+ *
+ * @param string $delimiter a CSV delimiter
+ * @param int $nb_rows the number of row to consider
+ *
+ * @return int
+ */
+ protected function fetchRowsCountByDelimiter($delimiter, $nb_rows = 1)
+ {
+ $iterator = $this->getIterator();
+ $iterator->setCsvControl($delimiter, $this->enclosure, $this->escape);
+ $iterator = new LimitIterator($iterator, 0, $nb_rows);
+ $iterator = new CallbackFilterIterator($iterator, function ($row) {
+ return is_array($row) && count($row) > 1;
+ });
+
+ return count(iterator_to_array($iterator, false), COUNT_RECURSIVE);
}
/**
+ * Returns the CSV Iterator
+ *
+ * @return \Iterator
+ */
+ abstract public function getIterator();
+
+ /**
* Sets the field enclosure
*
* @param string $enclosure
diff --git a/src/Config/Output.php b/src/Config/Output.php
index ca2cf44..f21dcc2 100644
--- a/src/Config/Output.php
+++ b/src/Config/Output.php
@@ -4,7 +4,7 @@
*
* @license http://opensource.org/licenses/MIT
* @link https://github.com/thephpleague/csv/
-* @version 7.1.1
+* @version 7.2.0
* @package League.csv
*
* For the full copyright and license information, please view the LICENSE