Completed
Push — master ( d8a5e5...b3757c )
by ignace nyamagana
03:17
created

AbstractCsv::filterInteger()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

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