Skip to content
This repository was archived by the owner on Jul 28, 2024. It is now read-only.

Commit c8a8e59

Browse files
committed
Add FileInfo
1 parent 1c7dd83 commit c8a8e59

File tree

3 files changed

+103
-0
lines changed

3 files changed

+103
-0
lines changed

src/Utils/FileInfo.php

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Wavevision\Utils;
4+
5+
use finfo;
6+
use Nette\IOException;
7+
use Nette\SmartObject;
8+
use Nette\Utils\DateTime;
9+
10+
class FileInfo
11+
{
12+
13+
use SmartObject;
14+
15+
private string $pathName;
16+
17+
private string $baseName;
18+
19+
private string $extension;
20+
21+
private string $contentType;
22+
23+
private int $size;
24+
25+
private DateTime $mtime;
26+
27+
public function __construct(string $pathName)
28+
{
29+
if (!is_file($pathName)) {
30+
throw new IOException(sprintf("File '%s' not found.", $pathName));
31+
}
32+
$this->pathName = $pathName;
33+
$this->baseName = basename($pathName);
34+
$this->extension = pathinfo($pathName, PATHINFO_EXTENSION);
35+
$this->contentType = (string)(new finfo(FILEINFO_MIME_TYPE))->file($pathName);
36+
$this->size = (int)filesize($pathName);
37+
$this->mtime = (new DateTime())->setTimestamp((int)filemtime($pathName));
38+
}
39+
40+
public function getPathName(): string
41+
{
42+
return $this->pathName;
43+
}
44+
45+
public function getBaseName(): string
46+
{
47+
return $this->baseName;
48+
}
49+
50+
public function getExtension(): string
51+
{
52+
return $this->extension;
53+
}
54+
55+
public function getContentType(): string
56+
{
57+
return $this->contentType;
58+
}
59+
60+
public function getSize(): int
61+
{
62+
return $this->size;
63+
}
64+
65+
public function getMtime(): DateTime
66+
{
67+
return $this->mtime;
68+
}
69+
70+
}

tests/UtilsTests/FileInfoTest.php

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Wavevision\UtilsTests;
4+
5+
use Nette\IOException;
6+
use Nette\Utils\DateTime;
7+
use PHPUnit\Framework\TestCase;
8+
use Wavevision\Utils\FileInfo;
9+
10+
class FileInfoTest extends TestCase
11+
{
12+
13+
public function testFileInfo(): void
14+
{
15+
$file = __DIR__ . '/file.txt';
16+
$fileInfo = new FileInfo($file);
17+
$this->assertEquals($file, $fileInfo->getPathName());
18+
$this->assertEquals('file.txt', $fileInfo->getBaseName());
19+
$this->assertEquals('txt', $fileInfo->getExtension());
20+
$this->assertEquals(5, $fileInfo->getSize());
21+
$this->assertEquals('text/plain', $fileInfo->getContentType());
22+
$this->assertInstanceOf(DateTime::class, $fileInfo->getMtime());
23+
}
24+
25+
public function testNotFound(): void
26+
{
27+
$this->expectException(IOException::class);
28+
$this->expectExceptionMessage("File '42' not found.");
29+
new FileInfo('42');
30+
}
31+
32+
}

tests/UtilsTests/file.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
hello

0 commit comments

Comments
 (0)