PluginProbe
FluentBoards – Project Management, Task Management, Goal Tracking, Kanban Board, and, Team Collaboration / 1.21
FluentBoards – Project Management, Task Management, Goal Tracking, Kanban Board, and, Team Collaboration v1.21
2.0.15 2.0.12 2.0.10 2.0.4 2.0.1 2.0.0 1.95.3 1.95.2 1.95 1.91.6 trunk 1.11 1.12 1.13 1.20 1.21 1.22 1.23 1.30 1.31 1.32 1.35 1.40 1.41 1.45 All 41 releases
fluent-boards / app / Services / Libs / csv / src / Config / Controls.php

Controls.php in FluentBoards – Project Management, Task Management, Goal Tracking, Kanban Board, and, Team Collaboration 1.21, at app/Services/Libs/csv/src/Config/Controls.php

289 lines 6.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * This file is part of the League.csv library
4 *
5 * @license http://opensource.org/licenses/MIT
6 * @link https://github.com/thephpleague/csv/
7 * @version 7.2.0
8 * @package League.csv
9 *
10 * For the full copyright and license information, please view the LICENSE
11 * file that was distributed with this source code.
12 */
13 namespace League\Csv\Config;
14
15 use CallbackFilterIterator;
16 use InvalidArgumentException;
17 use LimitIterator;
18 use SplFileObject;
19
20 /**
21 * A trait to configure and check CSV file and content
22 *
23 * @package League.csv
24 * @since 6.0.0
25 *
26 */
27 trait Controls
28 {
29 /**
30 * the field delimiter (one character only)
31 *
32 * @var string
33 */
34 protected $delimiter = ',';
35
36 /**
37 * the field enclosure character (one character only)
38 *
39 * @var string
40 */
41 protected $enclosure = '"';
42
43 /**
44 * the field escape character (one character only)
45 *
46 * @var string
47 */
48 protected $escape = '\\';
49
50 /**
51 * the \SplFileObject flags holder
52 *
53 * @var int
54 */
55 protected $flags;
56
57 /**
58 * newline character
59 *
60 * @var string
61 */
62 protected $newline = "\n";
63
64 /**
65 * Sets the field delimiter
66 *
67 * @param string $delimiter
68 *
69 * @throws InvalidArgumentException If $delimiter is not a single character
70 *
71 * @return $this
72 */
73 public function setDelimiter($delimiter)
74 {
75 if (!$this->isValidCsvControls($delimiter)) {
76 throw new InvalidArgumentException('The delimiter must be a single character');
77 }
78 $this->delimiter = $delimiter;
79
80 return $this;
81 }
82
83 /**
84 * Tell whether the submitted string is a valid CSV Control character
85 *
86 * @param string $str The submitted string
87 *
88 * @return bool
89 */
90 protected function isValidCsvControls($str)
91 {
92 return 1 == mb_strlen($str);
93 }
94
95 /**
96 * Returns the current field delimiter
97 *
98 * @return string
99 */
100 public function getDelimiter()
101 {
102 return $this->delimiter;
103 }
104
105 /**
106 * Detects the CSV file delimiters
107 *
108 * Returns a associative array where each key represents
109 * the number of occurences and each value a delimiter with the
110 * given occurence
111 *
112 * This method returns incorrect informations when two delimiters
113 * have the same occurrence count
114 *
115 * DEPRECATION WARNING! This method will be removed in the next major point release
116 *
117 * @deprecated deprecated since version 7.2
118 *
119 * @param int $nb_rows
120 * @param string[] $delimiters additional delimiters
121 *
122 * @return string[]
123 */
124 public function detectDelimiterList($nb_rows = 1, array $delimiters = [])
125 {
126 $delimiters = array_merge([$this->delimiter, ',', ';', "\t"], $delimiters);
127 $stats = $this->fetchDelimitersOccurrence($delimiters, $nb_rows);
128
129 return array_flip(array_filter($stats));
130 }
131
132 /**
133 * Detect Delimiters occurences in the CSV
134 *
135 * Returns a associative array where each key represents
136 * a valid delimiter and each value the number of occurences
137 *
138 * @param string[] $delimiters the delimiters to consider
139 * @param int $nb_rows Detection is made using $nb_rows of the CSV
140 *
141 * @throws InvalidArgumentException If $nb_rows value is invalid
142 *
143 * @return array
144 */
145 public function fetchDelimitersOccurrence(array $delimiters, $nb_rows = 1)
146 {
147 if (!($nb_rows = filter_var($nb_rows, FILTER_VALIDATE_INT, ['options' => ['min_range' => 1]]))) {
148 throw new InvalidArgumentException('The number of rows to consider must be a valid positive integer');
149 }
150
151 $filterRow = function ($row) {
152 return is_array($row) && count($row) > 1;
153 };
154 $delimiters = array_unique(array_filter($delimiters, [$this, 'isValidCsvControls']));
155 $csv = $this->getIterator();
156 $res = [];
157 foreach ($delimiters as $delim) {
158 $csv->setCsvControl($delim, $this->enclosure, $this->escape);
159 $iterator = new CallbackFilterIterator(new LimitIterator($csv, 0, $nb_rows), $filterRow);
160 $res[$delim] = count(iterator_to_array($iterator, false), COUNT_RECURSIVE);
161 }
162 arsort($res, SORT_NUMERIC);
163
164 return $res;
165 }
166
167 /**
168 * Returns the CSV Iterator
169 *
170 * @return SplFileObject
171 */
172 abstract public function getIterator();
173
174 /**
175 * Sets the field enclosure
176 *
177 * @param string $enclosure
178 *
179 * @throws InvalidArgumentException If $enclosure is not a single character
180 *
181 * @return $this
182 */
183 public function setEnclosure($enclosure)
184 {
185 if (!$this->isValidCsvControls($enclosure)) {
186 throw new InvalidArgumentException('The enclosure must be a single character');
187 }
188 $this->enclosure = $enclosure;
189
190 return $this;
191 }
192
193 /**
194 * Returns the current field enclosure
195 *
196 * @return string
197 */
198 public function getEnclosure()
199 {
200 return $this->enclosure;
201 }
202
203 /**
204 * Sets the field escape character
205 *
206 * @param string $escape
207 *
208 * @throws InvalidArgumentException If $escape is not a single character
209 *
210 * @return $this
211 */
212 public function setEscape($escape)
213 {
214 if (!$this->isValidCsvControls($escape)) {
215 throw new InvalidArgumentException('The escape character must be a single character');
216 }
217 $this->escape = $escape;
218
219 return $this;
220 }
221
222 /**
223 * Returns the current field escape character
224 *
225 * @return string
226 */
227 public function getEscape()
228 {
229 return $this->escape;
230 }
231
232 /**
233 * Sets the Flags associated to the CSV SplFileObject
234 *
235 * @param int $flags
236 *
237 * @throws InvalidArgumentException If the argument is not a valid integer
238 *
239 * @return $this
240 */
241 public function setFlags($flags)
242 {
243 $flags = $this->filterInteger($flags, 0, 'you should use a `SplFileObject` Constant');
244 $this->flags = $flags | SplFileObject::READ_CSV;
245
246 return $this;
247 }
248
249 /**
250 * @inheritdoc
251 */
252 abstract protected function filterInteger($int, $minValue, $errorMessage);
253
254 /**
255 * Returns the file Flags
256 *
257 * @return int
258 */
259 public function getFlags()
260 {
261 return $this->flags;
262 }
263
264
265 /**
266 * Sets the newline sequence characters
267 *
268 * @param string $newline
269 *
270 * @return static
271 */
272 public function setNewline($newline)
273 {
274 $this->newline = (string) $newline;
275
276 return $this;
277 }
278
279 /**
280 * Returns the current newline sequence characters
281 *
282 * @return string
283 */
284 public function getNewline()
285 {
286 return $this->newline;
287 }
288 }
289