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

CsvConfiguration::__construct()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 27
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 23
CRAP Score 1

Importance

Changes 5
Bugs 0 Features 1
Metric Value
c 5
b 0
f 1
dl 0
loc 27
ccs 23
cts 23
cp 1
rs 8.8571
cc 1
eloc 21
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
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 8
            $options,
77 8
            static::OPTION_NEW_LINES,
78 8
            ["\n", "\r", "\r\n"],
79
            'is_array'
80 8
        );
81 7
        $this->boms = (array) $this->getOption(
82 7
            $options,
83 7
            static::OPTION_BOMS,
84
            [
85 7
                Bom::BOM_UTF8,
86 7
                Bom::BOM_UTF16_BE,
87 7
                Bom::BOM_UTF16_LE,
88 7
                Bom::BOM_UTF32_BE,
89 7
                Bom::BOM_UTF32_LE,
90 7
            ],
91
            'is_array'
92 7
        );
93 6
    }
94
95
    /**
96
     * @param array    $options
97
     * @param string   $name
98
     * @param mixed    $default
99
     * @param callable $type
0 ignored issues
show
Documentation introduced by
Should the type for parameter $type not be null|callable?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
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 3
        } else {
108 7
            $result = $default;
109
        }
110 8
        if ($type) {
111 8
            if (!call_user_func($type, $result)) {
112 2
                throw new InvalidArgumentException(
113 2
                    "The value: " . print_r($result, true) . " for option: {$name} is invalid"
114 2
                );
115
            }
116 7
        }
117
118 8
        return $result;
119
    }
120
121
    /**
122
     * @return string
123
     */
124 30
    public function getDelimiter()
125
    {
126 30
        return $this->delimiter;
127
    }
128
129
    /**
130
     * @return string
131
     */
132 30
    public function getQuote()
133
    {
134 30
        return $this->quote;
135
    }
136
137
    /**
138
     * @return string
139
     */
140 30
    public function getEscape()
141
    {
142 30
        return $this->escape;
143
    }
144
145
    /**
146
     * @return string[]
147
     */
148 30
    public function getNewLines()
149
    {
150 30
        return $this->newLines;
151
    }
152
153
    /**
154
     * @return bool
155
     */
156 28
    public function useDoubleQuotes()
157
    {
158 28
        return $this->doubleQuotes;
159
    }
160
161
    /**
162
     * @return string
163
     */
164 30
    public function getNullValue()
165
    {
166 30
        return $this->null;
167
    }
168
169
    /**
170
     * @return string[]
171
     */
172 30
    public function getBoms()
173
    {
174 30
        return $this->boms;
175
    }
176
177
    /**
178
     * @return string
179
     */
180 30
    public function getEncoding()
181
    {
182 30
        return $this->encoding;
183
    }
184
}
185