‪TYPO3CMS  10.4
CsvStreamFilter.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
26 class ‪CsvStreamFilter extends \php_user_filter
27 {
28  protected const ‪NAME = 'csv.typo3';
29 
35  public ‪$params = [];
36 
52  public function ‪filter($in, $out, &$consumed, $closing): int
53  {
54  while ($bucket = stream_bucket_make_writeable($in)) {
55  // removes sequence boundary indicator
56  $bucket->data = str_replace(
57  $this->params['sequence'],
58  '',
59  $bucket->data
60  );
61  if ($this->params['LF'] === false) {
62  // remove line-feed added by `fputcsv` per default
63  $bucket->data = preg_replace('#\r?\n$#', '', $bucket->data);
64  }
65  $consumed += $bucket->datalen;
66  stream_bucket_append($out, $bucket);
67  }
68  return PSFS_PASS_ON;
69  }
70 
76  public static function ‪applyStreamFilter($stream, bool $LF = true): \Closure
77  {
79  // must contain a spacing character to enforce enclosure
80  $sequence = "\t\x1d\x1e\x1f";
81  stream_filter_append(
82  $stream,
83  self::NAME,
84  STREAM_FILTER_WRITE,
85  ['sequence' => $sequence, 'LF' => $LF]
86  );
87  return ‪self::buildStreamFilterModifier($sequence);
88  }
89 
93  protected static function ‪registerStreamFilter()
94  {
95  if (in_array(self::NAME, stream_get_filters(), true)) {
96  return;
97  }
98  stream_filter_register(
99  self::NAME,
100  static::class
101  );
102  }
103 
108  protected static function ‪buildStreamFilterModifier(string $sequence): \Closure
109  {
110  return function ($element) use ($sequence) {
111  foreach ($element as &$value) {
112  if (is_numeric($value) || $value === '') {
113  continue;
114  }
115  $value = $sequence . $value;
116  }
117  unset($value); // de-reference
118  return $element;
119  };
120  }
121 }
‪TYPO3\CMS\Core\IO\CsvStreamFilter\NAME
‪const NAME
Definition: CsvStreamFilter.php:28
‪TYPO3\CMS\Core\IO\CsvStreamFilter\registerStreamFilter
‪static registerStreamFilter()
Definition: CsvStreamFilter.php:92
‪TYPO3\CMS\Core\IO\CsvStreamFilter\filter
‪int filter($in, $out, &$consumed, $closing)
Definition: CsvStreamFilter.php:51
‪TYPO3\CMS\Core\IO\CsvStreamFilter\buildStreamFilterModifier
‪static Closure buildStreamFilterModifier(string $sequence)
Definition: CsvStreamFilter.php:107
‪TYPO3\CMS\Core\IO\CsvStreamFilter\$params
‪array $params
Definition: CsvStreamFilter.php:34
‪TYPO3\CMS\Core\IO\CsvStreamFilter\applyStreamFilter
‪static Closure applyStreamFilter($stream, bool $LF=true)
Definition: CsvStreamFilter.php:75
‪TYPO3\CMS\Core\IO
Definition: CsvStreamFilter.php:18
‪TYPO3\CMS\Core\IO\CsvStreamFilter
Definition: CsvStreamFilter.php:27