CsvConfiguration::getEscape()   A
last analyzed

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
use InvalidArgumentException;
17
18
class CsvConfiguration implements CsvConfigurationInterface
19
{
20
    const DEFAULT_DELIMITER    = ',';
21
    const DEFAULT_NULL         = '\\N';
22
    const DEFAULT_QUOTE        = '"';
23
    const DEFAULT_ESCAPE       = '\\';
24
    const DEFAULT_DOUBLE_QUOTE = false;
25
    const DEFAULT_ENCODING     = 'UTF-8';
26
27
    const OPTION_DELIMITER    = 'delimiter';
28
    const OPTION_NULL         = 'null';
29
    const OPTION_NEW_LINES    = 'newLines';
30
    const OPTION_QUOTE        = 'quote';
31
    const OPTION_ESCAPE       = 'escape';
32
    const OPTION_DOUBLE_QUOTE = 'doubleQuote';
33
    const OPTION_BOMS         = 'boms';
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[] */
45
    private $newLines;
46
    /** @var string */
47
    private $null;
48
    /** @var string[] */
49
    private $boms;
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> `newLines`     string[] (Default: `["\n","\r","\r\n"]`)
62
     *                       <p> `null`         string (Default: `'\\N'`)
63
     *                       <p> `boms`         string[] (Default:
64
     *                       `[Bom::BOM_UTF8,Bom::BOM_UTF16_BE,Bom::BOM_UTF16_LE,Bom::BOM_UTF32_BE,Bom::BOM_UTF32_LE]`)
65
     *                       <p> `encoding`     string (Default: `'UTF-8'`)
66
     */
67 8
    public function __construct(array $options = [])
68
    {
69 8
        $this->delimiter = $this->getOption($options, static::OPTION_DELIMITER, static::DEFAULT_DELIMITER);
70 8
        $this->quote = $this->getOption($options, static::OPTION_QUOTE, static::DEFAULT_QUOTE);
71 8
        $this->escape = $this->getOption($options, static::OPTION_ESCAPE, static::DEFAULT_ESCAPE);
72 8
        $this->doubleQuotes = $this->getOption($options, static::OPTION_DOUBLE_QUOTE, static::DEFAULT_DOUBLE_QUOTE);
73 8
        $this->null = $this->getOption($options, static::OPTION_NULL, static::DEFAULT_NULL);
74 8
        $this->encoding = $this->getOption($options, static::OPTION_ENCODING, static::DEFAULT_ENCODING);
75 8
        $this->newLines = (array) $this->getOption(
76
            $options,
77 8
            static::OPTION_NEW_LINES,
78 8
            ["\n", "\r", "\r\n"],
79 8
            'is_array'
80
        );
81 7
        $this->boms = (array) $this->getOption(
82
            $options,
83 7
            static::OPTION_BOMS,
84
            [
85 7
                Bom::BOM_UTF8,
86
                Bom::BOM_UTF16_BE,
87
                Bom::BOM_UTF16_LE,
88
                Bom::BOM_UTF32_BE,
89
                Bom::BOM_UTF32_LE,
90
            ],
91 7
            'is_array'
92
        );
93 6
    }
94
95
    /**
96
     * @param array         $options
97
     * @param string        $name
98
     * @param mixed         $default
99
     * @param callable|null $type
100
     *
101
     * @return mixed
102
     */
103 8
    private function getOption(array $options, $name, $default = null, callable $type = null)
104
    {
105 8
        if (array_key_exists($name, $options)) {
106 3
            $result = $options[$name];
107
        } else {
108 7
            $result = $default;
109
        }
110 8
        if ($type && !call_user_func($type, $result)) {
111 2
            throw new InvalidArgumentException(
112 2
                "The provided option for {$name}: " . print_r($result, true) . " is invalid"
113
            );
114
        }
115
116 8
        return $result;
117
    }
118
119
    /**
120
     * @return string
121
     */
122 32
    public function getDelimiter()
123
    {
124 32
        return $this->delimiter;
125
    }
126
127
    /**
128
     * @return string
129
     */
130 32
    public function getQuote()
131
    {
132 32
        return $this->quote;
133
    }
134
135
    /**
136
     * @return string
137
     */
138 32
    public function getEscape()
139
    {
140 32
        return $this->escape;
141
    }
142
143
    /**
144
     * @return string[]
145
     */
146 32
    public function getNewLines()
147
    {
148 32
        return $this->newLines;
149
    }
150
151
    /**
152
     * @return bool
153
     */
154 30
    public function useDoubleQuotes()
155
    {
156 30
        return $this->doubleQuotes;
157
    }
158
159
    /**
160
     * @return string
161
     */
162 32
    public function getNullValue()
163
    {
164 32
        return $this->null;
165
    }
166
167
    /**
168
     * @return string[]
169
     */
170 32
    public function getBoms()
171
    {
172 32
        return $this->boms;
173
    }
174
175
    /**
176
     * @return string
177
     */
178 32
    public function getEncoding()
179
    {
180 32
        return $this->encoding;
181
    }
182
}
183