Skip to content

Commit

Permalink
Merge pull request #10 from digitalcz/file-from-named-constructor
Browse files Browse the repository at this point in the history
Add File::from named constructor
  • Loading branch information
spajxo authored Feb 13, 2024
2 parents 791762a + 6124896 commit db31d3b
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 0 deletions.
39 changes: 39 additions & 0 deletions src/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

namespace DigitalCz\Streams;

use InvalidArgumentException;
use Psr\Http\Message\StreamInterface as PsrStreamInterface;

final class File implements FileInterface
{
use StreamDecoratorTrait;
Expand Down Expand Up @@ -39,6 +42,42 @@ public static function temp(): self
return new self($path);
}

/**
* @param PsrStreamInterface|resource|string $from
*/
public static function from(mixed $from): self
{
if ($from instanceof PsrStreamInterface) {
$file = self::temp();
$file->copy($from);
$file->rewind();

return $file;
}

if (is_string($from)) {
if (file_exists($from)) {
return new self($from);
}

$file = self::temp();
$file->write($from);
$file->rewind();

return $file;
}

if (is_resource($from)) {
$file = self::temp();
$file->copy(new Stream($from));
$file->rewind();

return $file;
}

throw new InvalidArgumentException(sprintf('Cannot create %s from %s.', self::class, get_debug_type($from)));
}

public function getPath(): string
{
return $this->path;
Expand Down
45 changes: 45 additions & 0 deletions tests/FileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

namespace DigitalCz\Streams;

use InvalidArgumentException;
use PHPUnit\Framework\TestCase;
use stdClass;

/**
* @covers \DigitalCz\Streams\File
Expand Down Expand Up @@ -36,4 +38,47 @@ public function testTemp(): void
self::assertSame('plainfile', $file->getMetadata('wrapper_type'));
self::assertSame('STDIO', $file->getMetadata('stream_type'));
}

public function testFromStream(): void
{
$stream = Stream::from('test');
$file = File::from($stream);
self::assertSame('test', $file->getContents());
}

public function testFromString(): void
{
$file = File::from('test');
self::assertSame('test', $file->getContents());
}

public function testFromFilename(): void
{
$path = tempnam(sys_get_temp_dir(), 'temp');
self::assertNotFalse($path);
file_put_contents($path, 'test');

$file = File::from($path);
self::assertSame('test', $file->getContents());
}

public function testFromResource(): void
{
$resource = fopen('php://temp', 'wb+');
self::assertNotFalse($resource);
fwrite($resource, 'test');
fseek($resource, 0);

$file = File::from($resource);
self::assertSame('test', $file->getContents());
}

public function testFromInvalid(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Cannot create DigitalCz\Streams\File from stdClass');

$object = new stdClass();
File::from($object); // @phpstan-ignore-line
}
}

0 comments on commit db31d3b

Please sign in to comment.