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

CsvConfiguration::useDoubleQuotes()   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_QUOTE        = '"';
21
    const DEFAULT_ESCAPE       = '\\';
22
    const DEFAULT_DOUBLE_QUOTE = false;
23
    const DEFAULT_ENCODING     = 'UTF-8';
24
25
    const OPTION_DELIMITER    = 'delimiter';
26
    const OPTION_NULL         = 'null';
27
    const OPTION_NEW_LINES    = 'newLines';
28
    const OPTION_QUOTE        = 'quote';
29
    const OPTION_ESCAPE       = 'escape';
30
    const OPTION_DOUBLE_QUOTE = 'doubleQuote';
31
    const OPTION_BOMS         = 'boms';
32
    const OPTION_ENCODING     = 'encoding';
33
34
    /** @var string */
35
    private $delimiter;
36
    /** @var string */
37
    private $quote;
38
    /** @var string */
39
    private $escape;
40
    /** @var bool */
41
    private $doubleQuotes;
42
    /** @var string[] */
43
    private $newLines;
44
    /** @var string */
45
    private $null;
46
    /** @var string[] */
47
    private $boms;
48
    /** @var string */
49
    private $encoding;
50
51
    /**
52
     * CsvConfiguration constructor.
53
     *
54
     * @param array $options List of options that can be configured:
55
     *                       <p> `delimiter`    string (Default: `','`)
56
     *                       <p> `quote`        string (Default: `'"'`)
57
     *                       <p> `escape`       string (Default: `'\\'`)
58
     *                       <p> `doubleQuotes` string (Default: `false`)
59
     *                       <p> `newLines`     string[] (Default: `["\n","\r","\r\n"]`)
60
     *                       <p> `null`         string (Default: `'\\N'`)
61
     *                       <p> `boms`         string[] (Default:
62
     *                       `[Bom::BOM_UTF8,Bom::BOM_UTF16_BE,Bom::BOM_UTF16_LE,Bom::BOM_UTF32_BE,Bom::BOM_UTF32_LE]`)
63
     *                       <p> `encoding`     string (Default: `'UTF-8'`)
64
     */
65 6
    public function __construct(array $options = [])
66
    {
67 6
        $this->delimiter = $this->getOption($options, static::OPTION_DELIMITER, static::DEFAULT_DELIMITER);
68 6
        $this->quote = $this->getOption($options, static::OPTION_QUOTE, static::DEFAULT_QUOTE);
69 6
        $this->escape = $this->getOption($options, static::OPTION_ESCAPE, static::DEFAULT_ESCAPE);
70 6
        $this->doubleQuotes = $this->getOption($options, static::OPTION_DOUBLE_QUOTE, static::DEFAULT_DOUBLE_QUOTE);
71 6
        $this->newLines = $this->getOption($options, static::OPTION_NEW_LINES, ["\n", "\r", "\r\n"]);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getOption($option... array(' ', ' ', ' ')) of type * is incompatible with the declared type array<integer,string> of property $newLines.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
72 6
        $this->null = $this->getOption($options, static::OPTION_NULL, static::DEFAULT_NULL);
73 6
        $this->boms = $this->getOption($options, static::OPTION_BOMS, [
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getOption($option...Csv\Bom::BOM_UTF32_LE)) of type * is incompatible with the declared type array<integer,string> of property $boms.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
74 6
            Bom::BOM_UTF8,
75
            Bom::BOM_UTF16_BE,
76
            Bom::BOM_UTF16_LE,
77
            Bom::BOM_UTF32_BE,
78
            Bom::BOM_UTF32_LE,
79
        ]);
80 6
        $this->encoding = $this->getOption($options, static::OPTION_ENCODING, static::DEFAULT_ENCODING);
81 6
    }
82
83
    /**
84
     * @param array  $options
85
     * @param string $name
86
     * @param mixed  $default
87
     *
88
     * @return mixed
89
     */
90 6
    private function getOption(array $options, $name, $default = null)
91
    {
92 6
        if (array_key_exists($name, $options)) {
93 1
            return $options[$name];
94
        } else {
95 5
            return $default;
96
        }
97
    }
98
99
    /**
100
     * @return string
101
     */
102 30
    public function getDelimiter()
103
    {
104 30
        return $this->delimiter;
105
    }
106
107
    /**
108
     * @return string
109
     */
110 30
    public function getQuote()
111
    {
112 30
        return $this->quote;
113
    }
114
115
    /**
116
     * @return string
117
     */
118 30
    public function getEscape()
119
    {
120 30
        return $this->escape;
121
    }
122
123
    /**
124
     * @return string[]
125
     */
126 30
    public function getNewLines()
127
    {
128 30
        return $this->newLines;
129
    }
130
131
    /**
132
     * @return bool
133
     */
134 28
    public function useDoubleQuotes()
135
    {
136 28
        return $this->doubleQuotes;
137
    }
138
139
    /**
140
     * @return string
141
     */
142 30
    public function getNullValue()
143
    {
144 30
        return $this->null;
145
    }
146
147
    /**
148
     * @return string[]
149
     */
150 30
    public function getBoms()
151
    {
152 30
        return $this->boms;
153
    }
154
155
    /**
156
     * @return string
157
     */
158 30
    public function getEncoding()
159
    {
160 30
        return $this->encoding;
161
    }
162
}
163