Skip to content

Commit

Permalink
Add 'InputFile::fromLocalFile()' (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
vjik authored Jun 5, 2024
1 parent efe11ac commit a347173
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ $api->sendMessage(
// Send local photo
$api->sendPhoto(
chatId: 22351,
photo: new InputFile(fopen('/path/to/photo.jpg', 'r')),
photo: InputFile::fromLocalFile('/path/to/photo.jpg'),
);
```

Expand Down
5 changes: 5 additions & 0 deletions src/Type/InputFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,9 @@ public function __construct(
public ?string $filename = null,
) {
}

public static function fromLocalFile(string $path, ?string $filename = null): self
{
return new self(fopen($path, 'r'), $filename);
}
}
16 changes: 16 additions & 0 deletions tests/Type/InputFileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,20 @@ public function testFilled(): void
$this->assertSame($stream, $file->resource);
$this->assertSame('file.txt', $file->filename);
}

public function testFromLocalFile(): void
{
$file = InputFile::fromLocalFile(__FILE__);

$this->assertIsResource($file->resource);
$this->assertNull($file->filename);
}

public function testFromLocalFileWithName(): void
{
$file = InputFile::fromLocalFile(__FILE__, 'test.php');

$this->assertIsResource($file->resource);
$this->assertSame('test.php', $file->filename);
}
}

0 comments on commit a347173

Please sign in to comment.