Completed
Pull Request — master (#7)
by Harry
02:58
created

CsvConfiguration::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 11
rs 9.4285
ccs 3
cts 3
cp 1
cc 1
eloc 9
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * This file is part of graze/csv-token
4
 *
5
 * Copyright (c) 2016 Nature Delivered Ltd. <https://www.graze.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @license https://github.com/graze/csv-token/blob/master/LICENSE.md
11
 * @link    https://github.com/graze/csv-token
12
 */
13
14
namespace Graze\CsvToken\Csv;
15
16
class CsvConfiguration implements CsvConfigurationInterface
17
{
18
    const DEFAULT_DELIMITER    = ',';
19
    const DEFAULT_NULL         = '\\N';
20
    const DEFAULT_NEW_LINE     = PHP_EOL;
21
    const DEFAULT_QUOTE        = '"';
22
    const DEFAULT_ESCAPE       = '\\';
23
    const DEFAULT_DOUBLE_QUOTE = false;
24
    const DEFAULT_BOM          = null;
25
    const DEFAULT_ENCODING     = 'UTF-8';
26
27
    const OPTION_DELIMITER    = 'delimiter';
28
    const OPTION_NULL         = 'null';
29
    const OPTION_NEW_LINE     = 'newLine';
30
    const OPTION_QUOTE        = 'quote';
31
    const OPTION_ESCAPE       = 'escape';
32
    const OPTION_DOUBLE_QUOTE = 'doubleQuote';
33
    const OPTION_BOM          = 'bom';
34
    const OPTION_ENCODING     = 'encoding';
35
36
    /** @var string */
37
    private $delimiter;
38
    /** @var string */
39
    private $quote;
40
    /** @var string */
41
    private $escape;
42
    /** @var bool */
43
    private $doubleQuotes;
44
    /** @var string|array */
45
    private $newLine;
46
    /** @var string */
47
    private $null;
48
    /** @var string|null */
49
    private $bom;
50
    /** @var string */
51
    private $encoding;
52
53
    /**
54
     * CsvConfiguration constructor.
55
     *
56 6
     * @param array $options List of options that can be configured:
57
     *                       <p> `delimiter`    string (Default: `','`)
58 6
     *                       <p> `quote`        string (Default: `'"'`)
59 6
     *                       <p> `escape`       string (Default: `'\\'`)
60 6
     *                       <p> `doubleQuotes` string (Default: `false`)
61 6
     *                       <p> `newLine`      string|array (Default: `PHP_EOL`)
62 6
     *                       <p> `null`         string (Default: `'\\N'`)
63 6
     *                       <p> `bom`          string (Default: `null`)
64 6
     *                       <p> `encoding`     string (Default: `'UTF-8'`)
65
     */
66
    public function __construct(array $options = [])
67
    {
68
        $this->delimiter = $this->getOption($options, static::OPTION_DELIMITER, static::DEFAULT_DELIMITER);
69
        $this->quote = $this->getOption($options, static::OPTION_QUOTE, static::DEFAULT_QUOTE);
70
        $this->escape = $this->getOption($options, static::OPTION_ESCAPE, static::DEFAULT_ESCAPE);
71
        $this->doubleQuotes = $this->getOption($options, static::OPTION_DOUBLE_QUOTE, static::DEFAULT_DOUBLE_QUOTE);
72
        $this->newLine = $this->getOption($options, static::OPTION_NEW_LINE, static::DEFAULT_NEW_LINE);
73 6
        $this->null = $this->getOption($options, static::OPTION_NULL, static::DEFAULT_NULL);
74
        $this->bom = $this->getOption($options, static::OPTION_BOM, static::DEFAULT_BOM);
75 6
        $this->encoding = $this->getOption($options, static::OPTION_ENCODING, static::DEFAULT_ENCODING);
76 1
    }
77
78 5
    /**
79
     * @param array  $options
80
     * @param string $name
81
     * @param mixed  $default
82
     *
83
     * @return mixed
84
     */
85 26
    private function getOption(array $options, $name, $default = null)
86
    {
87 26
        if (array_key_exists($name, $options)) {
88
            return $options[$name];
89
        } else {
90
            return $default;
91
        }
92
    }
93 26
94
    /**
95 26
     * @return string
96
     */
97
    public function getDelimiter()
98
    {
99
        return $this->delimiter;
100
    }
101 26
102
    /**
103 26
     * @return string
104
     */
105
    public function getQuote()
106
    {
107
        return $this->quote;
108
    }
109 26
110
    /**
111 26
     * @return string
112
     */
113
    public function getEscape()
114
    {
115
        return $this->escape;
116
    }
117 24
118
    /**
119 24
     * @return string|array
120
     */
121
    public function getNewLine()
122
    {
123
        return $this->newLine;
124
    }
125 26
126
    /**
127 26
     * @return bool
128
     */
129
    public function useDoubleQuotes()
130
    {
131
        return $this->doubleQuotes;
132
    }
133
134
    /**
135
     * @return string
136
     */
137
    public function getNullValue()
138
    {
139
        return $this->null;
140
    }
141
142
    /**
143
     * @return string|null
144
     */
145
    public function getBom()
146
    {
147
        return $this->bom;
148
    }
149
150
    /**
151
     * @return string
152
     */
153
    public function getEncoding()
154
    {
155
        return $this->encoding;
156
    }
157
}
158