Completed
Pull Request — master (#203)
by ignace nyamagana
02:06
created

Writer   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 138
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 15
lcom 1
cbo 2
dl 0
loc 138
ccs 46
cts 46
cp 1
rs 10
c 4
b 0
f 0

7 Methods

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