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