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 / Writer.php

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

165 lines 3.7 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;
14
15 use InvalidArgumentException;
16 use League\Csv\Modifier\RowFilter;
17 use ReflectionMethod;
18 use Traversable;
19
20 /**
21 * A class to manage data insertion into a CSV
22 *
23 * @package League.csv
24 * @since 4.0.0
25 *
26 */
27 class Writer extends AbstractCsv
28 {
29 use RowFilter;
30
31 /**
32 * @inheritdoc
33 */
34 protected $stream_filter_mode = STREAM_FILTER_WRITE;
35
36 /**
37 * The CSV object holder
38 *
39 * @var \SplFileObject
40 */
41 protected $csv;
42
43 /**
44 * fputcsv method from SplFileObject
45 *
46 * @var ReflectionMethod
47 */
48 protected static $fputcsv;
49
50 /**
51 * Nb parameters for SplFileObject::fputcsv method
52 *
53 * @var integer
54 */
55 protected static $fputcsv_param_count;
56
57 /**
58 * @inheritdoc
59 */
60 protected function __construct($path, $open_mode = 'r+')
61 {
62 parent::__construct($path, $open_mode);
63 static::initFputcsv();
64 }
65
66 /**
67 * initiate a SplFileObject::fputcsv method
68 */
69 protected static function initFputcsv()
70 {
71 if (is_null(static::$fputcsv)) {
72 static::$fputcsv = new ReflectionMethod('\SplFileObject', 'fputcsv');
73 static::$fputcsv_param_count = static::$fputcsv->getNumberOfParameters();
74 }
75 }
76
77 /**
78 * Adds multiple lines to the CSV document
79 *
80 * a simple wrapper method around insertOne
81 *
82 * @param Traversable|array $rows a multidimensional array or a Traversable object
83 *
84 * @throws InvalidArgumentException If the given rows format is invalid
85 *
86 * @return static
87 */
88 public function insertAll($rows)
89 {
90 if (!is_array($rows) && !$rows instanceof Traversable) {
91 throw new InvalidArgumentException(
92 'the provided data must be an array OR a \Traversable object'
93 );
94 }
95
96 foreach ($rows as $row) {
97 $this->insertOne($row);
98 }
99
100 return $this;
101 }
102
103 /**
104 * Adds a single line to a CSV document
105 *
106 * @param string[]|string $row a string, an array or an object implementing to '__toString' method
107 *
108 * @return static
109 */
110 public function insertOne($row)
111 {
112 if (!is_array($row)) {
113 $row = str_getcsv($row, $this->delimiter, $this->enclosure, $this->escape);
114 }
115 $row = $this->formatRow($row);
116 $this->validateRow($row);
117
118 if (is_null($this->csv)) {
119 $this->csv = $this->getIterator();
120 }
121
122 static::$fputcsv->invokeArgs($this->csv, $this->getFputcsvParameters($row));
123 if ("\n" !== $this->newline) {
124 $this->csv->fseek(-1, SEEK_CUR);
125 $this->csv->fwrite($this->newline);
126 }
127
128 return $this;
129 }
130
131 /**
132 * returns the parameters for SplFileObject::fputcsv
133 *
134 * @param array $fields The fields to be add
135 *
136 * @return array
137 */
138 protected function getFputcsvParameters(array $fields)
139 {
140 $parameters = [$fields, $this->delimiter, $this->enclosure];
141 if (4 == static::$fputcsv_param_count) {
142 $parameters[] = $this->escape;
143 }
144
145 return $parameters;
146 }
147
148 /**
149 * {@inheritdoc}
150 */
151 public function isActiveStreamFilter()
152 {
153 return parent::isActiveStreamFilter() && is_null($this->csv);
154 }
155
156 /**
157 * {@inheritdoc}
158 */
159 public function __destruct()
160 {
161 $this->csv = null;
162 parent::__destruct();
163 }
164 }
165