Completed
Pull Request — master (#210)
by ignace nyamagana
02:41
created

ControlsTrait::fetchDelimitersOccurrence()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 21
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 9.3142
c 0
b 0
f 0
cc 3
eloc 15
nc 2
nop 2
1
<?php
2
/**
3
* This file is part of the League.csv library
4
*
5
* @license http://opensource.org/licenses/MIT
6
* @link https://github.com/thephpleague/csv/
7
* @version 9.0.0
8
* @package League.csv
9
*
10
* For the full copyright and license information, please view the LICENSE
11
* file that was distributed with this source code.
12
*/
13
declare(strict_types=1);
14
15
namespace League\Csv\Config;
16
17
use InvalidArgumentException;
18
use League\Csv\AbstractCsv;
19
use SplFileObject;
20
21
/**
22
 *  An abstract class to enable basic CSV manipulation
23
 *
24
 * @package League.csv
25
 * @since  9.0.0
26
 * @internal
27
 */
28
trait ControlsTrait
29
{
30
    use ValidatorTrait;
31
32
    /**
33
     * the field delimiter (one character only)
34
     *
35
     * @var string
36
     */
37
    protected $delimiter = ',';
38
39
    /**
40
     * the field enclosure character (one character only)
41
     *
42
     * @var string
43
     */
44
    protected $enclosure = '"';
45
46
    /**
47
     * the field escape character (one character only)
48
     *
49
     * @var string
50
     */
51
    protected $escape = '\\';
52
53
    /**
54
     * The Input file BOM character
55
     * @var string
56
     */
57
    protected $input_bom;
58
59
    /**
60
     * The Output file BOM character
61
     * @var string
62
     */
63
    protected $output_bom = '';
64
65
    /**
66
     * Returns the inner CSV Document Iterator object
67
     *
68
     * @return StreamIterator|SplFileObject
69
     */
70
    abstract public function getDocument();
71
72
    /**
73
     * Returns the current field delimiter
74
     *
75
     * @return string
76
     */
77 124
    public function getDelimiter(): string
78
    {
79 124
        return $this->delimiter;
80
    }
81
82
    /**
83
     * Returns the current field enclosure
84
     *
85
     * @return string
86
     */
87 124
    public function getEnclosure(): string
88
    {
89 124
        return $this->enclosure;
90
    }
91
92
    /**
93
     * Returns the current field escape character
94
     *
95
     * @return string
96
     */
97 124
    public function getEscape(): string
98
    {
99 124
        return $this->escape;
100
    }
101
102
    /**
103
     * Returns the BOM sequence in use on Output methods
104
     *
105
     * @return string
106
     */
107 2
    public function getOutputBOM(): string
108
    {
109 2
        return $this->output_bom;
110
    }
111
112
    /**
113
     * Returns the BOM sequence of the given CSV
114
     *
115
     * @return string
116
     */
117 152
    public function getInputBOM(): string
118
    {
119 152
        if (null === $this->input_bom) {
120
            $bom = [
121 152
                AbstractCsv::BOM_UTF32_BE, AbstractCsv::BOM_UTF32_LE,
122
                AbstractCsv::BOM_UTF16_BE, AbstractCsv::BOM_UTF16_LE, AbstractCsv::BOM_UTF8,
123
            ];
124 152
            $csv = $this->getDocument();
125 152
            $csv->setFlags(SplFileObject::READ_CSV);
126 152
            $csv->rewind();
127 152
            $line = $csv->fgets();
128 152
            $res  = array_filter($bom, function ($sequence) use ($line) {
129 152
                return strpos($line, $sequence) === 0;
130 152
            });
131
132 152
            $this->input_bom = (string) array_shift($res);
133
        }
134
135 152
        return $this->input_bom;
136
    }
137
138
    /**
139
     * Sets the field delimiter
140
     *
141
     * @param string $delimiter
142
     *
143
     * @throws InvalidArgumentException If $delimiter is not a single character
144
     *
145
     * @return $this
146
     */
147 214
    public function setDelimiter(string $delimiter): self
148
    {
149 214
        $this->delimiter = $this->filterControl($delimiter, 'delimiter');
150
151 214
        return $this;
152
    }
153
154
    /**
155
     * Sets the field enclosure
156
     *
157
     * @param string $enclosure
158
     *
159
     * @throws InvalidArgumentException If $enclosure is not a single character
160
     *
161
     * @return $this
162
     */
163 188
    public function setEnclosure(string $enclosure): self
164
    {
165 188
        $this->enclosure = $this->filterControl($enclosure, 'enclosure');
166
167 188
        return $this;
168
    }
169
170
    /**
171
     * Sets the field escape character
172
     *
173
     * @param string $escape
174
     *
175
     * @throws InvalidArgumentException If $escape is not a single character
176
     *
177
     * @return $this
178
     */
179 188
    public function setEscape(string $escape): self
180
    {
181 188
        $this->escape = $this->filterControl($escape, 'escape');
182
183 188
        return $this;
184
    }
185
186
    /**
187
     * Sets the BOM sequence to prepend the CSV on output
188
     *
189
     * @param string $str The BOM sequence
190
     *
191
     * @return static
192
     */
193 6
    public function setOutputBOM(string $str): self
194
    {
195 6
        $this->output_bom = $str;
196
197 6
        return $this;
198
    }
199
}
200