Skip to content

Commit 137dcde

Browse files
committed
Improve CSV named constructor
1 parent 44f91ba commit 137dcde

3 files changed

Lines changed: 77 additions & 28 deletions

File tree

docs/9.0/connections/instantiation.md

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ description: The different ways the package allow to load and interact with CSV
88

99
Because CSV documents come in different forms, we use named constructors to offer several ways to load them.
1010

11-
## Loading from a string
11+
## New API
12+
13+
### Loading from a string
1214

1315
<p class="message-notice">This new API is introduced in version <code>9.27.0</code></p>
1416

@@ -28,7 +30,53 @@ $writer = Writer::fromString('john,doe,john.doe@example.com');
2830

2931
<p class="message-notice">The <code>$content</code> argument default value is an empty string to ease usage.</p>
3032

31-
## Loading from a file pointer
33+
### Loading from a path
34+
35+
<div class="message-info">Since version <code>9.29.0</code></div>
36+
<div class="message-notice">Since version <code>9.27.0</code> the <code>createFromPath()</code> method is <strong>deprecated</strong></div>
37+
38+
```php
39+
public static Reader::fromPath(SplFileInfo|string $path, string $mode = 'r', ?resource $context = null): Reader
40+
public static Writer::fromPath(SplFileInfo|string $path, string $mode = 'r+', ?resource $context = null): Writer
41+
```
42+
43+
Creates a new object *à la* `fopen`.
44+
45+
```php
46+
use League\Csv\Reader;
47+
use League\Csv\Writer;
48+
49+
$reader = Reader::fromPath('/path/to/your/csv/file.csv', 'r');
50+
$writer = Writer::fromPath(new SplFileInfo('/path/to/your/csv/file.csv'), 'w');
51+
```
52+
53+
<p class="message-warning">A <code>SplFileObject</code> does not expose its context. If it was created with one, you must pass it explicitly to the <code>$context</code> argument.
54+
Alternatively, you can use the <code>fromStream</code> method.</p>
55+
56+
### Loading from stream
57+
58+
<div class="message-info">Since version <code>9.29.0</code></div>
59+
<div class="message-notice">Since version <code>9.27.0</code> the <code>createFromStream()</code> and <code>createFromFileObject()</code> methods are <strong>deprecated</strong></div>
60+
61+
```php
62+
public static AbstractCsv::fromStream(SplFileObject|resource $stream): self
63+
```
64+
Creates a new object from a stream resource or a streaming object.
65+
66+
```php
67+
use League\Csv\Reader;
68+
use League\Csv\Writer;
69+
70+
$reader = Reader::fromStream(fopen('/path/to/the/file.csv', 'r+'));
71+
$writer = Writer::fromStream(tmpfile());
72+
$reader = Reader::fromStream(new SplFileObject('/path/to/your/csv/file.csv'));
73+
$writer = Writer::fromStream(new SplTempFileObject());
74+
```
75+
76+
The provided stream—whether a resource or a SplFileObject—is used as-is. It is the developer’s responsibility to ensure that the stream
77+
is valid and has the appropriate permissions; otherwise, exceptions may be thrown during use.
78+
79+
### Loading from a file pointer
3280

3381
<p class="message-notice">This new API is introduced in version <code>9.27.0</code></p>
3482

@@ -46,7 +94,7 @@ is created *à la* `fopen` and the `$mode` and `$context` parameters are taken i
4694
Otherwise, when a stream resource or an `SplFileObject` instance is given, both arguments are
4795
ignored.
4896

49-
<div class="message-notice">Since version <code>9.27.0</code> the following methods are <strong>deprecated</strong>:
97+
<div class="message-notice">Since version <code>9.27.0</code> this method can be use to replace the <strong>deprecated</strong> methods:
5098
<ul>
5199
<li><code>createFromPath()</code></li>
52100
<li><code>createFromStream()</code></li>

src/AbstractCsv.php

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -86,24 +86,6 @@ public function __clone()
8686
throw UnavailableStream::dueToForbiddenCloning(static::class);
8787
}
8888

89-
/**
90-
* Returns a new instance from a file path.
91-
*
92-
* @param SplFileInfo|SplFileObject|resource|string $filename an SPL file object, a resource stream or a file path
93-
* @param non-empty-string $mode the file path open mode used with a file path or a SplFileInfo object
94-
* @param resource|null $context the resource context used with a file pathor a SplFileInfo object
95-
*
96-
* @throws UnavailableStream
97-
*/
98-
public static function from($filename, string $mode = 'r+', $context = null): static
99-
{
100-
return match (true) {
101-
$filename instanceof SplFileObject => new static($filename),
102-
$filename instanceof SplFileInfo => new static($filename->openFile(mode: $mode, context: $context)),
103-
default => new static(Stream::from($filename, $mode, $context)),
104-
};
105-
}
106-
10789
/**
10890
* @param SplFileInfo|string $path an SPL file object, a file path or a stream URI
10991
* @param non-empty-string $mode the file path open mode
@@ -113,10 +95,7 @@ public static function from($filename, string $mode = 'r+', $context = null): st
11395
*/
11496
public static function fromPath(SplFileInfo|string $path, string $mode = 'r', $context = null): static
11597
{
116-
return match (true) {
117-
$path instanceof SplFileInfo => static::from(filename: $path->openFile(mode: $mode, context: $context)),
118-
default => static::from(filename: $path, mode: $mode, context: $context),
119-
};
98+
return static::from(filename: $path, mode: $mode, context: $context);
12099
}
121100

122101
/**
@@ -129,8 +108,8 @@ public static function fromPath(SplFileInfo|string $path, string $mode = 'r', $c
129108
public static function fromStream($stream): static
130109
{
131110
if (!$stream instanceof SplFileObject) {
132-
is_resource($stream) || throw new TypeError('Argument passed must be a stream resource, '.gettype($stream).' given.');
133-
'stream' === ($type = get_resource_type($stream)) || throw new TypeError('Argument passed must be a stream resource, '.$type.' resource given');
111+
is_resource($stream) || throw new TypeError('Argument passed must be a stream resource or an SplFileObject instance, '.gettype($stream).' given.');
112+
'stream' === ($type = get_resource_type($stream)) || throw new TypeError('Argument passed must be a stream resource or an SplFileObject instance, '.$type.' resource given');
134113
}
135114

136115
return static::from(filename: $stream);
@@ -144,6 +123,23 @@ public static function fromString(Stringable|string $content = ''): static
144123
return new static(document: Stream::fromString($content));
145124
}
146125

126+
/**
127+
* Returns a new instance from a file path.
128+
*
129+
* @param SplFileInfo|SplFileObject|resource|string $filename an SPL file object, a resource stream or a file path
130+
* @param non-empty-string $mode the file path open mode used with a file path or a SplFileInfo object
131+
* @param resource|null $context the resource context used with a file pathor a SplFileInfo object
132+
*
133+
* @throws UnavailableStream
134+
*/
135+
public static function from($filename, string $mode = 'r+', $context = null): static
136+
{
137+
return new static($filename instanceof SplFileObject
138+
? $filename
139+
: Stream::from($filename, $mode, $context)
140+
);
141+
}
142+
147143
/**
148144
* Returns the current field delimiter.
149145
*/

src/Stream.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Deprecated;
1717
use RuntimeException;
1818
use SeekableIterator;
19+
use SplFileInfo;
1920
use SplFileObject;
2021
use Stringable;
2122
use TypeError;
@@ -124,14 +125,18 @@ public function ftell(): int|false
124125
/**
125126
* Returns a new instance from a file path.
126127
*
127-
* @param resource|string $filename
128+
* @param resource|string|SplFileInfo $filename
128129
* @param resource|null $context
129130
*
130131
* @throws UnavailableStream if the stream resource cannot be created
131132
*/
132133
public static function from($filename, string $mode = 'r', $context = null): self
133134
{
134135
$should_close_stream = false;
136+
if ($filename instanceof SplFileInfo) {
137+
$filename = $filename->getPathname();
138+
}
139+
135140
if (is_string($filename)) {
136141
$should_close_stream = true;
137142
/** @var resource|false $resource */

0 commit comments

Comments
 (0)