Completed
Pull Request — master (#178)
by ignace nyamagana
02:10
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
     * Returns the Record object
101
     *
102
     * @param Statement|null $stmt
103
     *
104
     * @return RecordSet
105 357
     */
106
    public function select(Statement $stmt = null)
107 357
    {
108 357
        $stmt = $stmt ?: new Statement();
109 357
110 357
        return $stmt->process($this);
111
    }
112
113
    /**
114
     * Return a new {@link AbstractCsv} from a SplFileObject
115 240
     *
116
     * @param SplFileObject $file
117 240
     *
118 240
     * @return static
119
     */
120
    public static function createFromFileObject(SplFileObject $file)
121
    {
122
        $csv = new static($file);
123
        $controls = $file->getCsvControl();
124
        $csv->setDelimiter($controls[0]);
125
        $csv->setEnclosure($controls[1]);
126
        if (isset($controls[2])) {
127 312
            $csv->setEscape($controls[2]);
128
        }
129 312
130
        return $csv;
131
    }
132
133
    /**
134
     * Return a new {@link AbstractCsv} from a string
135
     *
136
     * The string must be an object that implements the `__toString` method,
137
     * or a string
138
     *
139
     * @param string|object $str the string
140
     *
141
     * @return static
142 27
     */
143
    public static function createFromString($str)
144 27
    {
145 27
        $file = new SplTempFileObject();
146
        $file->fwrite(static::validateString($str));
147 27
148
        return new static($file);
149
    }
150
151
    /**
152
     * Return a new {@link AbstractCsv} from a string
153
     *
154
     * @param mixed  $path      file path
155
     * @param string $open_mode the file open mode flag
156
     *
157
     * @throws InvalidArgumentException If $path is a SplTempFileObject object
158
     *
159 90
     * @return static
160
     */
161 90
    public static function createFromPath($path, $open_mode = 'r+')
162 87
    {
163
        if ($path instanceof SplTempFileObject) {
164 3
            throw new InvalidArgumentException('an `SplTempFileObject` object does not contain a valid path');
165
        }
166
167
        if ($path instanceof SplFileInfo) {
168
            $path = $path->getPath().'/'.$path->getBasename();
169
        }
170
171
        return new static(static::validateString($path), $open_mode);
172
    }
173
174
    /**
175
     * Return a new {@link AbstractCsv} instance from another {@link AbstractCsv} object
176
     *
177 54
     * @param string $class     the class to be instantiated
178
     * @param string $open_mode the file open mode flag
179 54
     *
180 3
     * @return static
181
     */
182
    protected function newInstance($class, $open_mode)
183 51
    {
184 3
        $csv = new $class($this->path, $open_mode);
185 2
        $csv->delimiter = $this->delimiter;
186
        $csv->enclosure = $this->enclosure;
187 51
        $csv->escape = $this->escape;
188
        $csv->input_encoding = $this->input_encoding;
189
        $csv->input_bom = $this->input_bom;
190
        $csv->output_bom = $this->output_bom;
191
        $csv->newline = $this->newline;
192
        $csv->header = $this->header;
193
194
        return $csv;
195
    }
196
197
    /**
198 6
     * Return a new {@link Writer} instance from a {@link AbstractCsv} object
199
     *
200 6
     * @param string $open_mode the file open mode flag
201 6
     *
202 6
     * @return Writer
203 6
     */
204 6
    public function newWriter($open_mode = 'r+')
205 6
    {
206 6
        return $this->newInstance(Writer::class, $open_mode);
207 6
    }
208
209 6
    /**
210
     * Return a new {@link Reader} instance from a {@link AbstractCsv} object
211
     *
212
     * @param string $open_mode the file open mode flag
213
     *
214
     * @return Reader
215
     */
216
    public function newReader($open_mode = 'r+')
217
    {
218
        return $this->newInstance(Reader::class, $open_mode);
219 3
    }
220
221 3
    /**
222
     * Returns the inner SplFileObject
223
     *
224
     * @return SplFileObject
225
     */
226
    public function getIterator()
227
    {
228
        $iterator = $this->path;
229
        if (!$iterator instanceof SplFileObject) {
230
            $iterator = new SplFileObject($this->getStreamFilterPath(), $this->open_mode);
231 3
        }
232
        $iterator->setCsvControl($this->delimiter, $this->enclosure, $this->escape);
233 3
        $iterator->setFlags(SplFileObject::READ_CSV | SplFileObject::READ_AHEAD | SplFileObject::SKIP_EMPTY);
234
235
        return $iterator;
236
    }
237
}
238