Page MenuHomeDevCentral

D1644.id.diff
No OneTemporary

D1644.id.diff

diff --git a/src/IO/File.php b/src/IO/File.php
--- a/src/IO/File.php
+++ b/src/IO/File.php
@@ -1,4 +1,5 @@
<?php
+declare(strict_types=1);
namespace Keruald\OmniTools\IO;
@@ -7,29 +8,30 @@
/**
* @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;
}
@@ -39,11 +41,31 @@
///
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);
}
///
@@ -55,7 +77,7 @@
return false;
}
- include($this->filename);
+ include($this->path);
return true;
}
diff --git a/src/IO/FileUtilities.php b/src/IO/FileUtilities.php
new file mode 100644
--- /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
--- /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
--- /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

Mime Type
text/plain
Expires
Sun, Nov 24, 14:10 (7 h, 17 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2260323
Default Alt Text
D1644.id.diff (5 KB)

Event Timeline