Completed
Push — master ( 789d3b...e059a1 )
by Emmanuel
01:49
created

CSVWriter::write()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 0
cts 12
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 9
nc 2
nop 1
crap 6
1
<?php
2
/**
3
 * neuralyzer : Data Anonymization Library and CLI Tool
4
 *
5
 * PHP Version 7.1
6
 *
7
 * @author Emmanuel Dyan
8
 * @author Rémi Sauvat
9
 * @copyright 2018 Emmanuel Dyan
10
 *
11
 * @package edyan/neuralyzer
12
 *
13
 * @license GNU General Public License v2.0
14
 *
15
 * @link https://github.com/edyan/neuralyzer
16
 */
17
18
namespace Edyan\Neuralyzer\Utils;
19
20
use Doctrine\DBAL\Connection;
21
use Edyan\Neuralyzer\Exception\NeuralizerException;
22
23
/**
24
 * A few generic methods to help interacting with DB
25
 */
26
class CSVWriter extends \SplFileObject
27
{
28
    public function __construct()
29
    {
30
        parent::__construct(tempnam(sys_get_temp_dir(), 'neuralyzer'), 'w');
31
    }
32
33
    public function write(array $fields)
34
    {
35
        $options = $this->getCsvControl();
36
        $delimiter = $options[0];
37
        $enclosure = $options[1];
38
        if (!empty($enclosure)) {
39
            return $this->fputcsv($fields, $delimiter, $enclosure);
40
        }
41
42
        $fields = array_map(function ($field) use ($delimiter, $enclosure) {
0 ignored issues
show
Unused Code introduced by
The import $enclosure is not used and could be removed.

This check looks for imports that have been defined, but are not used in the scope.

Loading history...
43
            return str_replace([$delimiter, PHP_EOL], ['', ''], $field);
44
        }, $fields);
45
46
        return $this->fwrite(implode($delimiter, $fields) . PHP_EOL);
47
    }
48
}
49