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

CsvConfiguration::getNewLine()   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_LINES    = ["\n", "\r", "\r\n"];
21
    const DEFAULT_QUOTE        = '"';
22
    const DEFAULT_ESCAPE       = '\\';
23
    const DEFAULT_DOUBLE_QUOTE = false;
24
    const DEFAULT_BOMS         = [
25
        Bom::BOM_UTF8,
26
        Bom::BOM_UTF16_BE,
27
        Bom::BOM_UTF16_LE,
28
        Bom::BOM_UTF32_BE,
29
        Bom::BOM_UTF32_LE,
30
    ];
31
    const DEFAULT_ENCODING     = 'UTF-8';
32
33
    const OPTION_DELIMITER    = 'delimiter';
34
    const OPTION_NULL         = 'null';
35
    const OPTION_NEW_LINES    = 'newLines';
36
    const OPTION_QUOTE        = 'quote';
37
    const OPTION_ESCAPE       = 'escape';
38
    const OPTION_DOUBLE_QUOTE = 'doubleQuote';
39
    const OPTION_BOMS         = 'boms';
40
    const OPTION_ENCODING     = 'encoding';
41
42
    /** @var string */
43
    private $delimiter;
44
    /** @var string */
45
    private $quote;
46
    /** @var string */
47
    private $escape;
48
    /** @var bool */
49
    private $doubleQuotes;
50
    /** @var string[] */
51
    private $newLines;
52
    /** @var string */
53
    private $null;
54
    /** @var string[] */
55
    private $boms;
56
    /** @var string */
57
    private $encoding;
58
59
    /**
60
     * CsvConfiguration constructor.
61
     *
62
     * @param array $options List of options that can be configured:
63
     *                       <p> `delimiter`    string (Default: `','`)
64
     *                       <p> `quote`        string (Default: `'"'`)
65
     *                       <p> `escape`       string (Default: `'\\'`)
66
     *                       <p> `doubleQuotes` string (Default: `false`)
67
     *                       <p> `newLines`     string[] (Default: `["\n","\r","\r\n"]`)
68
     *                       <p> `null`         string (Default: `'\\N'`)
69
     *                       <p> `boms`         string[] (Default:
70
     *                       `[Bom::BOM_UTF8,Bom::BOM_UTF16_BE,Bom::BOM_UTF16_LE,Bom::BOM_UTF32_BE,Bom::BOM_UTF32_LE]`)
71
     *                       <p> `encoding`     string (Default: `'UTF-8'`)
72
     */
73 6
    public function __construct(array $options = [])
74
    {
75 6
        $this->delimiter = $this->getOption($options, static::OPTION_DELIMITER, static::DEFAULT_DELIMITER);
76 6
        $this->quote = $this->getOption($options, static::OPTION_QUOTE, static::DEFAULT_QUOTE);
77 6
        $this->escape = $this->getOption($options, static::OPTION_ESCAPE, static::DEFAULT_ESCAPE);
78 6
        $this->doubleQuotes = $this->getOption($options, static::OPTION_DOUBLE_QUOTE, static::DEFAULT_DOUBLE_QUOTE);
79 6
        $this->newLines = $this->getOption($options, static::OPTION_NEW_LINES, static::DEFAULT_NEW_LINES);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getOption($option...tic::DEFAULT_NEW_LINES) 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...
80 6
        $this->null = $this->getOption($options, static::OPTION_NULL, static::DEFAULT_NULL);
81 6
        $this->boms = $this->getOption($options, static::OPTION_BOMS, static::DEFAULT_BOMS);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getOption($option..., static::DEFAULT_BOMS) 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...
82 6
        $this->encoding = $this->getOption($options, static::OPTION_ENCODING, static::DEFAULT_ENCODING);
83 6
    }
84
85
    /**
86
     * @param array  $options
87
     * @param string $name
88
     * @param mixed  $default
89
     *
90
     * @return mixed
91
     */
92 6
    private function getOption(array $options, $name, $default = null)
93
    {
94 6
        if (array_key_exists($name, $options)) {
95 1
            return $options[$name];
96
        } else {
97 5
            return $default;
98
        }
99
    }
100
101
    /**
102
     * @return string
103
     */
104 30
    public function getDelimiter()
105
    {
106 30
        return $this->delimiter;
107
    }
108
109
    /**
110
     * @return string
111
     */
112 30
    public function getQuote()
113
    {
114 30
        return $this->quote;
115
    }
116
117
    /**
118
     * @return string
119
     */
120 30
    public function getEscape()
121
    {
122 30
        return $this->escape;
123
    }
124
125
    /**
126
     * @return string[]
127
     */
128 30
    public function getNewLines()
129
    {
130 30
        return $this->newLines;
131
    }
132
133
    /**
134
     * @return bool
135
     */
136 28
    public function useDoubleQuotes()
137
    {
138 28
        return $this->doubleQuotes;
139
    }
140
141
    /**
142
     * @return string
143
     */
144 30
    public function getNullValue()
145
    {
146 30
        return $this->null;
147
    }
148
149
    /**
150
     * @return string[]
151
     */
152 30
    public function getBoms()
153
    {
154 30
        return $this->boms;
155
    }
156
157
    /**
158
     * @return string
159
     */
160 30
    public function getEncoding()
161
    {
162 30
        return $this->encoding;
163
    }
164
}
165