|
18 | 18 | use Generator; |
19 | 19 | use InvalidArgumentException; |
20 | 20 | use RuntimeException; |
| 21 | +use SplFileInfo; |
21 | 22 | use SplFileObject; |
22 | 23 | use Stringable; |
23 | 24 | use Throwable; |
| 25 | +use TypeError; |
24 | 26 |
|
25 | 27 | use function filter_var; |
26 | 28 | use function get_class; |
| 29 | +use function gettype; |
| 30 | +use function is_resource; |
27 | 31 | use function rawurlencode; |
28 | 32 | use function sprintf; |
29 | 33 | use function str_replace; |
@@ -79,42 +83,30 @@ public function __clone() |
79 | 83 | throw UnavailableStream::dueToForbiddenCloning(static::class); |
80 | 84 | } |
81 | 85 |
|
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 | | - |
100 | 86 | /** |
101 | 87 | * Returns a new instance from a string. |
102 | 88 | */ |
103 | | - public static function createFromString(Stringable|string $content = ''): static |
| 89 | + public static function fromString(Stringable|string $content = ''): static |
104 | 90 | { |
105 | | - return new static(Stream::createFromString($content)); |
| 91 | + return new static(Stream::fromString($content)); |
106 | 92 | } |
107 | 93 |
|
108 | 94 | /** |
109 | 95 | * Returns a new instance from a file path. |
110 | 96 | * |
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 |
112 | 100 | * |
113 | 101 | * @throws UnavailableStream |
114 | 102 | */ |
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 |
116 | 104 | { |
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 | + }; |
118 | 110 | } |
119 | 111 |
|
120 | 112 | /** |
@@ -621,4 +613,65 @@ public function addStreamFilter(string $filtername, ?array $params = null): stat |
621 | 613 |
|
622 | 614 | return $this->appendStreamFilterOnWrite($filtername, $params); |
623 | 615 | } |
| 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 | + } |
624 | 677 | } |
0 commit comments