1 | <?php |
||
30 | class EncloseField extends php_user_filter |
||
31 | { |
||
32 | const FILTERNAME = 'convert.league.csv.enclosure'; |
||
33 | |||
34 | /** |
||
35 | * the filter name used to instantiate the class with |
||
36 | * |
||
37 | * @var string |
||
38 | */ |
||
39 | public $filtername; |
||
40 | |||
41 | /** |
||
42 | * Contents of the params parameter passed to stream_filter_append |
||
43 | * or stream_filter_prepend functions |
||
44 | * |
||
45 | * @var mixed |
||
46 | */ |
||
47 | public $params; |
||
48 | |||
49 | /** |
||
50 | * Default sequence |
||
51 | * |
||
52 | * @var string |
||
53 | */ |
||
54 | protected $sequence; |
||
55 | |||
56 | /** |
||
57 | * Characters that triggers enclosure in PHP |
||
58 | * |
||
59 | * @var string |
||
60 | */ |
||
61 | protected static $force_enclosure = "\n\r\t "; |
||
62 | |||
63 | /** |
||
64 | * Static method to register the class as a stream filter |
||
65 | */ |
||
66 | 2 | public static function register() |
|
72 | |||
73 | /** |
||
74 | * Static method to add the stream filter to a {@link Writer} object |
||
75 | * |
||
76 | * @param Writer $csv |
||
77 | * @param string $sequence |
||
78 | * |
||
79 | * @return AbstractCsv |
||
80 | */ |
||
81 | 2 | public static function addTo(Writer $csv, string $sequence): Writer |
|
89 | |||
90 | /** |
||
91 | * Set the Sequence to be used to update enclosure usage |
||
92 | * |
||
93 | * @param string $sequence sequence used to work around fputcsv limitation |
||
94 | * |
||
95 | * @return self |
||
96 | */ |
||
97 | 4 | public function sequence(string $sequence): self |
|
112 | |||
113 | /** |
||
114 | * Filter type and sequence parameters |
||
115 | * |
||
116 | * - The sequence to force enclosure MUST contains one of the following character ("\n\r\t ") |
||
117 | * |
||
118 | * @param string $sequence |
||
119 | * |
||
120 | * @return bool |
||
121 | */ |
||
122 | 4 | protected static function isValidSequence(string $sequence): bool |
|
126 | |||
127 | /** |
||
128 | * @inheritdoc |
||
129 | */ |
||
130 | 2 | public function __invoke(array $record): array |
|
134 | |||
135 | /** |
||
136 | * Format the record field to force the addition of the enclosure by fputcsv |
||
137 | * |
||
138 | * @param mixed $value |
||
139 | * |
||
140 | * @return string |
||
141 | */ |
||
142 | 2 | protected function forceEnclosure($value) |
|
146 | |||
147 | /** |
||
148 | * @inheritdoc |
||
149 | */ |
||
150 | 8 | public function onCreate() |
|
155 | |||
156 | /** |
||
157 | * @inheritdoc |
||
158 | */ |
||
159 | 2 | public function filter($in, $out, &$consumed, $closing) |
|
169 | } |
||
170 |