Skip to content

Commit

Permalink
Add MimeSniffer constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
olivervogel committed May 4, 2024
1 parent 6fb8878 commit 6b11023
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 8 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ If your prefer non-static initialization:
use Intervention\MimeSniffer\MimeSniffer;

// create instance with constructor
$sniffer = new MimeSniffer();
$sniffer = new MimeSniffer($content);

// with setter for given content
$type = $sniffer->setFromString($other_content)->getType();
Expand Down
29 changes: 22 additions & 7 deletions src/MimeSniffer.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
namespace Intervention\MimeSniffer;

use Intervention\MimeSniffer\Interfaces\TypeInterface;
use InvalidArgumentException;
use RuntimeException;

class MimeSniffer
{
Expand All @@ -16,26 +18,35 @@ class MimeSniffer
protected string $content;

/**
* Universal factory method
* Create new instance
*
* @param mixed $content
* @return MimeSniffer
* @return void
*/
public static function create(mixed $content): self
public function __construct(mixed $content = null)
{
if (is_string($content) && file_exists($content)) {
return self::createFromFilename($content);
$this->setFromFilename($content);
}

if (is_string($content)) {
return self::createFromString($content);
$this->setFromString($content);
}

if (is_resource($content)) {
return self::createFromPointer($content);
$this->setFromPointer($content);
}
}

return new self();
/**
* Universal factory method
*
* @param mixed $content
* @return MimeSniffer
*/
public static function create(mixed $content): self
{
return new self($content);
}

/**
Expand Down Expand Up @@ -113,6 +124,10 @@ public static function createFromPointer($pointer): self
*/
public function setFromPointer($pointer): self
{
if (!is_resource($pointer)) {
throw new InvalidArgumentException('Argument #1 $pointer must be of type resource.');

Check failure on line 128 in src/MimeSniffer.php

View workflow job for this annotation

GitHub Actions / Testing on PHP 8.3

Method Intervention\MimeSniffer\MimeSniffer::setFromPointer() throws checked exception InvalidArgumentException but it's missing from the PHPDoc @throws tag.

Check failure on line 128 in src/MimeSniffer.php

View workflow job for this annotation

GitHub Actions / Testing on PHP 8.1

Method Intervention\MimeSniffer\MimeSniffer::setFromPointer() throws checked exception InvalidArgumentException but it's missing from the PHPDoc @throws tag.

Check failure on line 128 in src/MimeSniffer.php

View workflow job for this annotation

GitHub Actions / Testing on PHP 8.3

Method Intervention\MimeSniffer\MimeSniffer::setFromPointer() throws checked exception InvalidArgumentException but it's missing from the PHPDoc @throws tag.
}

$this->setFromString(fread($pointer, 1024));

return $this;
Expand Down
15 changes: 15 additions & 0 deletions tests/MimeSnifferTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,21 @@ public function testConstructor(): void
{
$sniffer = new MimeSniffer();
$this->assertInstanceOf(MimeSniffer::class, $sniffer);

$sniffer = new MimeSniffer(
__DIR__ . '/../tests/files/test.jpg',
);
$this->assertInstanceOf(MimeSniffer::class, $sniffer);

$sniffer = new MimeSniffer(
fopen(__DIR__ . '/../tests/files/test.jpg', 'r')
);
$this->assertInstanceOf(MimeSniffer::class, $sniffer);

$sniffer = new MimeSniffer(
'foo'
);
$this->assertInstanceOf(MimeSniffer::class, $sniffer);
}

public function testCreate(): void
Expand Down

0 comments on commit 6b11023

Please sign in to comment.