Completed
Push — master ( 7693a6...41a944 )
by Jonathan
12s
created

AbstractBase::__construct()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 20
rs 8.8571
cc 5
eloc 12
nc 8
nop 4
1
<?php
2
3
namespace EasyCSV;
4
5
abstract class AbstractBase
6
{
7
    protected $handle;
8
    protected $delimiter = ',';
9
    protected $enclosure = '"';
10
11
    public function __construct($path, $mode = 'r+', $isNeedBOM = false, $headers = array())
12
    {
13
        $isNewFile = false;
14
15
        if (! file_exists($path)) {
16
            touch($path);
17
            $isNewFile = true;
18
        }
19
        $this->handle = new \SplFileObject($path, $mode);
20
        $this->handle->setFlags(\SplFileObject::DROP_NEW_LINE);
21
22
        if ($isNeedBOM) {
23
            $this->handle->fwrite("\xEF\xBB\xBF");
24
        }
25
26
        if($isNewFile && isset($headers)) {
27
            $headerLine = join(',', $headers) . PHP_EOL;
28
            $this->handle->fwrite($headerLine, strlen($headerLine));
29
        }
30
    }
31
32
    public function __destruct()
33
    {
34
        $this->handle = null;
35
    }
36
37
    public function setDelimiter($delimiter)
38
    {
39
        $this->delimiter = $delimiter;
40
    }
41
42
    public function setEnclosure($enclosure)
43
    {
44
        $this->enclosure = $enclosure;
45
    }
46
}
47