Page Menu
Home
DevCentral
Search
Configure Global Search
Log In
Files
F24929064
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
5 KB
Referenced Files
None
Subscribers
None
View Options
diff --git a/src/IO/File.php b/src/IO/File.php
index 13bb148..7303ace 100644
--- a/src/IO/File.php
+++ b/src/IO/File.php
@@ -1,67 +1,89 @@
<?php
+declare(strict_types=1);
namespace Keruald\OmniTools\IO;
class File {
/**
* @var string
*/
- private $filename;
+ private $path;
///
/// Constructors
///
- public static function from (string $filename) : self {
- $instance = new self;
- $instance->filename = $filename;
+ public function __construct (string $path = null) {
+ $this->path = $path;
+ }
- return $instance;
+ public static function from (string $path) : self {
+ return new self($path);
}
///
/// Getters and setters
///
- public function getFilename () : string {
- return $this->filename;
+ public function getPath () : string {
+ return $this->path;
}
- public function setFilename (string $filename) : self {
- $this->filename = $filename;
+ public function setPath (string $path) : self {
+ $this->path = $path;
return $this;
}
///
/// File properties methods
///
public function exists () : bool {
- return file_exists($this->filename);
+ return file_exists($this->path);
}
public function isReadable () : bool {
- return is_readable($this->filename);
+ return is_readable($this->path);
+ }
+
+ public function getPathInfo () : array {
+ return pathinfo($this->path);
+ }
+
+ public function getDirectory () : string {
+ return pathinfo($this->path, PATHINFO_DIRNAME);
+ }
+
+ public function getFileName () : string {
+ return pathinfo($this->path, PATHINFO_BASENAME);
+ }
+
+ public function getFileNameWithoutExtension () : string {
+ return pathinfo($this->path, PATHINFO_FILENAME);
+ }
+
+ public function getExtension () : string {
+ return pathinfo($this->path, PATHINFO_EXTENSION);
}
///
/// Include methods
///
public function tryInclude () : bool {
if (!$this->canBeIncluded()) {
return false;
}
- include($this->filename);
+ include($this->path);
return true;
}
public function canBeIncluded () : bool {
return $this->exists() &&$this->isReadable();
}
}
diff --git a/src/IO/FileUtilities.php b/src/IO/FileUtilities.php
new file mode 100644
index 0000000..b30a4e4
--- /dev/null
+++ b/src/IO/FileUtilities.php
@@ -0,0 +1,12 @@
+<?php
+declare(strict_types=1);
+
+namespace Keruald\OmniTools\IO;
+
+class FileUtilities {
+
+ public static function getExtension (string $filename) : string {
+ return File::from($filename)->getExtension();
+ }
+
+}
diff --git a/tests/IO/FileTest.php b/tests/IO/FileTest.php
new file mode 100644
index 0000000..2a7be34
--- /dev/null
+++ b/tests/IO/FileTest.php
@@ -0,0 +1,81 @@
+<?php
+
+namespace Keruald\OmniTools\Tests\IO;
+
+use Keruald\OmniTools\IO\File;
+use PHPUnit\Framework\TestCase;
+
+class FileTest extends TestCase {
+
+ ///
+ /// Tests
+ ///
+
+ /**
+ * @dataProvider provideFilesAndDirectories
+ */
+ public function testGetDirectory (string $filename, string $expected) : void {
+ $this->assertSame($expected, File::from($filename)->getDirectory());
+ }
+
+ /**
+ * @dataProvider provideFilesAndFileNames
+ */
+ public function testGetFileName (string $filename, string $expected) : void {
+ $this->assertSame($expected, File::from($filename)->getFileName());
+ }
+
+ /**
+ * @dataProvider provideFilesAndFileNamesWithoutExtension
+ */
+ public function testGetFileNameWithoutExtension (string $filename, string $expected) : void {
+ $this->assertSame($expected, File::from($filename)->getFileNameWithoutExtension());
+ }
+
+ /**
+ * @dataProvider provideFilesAndExtensions
+ */
+ public function testGetExtension (string $filename, string $expected) : void {
+ $this->assertSame($expected, File::from($filename)->getExtension());
+ }
+
+ ///
+ /// Data providers
+ ///
+
+ public function provideFilesAndDirectories () : iterable {
+ yield ['', ''];
+ yield ['/', '/'];
+ yield ['/foo', '/'];
+ yield ['foo/bar', 'foo'];
+ yield ['foo/', '.'];
+ yield ['/full/path/to/foo.php', '/full/path/to'];
+ }
+
+ public function provideFilesAndFileNames () : iterable {
+ yield ['', ''];
+ yield ['foo', 'foo'];
+ yield ['foo', 'foo'];
+ yield ['foo.php', 'foo.php'];
+ yield ['/full/path/to/foo.php', 'foo.php'];
+ }
+
+ public function provideFilesAndFileNamesWithoutExtension () : iterable {
+ yield ['', ''];
+ yield ['foo', 'foo'];
+ yield ['foo.php', 'foo'];
+ yield ['/full/path/to/foo.php', 'foo'];
+ yield ['foo.tar.gz', 'foo.tar'];
+
+ }
+
+ public function provideFilesAndExtensions () : iterable {
+ yield ['', ''];
+ yield ['foo', ''];
+ yield ['foo.php', 'php'];
+ yield ['/full/path/to/foo.php', 'php'];
+ yield ['foo.tar.gz', 'gz'];
+
+ }
+
+}
diff --git a/tests/IO/FileUtilitiesTest.php b/tests/IO/FileUtilitiesTest.php
new file mode 100644
index 0000000..f6c012a
--- /dev/null
+++ b/tests/IO/FileUtilitiesTest.php
@@ -0,0 +1,14 @@
+<?php
+
+namespace Keruald\OmniTools\Tests\IO;
+
+use Keruald\OmniTools\IO\FileUtilities;
+use PHPUnit\Framework\TestCase;
+
+class FileUtilitiesTest extends TestCase {
+
+ public function testGetExtension () : void {
+ $this->assertSame("jpg", FileUtilities::getExtension("image.jpg"));
+ }
+
+}
File Metadata
Details
Attached
Mime Type
text/x-diff
Expires
Sat, Mar 21, 07:17 (1 d, 16 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
3546629
Default Alt Text
(5 KB)
Attached To
Mode
rKOT Keruald OmniTools
Attached
Detach File
Event Timeline
Log In to Comment