Completed
Push — master ( a94b15...b2eb2c )
by ignace nyamagana
03:05
created

Controls::setFlags()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 9.4286
cc 1
eloc 4
nc 1
nop 1
crap 1
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 8.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
namespace League\Csv\Config;
14
15
use CallbackFilterIterator;
16
use InvalidArgumentException;
17
use LimitIterator;
18
use SplFileObject;
19
20
/**
21
 *  A trait to configure and check CSV file and content
22
 *
23
 * @package League.csv
24
 * @since  6.0.0
25
 *
26
 */
27
trait Controls
28
{
29
    /**
30
     * the field delimiter (one character only)
31
     *
32
     * @var string
33
     */
34
    protected $delimiter = ',';
35
36
    /**
37
     * the field enclosure character (one character only)
38
     *
39
     * @var string
40
     */
41
    protected $enclosure = '"';
42
43
    /**
44
     * the field escape character (one character only)
45
     *
46
     * @var string
47
     */
48
    protected $escape = '\\';
49
50
    /**
51
     * newline character
52
     *
53
     * @var string
54
     */
55
    protected $newline = "\n";
56
57
    /**
58
     * Sets the field delimiter
59
     *
60
     * @param string $delimiter
61
     *
62
     * @throws InvalidArgumentException If $delimiter is not a single character
63
     *
64
     * @return $this
65
     */
66 2
    public function setDelimiter($delimiter)
67
    {
68 2
        if (!$this->isValidCsvControls($delimiter)) {
69 2
            throw new InvalidArgumentException('The delimiter must be a single character');
70
        }
71 2
        $this->delimiter = $delimiter;
72
73 2
        return $this;
74
    }
75
76
    /**
77
     * Tell whether the submitted string is a valid CSV Control character
78
     *
79
     * @param string $str The submitted string
80
     *
81
     * @return bool
82
     */
83 12
    protected function isValidCsvControls($str)
84
    {
85 12
        return 1 == mb_strlen($str);
86
    }
87
88
    /**
89
     * Returns the current field delimiter
90
     *
91
     * @return string
92
     */
93 2
    public function getDelimiter()
94
    {
95 2
        return $this->delimiter;
96
    }
97
98
    /**
99
     * Detect Delimiters occurences in the CSV
100
     *
101
     * Returns a associative array where each key represents
102
     * a valid delimiter and each value the number of occurences
103
     *
104
     * @param string[] $delimiters the delimiters to consider
105
     * @param int      $nb_rows    Detection is made using $nb_rows of the CSV
106
     *
107
     * @return array
108
     */
109 8
    public function fetchDelimitersOccurrence(array $delimiters, $nb_rows = 1)
110
    {
111 8
        $nb_rows = $this->filterInteger($nb_rows, 1, 'The number of rows to consider must be a valid positive integer');
112 6
        $filterRow = function ($row) {
113 6
            return is_array($row) && count($row) > 1;
114 6
        };
115 6
        $delimiters = array_unique(array_filter($delimiters, [$this, 'isValidCsvControls']));
116 6
        $csv = $this->getIterator();
117 6
        $res = [];
118 6
        foreach ($delimiters as $delim) {
119 6
            $csv->setCsvControl($delim, $this->enclosure, $this->escape);
120 6
            $iterator = new CallbackFilterIterator(new LimitIterator($csv, 0, $nb_rows), $filterRow);
121 6
            $res[$delim] = count(iterator_to_array($iterator, false), COUNT_RECURSIVE);
122 6
        }
123 6
        arsort($res, SORT_NUMERIC);
124
125 6
        return $res;
126
    }
127
128
    /**
129
     * @inheritdoc
130
     */
131
    abstract protected function filterInteger($int, $minValue, $errorMessage);
132
133
    /**
134
     * Returns the CSV Iterator
135
     *
136
     * @return SplFileObject
137
     */
138
    abstract public function getIterator();
139
140
    /**
141
     * Sets the field enclosure
142
     *
143
     * @param string $enclosure
144
     *
145
     * @throws InvalidArgumentException If $enclosure is not a single character
146
     *
147
     * @return $this
148
     */
149 2
    public function setEnclosure($enclosure)
150
    {
151 2
        if (!$this->isValidCsvControls($enclosure)) {
152 2
            throw new InvalidArgumentException('The enclosure must be a single character');
153
        }
154 2
        $this->enclosure = $enclosure;
155
156 2
        return $this;
157
    }
158
159
    /**
160
     * Returns the current field enclosure
161
     *
162
     * @return string
163
     */
164 16
    public function getEnclosure()
165
    {
166 16
        return $this->enclosure;
167
    }
168
169
    /**
170
     * Sets the field escape character
171
     *
172
     * @param string $escape
173
     *
174
     * @throws InvalidArgumentException If $escape is not a single character
175
     *
176
     * @return $this
177
     */
178 2
    public function setEscape($escape)
179
    {
180 2
        if (!$this->isValidCsvControls($escape)) {
181 2
            throw new InvalidArgumentException('The escape character must be a single character');
182
        }
183 2
        $this->escape = $escape;
184
185 2
        return $this;
186
    }
187
188
    /**
189
     * Returns the current field escape character
190
     *
191
     * @return string
192
     */
193 2
    public function getEscape()
194
    {
195 2
        return $this->escape;
196
    }
197
198
    /**
199
     * Sets the newline sequence characters
200
     *
201
     * @param string $newline
202
     *
203
     * @return static
204
     */
205 4
    public function setNewline($newline)
206
    {
207 4
        $this->newline = (string) $newline;
208
209 4
        return $this;
210
    }
211
212
    /**
213
     * Returns the current newline sequence characters
214
     *
215
     * @return string
216
     */
217 4
    public function getNewline()
218
    {
219 4
        return $this->newline;
220
    }
221
}
222