This repository has been archived by the owner on Jul 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
FileInfo.php
91 lines (71 loc) · 1.79 KB
/
FileInfo.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<?php declare(strict_types = 1);
namespace Wavevision\Utils;
use finfo;
use Nette\IOException;
use Nette\SmartObject;
use Nette\Utils\DateTime;
use function basename;
use function dirname;
use function filemtime;
use function filesize;
use function is_file;
use function pathinfo;
use function sprintf;
use function str_replace;
use const FILEINFO_MIME_TYPE;
use const PATHINFO_EXTENSION;
class FileInfo
{
use SmartObject;
private string $pathName;
private string $baseName;
private string $dirName;
private string $extension;
private string $contentType;
private int $size;
private DateTime $mtime;
public function __construct(string $pathName)
{
if (!is_file($pathName)) {
throw new IOException(sprintf("File '%s' not found.", $pathName));
}
$this->pathName = $pathName;
$this->baseName = basename($pathName);
$this->dirName = dirname($pathName);
$this->extension = pathinfo($pathName, PATHINFO_EXTENSION);
$this->contentType = (string)(new finfo(FILEINFO_MIME_TYPE))->file($pathName);
$this->size = (int)filesize($pathName);
$this->mtime = (new DateTime())->setTimestamp((int)filemtime($pathName));
}
public function getPathName(): string
{
return $this->pathName;
}
public function getBaseName(bool $withoutExtension = false): string
{
if ($withoutExtension) {
return str_replace($this->getExtension(true), '', $this->baseName);
}
return $this->baseName;
}
public function getDirName(): string
{
return $this->dirName;
}
public function getExtension(bool $withDot = false): string
{
return $withDot ? '.' . $this->extension : $this->extension;
}
public function getContentType(): string
{
return $this->contentType;
}
public function getSize(): int
{
return $this->size;
}
public function getMtime(): DateTime
{
return $this->mtime;
}
}