Completed
Pull Request — master (#178)
by ignace nyamagana
05:35
created

AbstractCsv   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 195
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 13
lcom 2
cbo 1
dl 0
loc 195
ccs 37
cts 37
cp 1
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A __destruct() 0 4 1
A createFromFileObject() 0 12 2
A createFromString() 0 7 1
A createFromPath() 0 12 3
A newInstance() 0 14 1
A newWriter() 0 4 1
A newReader() 0 4 1
A getIterator() 0 11 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
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
        $csv->header = $this->header;
179 54
180 3
        return $csv;
181
    }
182
183 51
    /**
184 3
     * Return a new {@link Writer} instance from a {@link AbstractCsv} object
185 2
     *
186
     * @param string $open_mode the file open mode flag
187 51
     *
188
     * @return Writer
189
     */
190
    public function newWriter($open_mode = 'r+')
191
    {
192
        return $this->newInstance(Writer::class, $open_mode);
193
    }
194
195
    /**
196
     * Return a new {@link Reader} instance from a {@link AbstractCsv} object
197
     *
198 6
     * @param string $open_mode the file open mode flag
199
     *
200 6
     * @return Reader
201 6
     */
202 6
    public function newReader($open_mode = 'r+')
203 6
    {
204 6
        return $this->newInstance(Reader::class, $open_mode);
205 6
    }
206 6
207 6
    /**
208
     * Returns the inner SplFileObject
209 6
     *
210
     * @return SplFileObject
211
     */
212
    public function getIterator()
213
    {
214
        $iterator = $this->path;
215
        if (!$iterator instanceof SplFileObject) {
216
            $iterator = new SplFileObject($this->getStreamFilterPath(), $this->open_mode);
217
        }
218
        $iterator->setCsvControl($this->delimiter, $this->enclosure, $this->escape);
219 3
        $iterator->setFlags(SplFileObject::READ_CSV | SplFileObject::READ_AHEAD | SplFileObject::SKIP_EMPTY);
220
221 3
        return $iterator;
222
    }
223
}
224