Completed
Pull Request — master (#7)
by Harry
06:13 queued 03:25
created

CsvConfiguration::getBom()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
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
     * @param array $options List of options that can be configured:
57
     *                       <p> `delimiter`    string (Default: `','`)
58
     *                       <p> `quote`        string (Default: `'"'`)
59
     *                       <p> `escape`       string (Default: `'\\'`)
60
     *                       <p> `doubleQuotes` string (Default: `false`)
61
     *                       <p> `newLine`      string|array (Default: `PHP_EOL`)
62
     *                       <p> `null`         string (Default: `'\\N'`)
63
     *                       <p> `bom`          string (Default: `null`)
64
     *                       <p> `encoding`     string (Default: `'UTF-8'`)
65
     */
66 6
    public function __construct(array $options = [])
67
    {
68 6
        $this->delimiter = $this->getOption($options, static::OPTION_DELIMITER, static::DEFAULT_DELIMITER);
69 6
        $this->quote = $this->getOption($options, static::OPTION_QUOTE, static::DEFAULT_QUOTE);
70 6
        $this->escape = $this->getOption($options, static::OPTION_ESCAPE, static::DEFAULT_ESCAPE);
71 6
        $this->doubleQuotes = $this->getOption($options, static::OPTION_DOUBLE_QUOTE, static::DEFAULT_DOUBLE_QUOTE);
72 6
        $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 6
        $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 6
    }
77
78
    /**
79
     * @param array  $options
80
     * @param string $name
81
     * @param mixed  $default
82
     *
83
     * @return mixed
84
     */
85 6
    private function getOption(array $options, $name, $default = null)
86
    {
87 6
        if (array_key_exists($name, $options)) {
88 1
            return $options[$name];
89
        } else {
90 6
            return $default;
91
        }
92
    }
93
94
    /**
95
     * @return string
96
     */
97 30
    public function getDelimiter()
98
    {
99 30
        return $this->delimiter;
100
    }
101
102
    /**
103
     * @return string
104
     */
105 30
    public function getQuote()
106
    {
107 30
        return $this->quote;
108
    }
109
110
    /**
111
     * @return string
112
     */
113 30
    public function getEscape()
114
    {
115 30
        return $this->escape;
116
    }
117
118
    /**
119
     * @return string|array
120
     */
121 30
    public function getNewLine()
122
    {
123 30
        return $this->newLine;
124
    }
125
126
    /**
127
     * @return bool
128
     */
129 28
    public function useDoubleQuotes()
130
    {
131 28
        return $this->doubleQuotes;
132
    }
133
134
    /**
135
     * @return string
136
     */
137 30
    public function getNullValue()
138
    {
139 30
        return $this->null;
140
    }
141
142
    /**
143
     * @return string|null
144
     */
145 29
    public function getBom()
146
    {
147 29
        return $this->bom;
148
    }
149
150
    /**
151
     * @return string
152
     */
153 29
    public function getEncoding()
154
    {
155 29
        return $this->encoding;
156
    }
157
}
158