Completed
Pull Request — master (#178)
by ignace nyamagana
02:43
created

AbstractCsv::validateString()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 4

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 7
ccs 3
cts 3
cp 1
rs 9.2
cc 4
eloc 4
nc 2
nop 1
crap 4
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.1.1
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;
14
15
use InvalidArgumentException;
16
use IteratorAggregate;
17
use League\Csv\Config\Controls;
18
use SplFileInfo;
19
use SplFileObject;
20
use SplTempFileObject;
21
22
/**
23
 *  An abstract class to enable basic CSV manipulation
24
 *
25
 * @package League.csv
26
 * @since  4.0.0
27
 *
28
 */
29
abstract class AbstractCsv implements IteratorAggregate
30
{
31
    use Controls;
32
33
    /**
34
     *  UTF-8 BOM sequence
35
     */
36
    const BOM_UTF8 = "\xEF\xBB\xBF";
37
38
    /**
39
     * UTF-16 BE BOM sequence
40
     */
41
    const BOM_UTF16_BE = "\xFE\xFF";
42
43
    /**
44
     * UTF-16 LE BOM sequence
45
     */
46
    const BOM_UTF16_LE = "\xFF\xFE";
47
48
    /**
49
     * UTF-32 BE BOM sequence
50
     */
51
    const BOM_UTF32_BE = "\x00\x00\xFE\xFF";
52
53
    /**
54
     * UTF-32 LE BOM sequence
55
     */
56
    const BOM_UTF32_LE = "\x00\x00\xFF\xFE";
57
58
    /**
59
     * The constructor path
60
     *
61
     * can be a SplFileInfo object or the string path to a file
62
     *
63
     * @var SplFileObject|string
64
     */
65
    protected $path;
66
67
    /**
68
     * The file open mode flag
69
     *
70
     * @var string
71
     */
72
    protected $open_mode;
73
74
    /**
75
     * Creates a new instance
76
     *
77
     * The path must be an SplFileInfo object
78
     * an object that implements the `__toString` method
79
     * a path to a file
80
     *
81
     * @param SplFileObject|string $path      The file path
82
     * @param string               $open_mode The file open mode flag
83
     */
84
    protected function __construct($path, $open_mode = 'r+')
85
    {
86
        $this->open_mode = strtolower($open_mode);
87
        $this->path = $path;
88
        $this->initStreamFilter($this->path);
89
    }
90
91
    /**
92
     * The destructor
93
     */
94
    public function __destruct()
95
    {
96
        $this->path = null;
97
    }
98
99
    /**
100
     * Return a new {@link AbstractCsv} from a SplFileObject
101
     *
102
     * @param SplFileObject $file
103
     *
104
     * @return static
105 357
     */
106
    public static function createFromFileObject(SplFileObject $file)
107 357
    {
108 357
        $csv = new static($file);
109 357
        $controls = $file->getCsvControl();
110 357
        $csv->setDelimiter($controls[0]);
111
        $csv->setEnclosure($controls[1]);
112
        if (isset($controls[2])) {
113
            $csv->setEscape($controls[2]);
114
        }
115 240
116
        return $csv;
117 240
    }
118 240
119
    /**
120
     * Return a new {@link AbstractCsv} from a string
121
     *
122
     * The string must be an object that implements the `__toString` method,
123
     * or a string
124
     *
125
     * @param string|object $str the string
126
     *
127 312
     * @return static
128
     */
129 312
    public static function createFromString($str)
130
    {
131
        $file = new SplTempFileObject();
132
        $file->fwrite(static::validateString($str));
133
134
        return new static($file);
135
    }
136
137
    /**
138
     * Return a new {@link AbstractCsv} from a string
139
     *
140
     * @param mixed  $path      file path
141
     * @param string $open_mode the file open mode flag
142 27
     *
143
     * @throws InvalidArgumentException If $path is a SplTempFileObject object
144 27
     *
145 27
     * @return static
146
     */
147 27
    public static function createFromPath($path, $open_mode = 'r+')
148
    {
149
        if ($path instanceof SplTempFileObject) {
150
            throw new InvalidArgumentException('an `SplTempFileObject` object does not contain a valid path');
151
        }
152
153
        if ($path instanceof SplFileInfo) {
154
            $path = $path->getPath().'/'.$path->getBasename();
155
        }
156
157
        return new static(static::validateString($path), $open_mode);
158
    }
159 90
160
    /**
161 90
     * Return a new {@link AbstractCsv} instance from another {@link AbstractCsv} object
162 87
     *
163
     * @param string $class     the class to be instantiated
164 3
     * @param string $open_mode the file open mode flag
165
     *
166
     * @return static
167
     */
168
    protected function newInstance($class, $open_mode)
169
    {
170
        $csv = new $class($this->path, $open_mode);
171
        $csv->delimiter = $this->delimiter;
172
        $csv->enclosure = $this->enclosure;
173
        $csv->escape = $this->escape;
174
        $csv->input_encoding = $this->input_encoding;
175
        $csv->input_bom = $this->input_bom;
176
        $csv->output_bom = $this->output_bom;
177 54
        $csv->newline = $this->newline;
178
179 54
        return $csv;
180 3
    }
181
182
    /**
183 51
     * Return a new {@link Writer} instance from a {@link AbstractCsv} object
184 3
     *
185 2
     * @param string $open_mode the file open mode flag
186
     *
187 51
     * @return Writer
188
     */
189
    public function newWriter($open_mode = 'r+')
190
    {
191
        return $this->newInstance(Writer::class, $open_mode);
192
    }
193
194
    /**
195
     * Return a new {@link Reader} instance from a {@link AbstractCsv} object
196
     *
197
     * @param string $open_mode the file open mode flag
198 6
     *
199
     * @return Reader
200 6
     */
201 6
    public function newReader($open_mode = 'r+')
202 6
    {
203 6
        return $this->newInstance(Reader::class, $open_mode);
204 6
    }
205 6
206 6
    /**
207 6
     * Returns the inner SplFileObject
208
     *
209 6
     * @return SplFileObject
210
     */
211
    public function getIterator()
212
    {
213
        $iterator = $this->path;
214
        if (!$iterator instanceof SplFileObject) {
215
            $iterator = new SplFileObject($this->getStreamFilterPath(), $this->open_mode);
216
        }
217
        $iterator->setCsvControl($this->delimiter, $this->enclosure, $this->escape);
218
        $iterator->setFlags(SplFileObject::READ_CSV | SplFileObject::READ_AHEAD | SplFileObject::SKIP_EMPTY);
219 3
220
        return $iterator;
221 3
    }
222
223
    /**
224
     * Returns the Record object
225
     *
226
     * @param Query|null $query
227
     *
228
     * @return Records
229
     */
230
    public function getRecords(Query $query = null)
231 3
    {
232
        $query = $query ?: new Query();
233 3
234
        return $query($this);
235
    }
236
}
237