PluginProbe
Better Payment – Instant Payments, Donations, Fundraising with Subscriptions & More / 2.2.2
Better Payment – Instant Payments, Donations, Fundraising with Subscriptions & More v2.2.2
2.4.0 2.3.4 2.3.3 2.3.2 2.3.1 2.3.0 2.2.2 2.2.1 2.2.0 2.1.2 2.1.1 trunk 0.0.1 0.0.2 0.0.3 0.0.4 0.0.5 0.0.6 0.0.7 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 All 67 releases
better-payment / vendor / league / csv / src / EncloseField.php

EncloseField.php in Better Payment – Instant Payments, Donations, Fundraising with Subscriptions & More 2.2.2, at vendor/league/csv/src/EncloseField.php

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