Completed
Push — master ( 4a880b...4a9e4f )
by Emmanuel
21:39 queued 20:01
created

CSVWriter::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
c 0
b 0
f 0
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 1
nc 1
nop 0
crap 1
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
    /**
29
     * Create a temporary file
30
     */
31 1
    public function __construct()
32
    {
33 1
        parent::__construct(tempnam(sys_get_temp_dir(), 'neuralyzer'), 'w');
34 1
    }
35
36
    /**
37
     * Write a CSV line either by PHP standard or manually when no $enclosure
38
     * @param  array  $fields
39
     * @return int
40
     */
41 1
    public function write(array $fields): int
42
    {
43 1
        $options = $this->getCsvControl();
44 1
        $delimiter = $options[0];
45 1
        $enclosure = $options[1];
46 1
        if (!empty($enclosure)) {
47
            return $this->fputcsv($fields, $delimiter, $enclosure);
48
        }
49
50 1
        $fields = array_map(function ($field) use ($delimiter) {
51 1
            return str_replace([$delimiter, PHP_EOL], ['', ''], $field);
52 1
        }, $fields);
53
54 1
        return $this->fwrite(implode($delimiter, $fields) . PHP_EOL);
55
    }
56
}
57