Skip to content

Commit f4ed03a

Browse files
committed
Simplify Reader and Writer instantiation
1 parent 7fce732 commit f4ed03a

32 files changed

+365
-239
lines changed

CHANGELOG.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,29 @@
22

33
All Notable changes to `Csv` will be documented in this file
44

5+
## [Next](https://github.com/thephpleague/csv/compare/9.26.0...master) - TBD
6+
7+
### Added
8+
9+
- `AbstractCsv::from` and `AbstractCsv::fromString` to simplify instantiating `Reader` and `Writer` instance.
10+
- `Stream::from` and `Stream::fromString` to simplify internal instantiation of `Stream` instances.
11+
12+
### Deprecated
13+
14+
- `AbstractCsv::createFromString` use `AbstractCsv::fromString` instead
15+
- `AbstractCsv::createFromStream` use `AbstractCsv::from` instead
16+
- `AbstractCsv::createFromFileObject` use `AbstractCsv::from` instead
17+
- `Stream::createFromResource` use `Stream::from` instead
18+
- `Stream::createFromString` use `Stream::fromString` instead
19+
20+
### Fixed
21+
22+
- `None
23+
24+
### Remove
25+
26+
- None
27+
528
## [9.26.0](https://github.com/thephpleague/csv/compare/9.25.0...9.26.0) - 2025-10-01
629

730
### Added

src/AbstractCsv.php

Lines changed: 76 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,16 @@
1818
use Generator;
1919
use InvalidArgumentException;
2020
use RuntimeException;
21+
use SplFileInfo;
2122
use SplFileObject;
2223
use Stringable;
2324
use Throwable;
25+
use TypeError;
2426

2527
use function filter_var;
2628
use function get_class;
29+
use function gettype;
30+
use function is_resource;
2731
use function rawurlencode;
2832
use function sprintf;
2933
use function str_replace;
@@ -79,42 +83,30 @@ public function __clone()
7983
throw UnavailableStream::dueToForbiddenCloning(static::class);
8084
}
8185

82-
/**
83-
* Returns a new instance from a SplFileObject.
84-
*/
85-
public static function createFromFileObject(SplFileObject $file): static
86-
{
87-
return new static($file);
88-
}
89-
90-
/**
91-
* Returns a new instance from a PHP resource stream.
92-
*
93-
* @param resource $stream
94-
*/
95-
public static function createFromStream($stream): static
96-
{
97-
return new static(Stream::createFromResource($stream));
98-
}
99-
10086
/**
10187
* Returns a new instance from a string.
10288
*/
103-
public static function createFromString(Stringable|string $content = ''): static
89+
public static function fromString(Stringable|string $content = ''): static
10490
{
105-
return new static(Stream::createFromString($content));
91+
return new static(Stream::fromString($content));
10692
}
10793

10894
/**
10995
* Returns a new instance from a file path.
11096
*
111-
* @param resource|null $context the resource context
97+
* @param SplFileInfo|SplFileObject|resource|string $filename an SPL file object, a resource stream or a file path
98+
* @param non-empty-string $mode the file path open mode used with a file path or a SplFileInfo object
99+
* @param resource|null $context the resource context used with a file pathor a SplFileInfo object
112100
*
113101
* @throws UnavailableStream
114102
*/
115-
public static function createFromPath(string $path, string $open_mode = 'r+', $context = null): static
103+
public static function from($filename, string $mode = 'r+', $context = null): static
116104
{
117-
return new static(Stream::createFromPath($path, $open_mode, $context));
105+
return match (true) {
106+
$filename instanceof SplFileObject => new static($filename),
107+
$filename instanceof SplFileInfo => new static($filename->openFile(mode: $mode, context: $context)),
108+
default => new static(Stream::from($filename, $mode, $context)),
109+
};
118110
}
119111

120112
/**
@@ -621,4 +613,65 @@ public function addStreamFilter(string $filtername, ?array $params = null): stat
621613

622614
return $this->appendStreamFilterOnWrite($filtername, $params);
623615
}
616+
617+
/**
618+
* DEPRECATION WARNING! This method will be removed in the next major point release.
619+
* @codeCoverageIgnore
620+
* @deprecated since version 9.27.0
621+
*
622+
* Returns a new instance from a SplFileObject.
623+
*/
624+
#[Deprecated(message:'use League\Csv\AbstractCsv::from() instead', since:'league/csv:9.27.0')]
625+
public static function createFromFileObject(SplFileObject $file): static
626+
{
627+
return new static($file);
628+
}
629+
630+
/**
631+
* DEPRECATION WARNING! This method will be removed in the next major point release.
632+
* @codeCoverageIgnore
633+
* @deprecated since version 9.27.0
634+
*
635+
* Returns a new instance from a PHP resource stream.
636+
*
637+
* @param resource $stream
638+
*/
639+
#[Deprecated(message:'use League\Csv\AbstractCsv::from() instead', since:'league/csv:9.27.0')]
640+
public static function createFromStream($stream): static
641+
{
642+
is_resource($stream) || throw new TypeError('Argument passed must be a stream resource or a string, '.gettype($stream).' given.');
643+
644+
return new static(Stream::from($stream));
645+
}
646+
647+
/**
648+
* DEPRECATION WARNING! This method will be removed in the next major point release.
649+
* @codeCoverageIgnore
650+
* @deprecated since version 9.27.0
651+
*
652+
* Returns a new instance from a string.
653+
*/
654+
#[Deprecated(message:'use League\Csv\AbstractCsv::fromString() instead', since:'league/csv:9.27.0')]
655+
public static function createFromString(Stringable|string $content = ''): static
656+
{
657+
return self::fromString($content);
658+
}
659+
660+
/**
661+
* DEPRECATION WARNING! This method will be removed in the next major point release.
662+
* @codeCoverageIgnore
663+
* @deprecated since version 9.27.0
664+
*
665+
* Returns a new instance from a file path.
666+
*
667+
* @param non-empty-string $open_mode
668+
* @param resource|null $context the resource context
669+
*
670+
* @throws UnavailableStream
671+
*/
672+
#[Deprecated(message:'use League\Csv\AbstractCsv::from() instead', since:'league/csv:9.27.0')]
673+
public static function createFromPath(string $path, string $open_mode = 'r+', $context = null): static
674+
{
675+
return new static(Stream::from($path, $open_mode, $context));
676+
}
624677
}

0 commit comments

Comments
 (0)