AbstractCsv.php
3 days ago
ByteSequence.php
3 days ago
CannotInsertRecord.php
3 days ago
CharsetConverter.php
3 days ago
ColumnConsistency.php
3 days ago
EncloseField.php
3 days ago
EscapeFormula.php
3 days ago
Exception.php
9 months ago
HTMLConverter.php
3 days ago
Info.php
3 days ago
InvalidArgument.php
9 months ago
MapIterator.php
3 days ago
RFC4180Field.php
3 days ago
Reader.php
3 days ago
ResultSet.php
3 days ago
Statement.php
3 days ago
Stream.php
3 days ago
SyntaxError.php
3 days ago
TabularDataReader.php
3 days ago
UnableToProcessCsv.php
9 months ago
UnavailableFeature.php
9 months ago
UnavailableStream.php
9 months ago
Writer.php
3 days ago
XMLConverter.php
3 days ago
functions.php
9 months ago
functions_include.php
9 months ago
EncloseField.php
98 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * League.Csv (https://csv.thephpleague.com) |
| 5 | * |
| 6 | * (c) Ignace Nyamagana Butera <nyamsprod@gmail.com> |
| 7 | * |
| 8 | * For the full copyright and license information, please view the LICENSE |
| 9 | * file that was distributed with this source code. |
| 10 | */ |
| 11 | declare (strict_types=1); |
| 12 | namespace IAWPSCOPED\League\Csv; |
| 13 | |
| 14 | use InvalidArgumentException; |
| 15 | use php_user_filter; |
| 16 | use function array_map; |
| 17 | use function in_array; |
| 18 | use function str_replace; |
| 19 | use function strcspn; |
| 20 | use function stream_bucket_append; |
| 21 | use function stream_bucket_make_writeable; |
| 22 | use function stream_filter_register; |
| 23 | use function stream_get_filters; |
| 24 | use function strlen; |
| 25 | /** |
| 26 | * A stream filter to improve enclosure character usage. |
| 27 | * |
| 28 | * @see https://tools.ietf.org/html/rfc4180#section-2 |
| 29 | * @see https://bugs.php.net/bug.php?id=38301 |
| 30 | * @internal |
| 31 | */ |
| 32 | class EncloseField extends php_user_filter |
| 33 | { |
| 34 | const FILTERNAME = 'convert.league.csv.enclosure'; |
| 35 | /** Default sequence. */ |
| 36 | protected string $sequence; |
| 37 | /** Characters that triggers enclosure in PHP. */ |
| 38 | protected static string $force_enclosure = "\n\r\t "; |
| 39 | /** |
| 40 | * Static method to return the stream filter filtername. |
| 41 | */ |
| 42 | public static function getFiltername() : string |
| 43 | { |
| 44 | return self::FILTERNAME; |
| 45 | } |
| 46 | /** |
| 47 | * Static method to register the class as a stream filter. |
| 48 | */ |
| 49 | public static function register() : void |
| 50 | { |
| 51 | if (!in_array(self::FILTERNAME, stream_get_filters(), \true)) { |
| 52 | stream_filter_register(self::FILTERNAME, self::class); |
| 53 | } |
| 54 | } |
| 55 | /** |
| 56 | * Static method to add the stream filter to a {@link Writer} object. |
| 57 | * |
| 58 | * @throws InvalidArgumentException if the sequence is malformed |
| 59 | * @throws Exception |
| 60 | */ |
| 61 | public static function addTo(Writer $csv, string $sequence) : Writer |
| 62 | { |
| 63 | self::register(); |
| 64 | if (!self::isValidSequence($sequence)) { |
| 65 | throw new InvalidArgumentException('The sequence must contain at least one character to force enclosure'); |
| 66 | } |
| 67 | return $csv->addFormatter(fn(array $record): array => array_map(fn(?string $value): string => $sequence . $value, $record))->addStreamFilter(self::FILTERNAME, ['sequence' => $sequence]); |
| 68 | } |
| 69 | /** |
| 70 | * Filter type and sequence parameters. |
| 71 | * |
| 72 | * The sequence to force enclosure MUST contains one of the following character ("\n\r\t ") |
| 73 | */ |
| 74 | protected static function isValidSequence(string $sequence) : bool |
| 75 | { |
| 76 | return strlen($sequence) != strcspn($sequence, self::$force_enclosure); |
| 77 | } |
| 78 | public function onCreate() : bool |
| 79 | { |
| 80 | return isset($this->params['sequence']) && self::isValidSequence($this->params['sequence']); |
| 81 | } |
| 82 | /** |
| 83 | * @param resource $in |
| 84 | * @param resource $out |
| 85 | * @param int $consumed |
| 86 | * @param bool $closing |
| 87 | */ |
| 88 | public function filter($in, $out, &$consumed, $closing) : int |
| 89 | { |
| 90 | while (null !== ($bucket = stream_bucket_make_writeable($in))) { |
| 91 | $bucket->data = str_replace($this->params['sequence'], '', $bucket->data); |
| 92 | $consumed += $bucket->datalen; |
| 93 | stream_bucket_append($out, $bucket); |
| 94 | } |
| 95 | return \PSFS_PASS_ON; |
| 96 | } |
| 97 | } |
| 98 |