‪TYPO3CMS  10.4
CsvUtility.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
19 
24 {
28  public const ‪TYPE_PASSTHROUGH = 0;
29 
33  public const ‪TYPE_REMOVE_CONTROLS = 1;
34 
39  public const ‪TYPE_PREFIX_CONTROLS = 2;
40 
52  public static function ‪csvToArray($input, $fieldDelimiter = ',', $fieldEnclosure = '"', $maximumColumns = 0)
53  {
54  $multiArray = [];
55  $maximumCellCount = 0;
56 
57  if (($handle = fopen('php://memory', 'r+')) !== false) {
58  fwrite($handle, $input);
59  rewind($handle);
60  while (($cells = fgetcsv($handle, 0, $fieldDelimiter, $fieldEnclosure)) !== false) {
61  $cells = is_array($cells) ? $cells : [];
62  $maximumCellCount = max(count($cells), $maximumCellCount);
63  $multiArray[] = preg_replace('|<br */?>|i', LF, $cells);
64  }
65  fclose($handle);
66  }
67 
68  if ($maximumColumns > $maximumCellCount) {
69  $maximumCellCount = $maximumColumns;
70  }
71 
72  foreach ($multiArray as &$row) {
73  for ($key = 0; $key < $maximumCellCount; $key++) {
74  if (
75  $maximumColumns > 0
76  && $maximumColumns < $maximumCellCount
77  && $key >= $maximumColumns
78  ) {
79  if (isset($row[$key])) {
80  unset($row[$key]);
81  }
82  } elseif (!isset($row[$key])) {
83  $row[$key] = '';
84  }
85  }
86  }
87 
88  return $multiArray;
89  }
90 
100  public static function ‪csvValues(array $row, string $delim = ',', string $quote = '"', int $type = self::TYPE_REMOVE_CONTROLS)
101  {
102  $resource = fopen('php://temp', 'w');
103  if (!is_resource($resource)) {
104  throw new \RuntimeException('Cannot open temporary data stream for writing', 1625556521);
105  }
106  $modifier = ‪CsvStreamFilter::applyStreamFilter($resource, false);
107  array_map([self::class, 'assertCellValueType'], $row);
108  if ($type === self::TYPE_REMOVE_CONTROLS) {
109  $row = array_map([self::class, 'removeControlLiterals'], $row);
110  } elseif ($type === self::TYPE_PREFIX_CONTROLS) {
111  $row = array_map([self::class, 'prefixControlLiterals'], $row);
112  }
113  fputcsv($resource, $modifier($row), $delim, $quote);
114  fseek($resource, 0);
115  $content = stream_get_contents($resource);
116  return $content;
117  }
118 
126  protected static function ‪prefixControlLiterals($cellValue)
127  {
128  if (!self::shallFilterValue($cellValue)) {
129  return $cellValue;
130  }
131  $cellValue = (string)$cellValue;
132  return preg_replace('#^([\t\v=+*%/@-])#', '\'${1}', $cellValue);
133  }
134 
142  protected static function removeControlLiterals($cellValue)
143  {
144  if (!self::shallFilterValue($cellValue)) {
145  return $cellValue;
146  }
147  $cellValue = (string)$cellValue;
148  return preg_replace('#^([\t\v=+*%/@-]+)+#', '', $cellValue);
149  }
150 
156  protected static function assertCellValueType($cellValue): void
157  {
158  // int, float, string, bool, null
159  if ($cellValue === null || is_scalar($cellValue)) {
160  return;
161  }
162  throw new \RuntimeException(
163  sprintf('Unexpected type %s for cell value', gettype($cellValue)),
164  1625562833
165  );
166  }
167 
175  protected static function shallFilterValue($cellValue): bool
176  {
177  return $cellValue !== null
178  && !is_bool($cellValue)
179  && !is_numeric($cellValue)
180  && !MathUtility::canBeInterpretedAsInteger($cellValue)
181  && !MathUtility::canBeInterpretedAsFloat($cellValue);
182  }
183 }
‪TYPO3\CMS\Core\Utility\CsvUtility\TYPE_PREFIX_CONTROLS
‪const TYPE_PREFIX_CONTROLS
Definition: CsvUtility.php:39
‪TYPO3\CMS\Core\Utility\CsvUtility\csvToArray
‪static array csvToArray($input, $fieldDelimiter=',', $fieldEnclosure='"', $maximumColumns = 0)
Definition: CsvUtility.php:52
‪TYPO3\CMS\Core\Utility\CsvUtility\csvValues
‪static string csvValues(array $row, string $delim=',', string $quote='"', int $type = self::TYPE_REMOVE_CONTROLS)
Definition: CsvUtility.php:100
‪TYPO3\CMS\Core\Utility
Definition: ArrayUtility.php:16
‪TYPO3\CMS\Core\Utility\CsvUtility\TYPE_REMOVE_CONTROLS
‪const TYPE_REMOVE_CONTROLS
Definition: CsvUtility.php:33
‪TYPO3\CMS\Core\Utility\CsvUtility\prefixControlLiterals
‪static bool int float string null prefixControlLiterals($cellValue)
Definition: CsvUtility.php:126
‪TYPO3\CMS\Core\Utility\CsvUtility\TYPE_PASSTHROUGH
‪const TYPE_PASSTHROUGH
Definition: CsvUtility.php:28
‪TYPO3\CMS\Core\IO\CsvStreamFilter\applyStreamFilter
‪static Closure applyStreamFilter($stream, bool $LF=true)
Definition: CsvStreamFilter.php:75
‪TYPO3\CMS\Core\Utility\CsvUtility
Definition: CsvUtility.php:24
‪TYPO3\CMS\Core\IO\CsvStreamFilter
Definition: CsvStreamFilter.php:27