[TASK] File: add getNameWithoutExtension() method
authorAndreas Wolf <andreas.wolf@typo3.org>
Tue, 9 Oct 2012 08:16:04 +0000 (10:16 +0200)
committerSteffen Ritter <info@rs-websystems.de>
Wed, 10 Oct 2012 14:05:37 +0000 (16:05 +0200)
There is currently no way to get the filename without its extension,
e.g. to create a new name for a file converted to another format. This
commits adds such a method, together with tests and a fix for names like
somefile.tar.gz.

Resolves: #41768
Releases: 6.0
Change-Id: Iad924d1be4100cf798e36f25a22a32b221b8aef7
Reviewed-on: http://review.typo3.org/15427
Reviewed-by: Steffen Ritter
Tested-by: Steffen Ritter
typo3/sysext/core/Classes/Resource/AbstractFile.php
typo3/sysext/core/Classes/Resource/FileInterface.php
typo3/sysext/core/Tests/Unit/Resource/FileTest.php

index 3cd199d..f57bb97 100644 (file)
@@ -153,6 +153,15 @@ abstract class AbstractFile implements \TYPO3\CMS\Core\Resource\FileInterface {
        }
 
        /**
+        * Returns the basename (the name without extension) of this file.
+        *
+        * @return string
+        */
+       public function getNameWithoutExtension() {
+               return pathinfo($this->getName(), PATHINFO_FILENAME);
+       }
+
+       /**
         * Returns the size of this file
         *
         * @return integer
@@ -215,7 +224,11 @@ abstract class AbstractFile implements \TYPO3\CMS\Core\Resource\FileInterface {
         * @return string The file extension
         */
        public function getExtension() {
-               return strtolower(pathinfo($this->getName(), PATHINFO_EXTENSION));
+               $pathinfo = pathinfo($this->getName());
+
+               $extension = strtolower($pathinfo['extension']);
+
+               return $extension;
        }
 
        /**
index 90cf5e4..29266d0 100644 (file)
@@ -68,6 +68,13 @@ interface FileInterface extends \TYPO3\CMS\Core\Resource\ResourceInterface {
        public function getSha1();
 
        /**
+        * Returns the basename (the name without extension) of this file.
+        *
+        * @return string
+        *
+       public function getNameWithoutExtension();
+
+       /**
         * Get the file extension
         *
         * @return string The file extension
index b348c65..987916a 100644 (file)
@@ -360,6 +360,41 @@ class FileTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
                $fixture->write('asdf');
        }
 
+       public function filenameExtensionDataProvider() {
+               return array(
+                       array('somefile.jpg', 'somefile', 'jpg'),
+                       array('SomeFile.PNG', 'SomeFile', 'png'),
+                       array('somefile', 'somefile', ''),
+                       array('somefile.tar.gz', 'somefile', 'gz'),
+                       array('somefile.tar.bz2', 'somefile', 'bz2'),
+               );
+       }
+
+       /**
+        * @test
+        * @dataProvider filenameExtensionDataProvider
+        */
+       public function getNameWithoutExtensionReturnsCorrectName($originalFilename, $expectedBasename) {
+               $fixture = new \TYPO3\CMS\Core\Resource\File(array(
+                       'name' => $originalFilename,
+                       'identifier' => '/' . $originalFilename
+               ));
+               $this->assertSame($expectedBasename, $fixture->getNameWithoutExtension());
+       }
+
+       /**
+        * @test
+        * @dataProvider filenameExtensionDataProvider
+        */
+       public function getExtensionReturnsCorrectExtension($originalFilename, $expectedBasename, $expectedExtension) {
+               $fixture = new \TYPO3\CMS\Core\Resource\File(array(
+                       'name' => $originalFilename,
+                       'identifier' => '/' . $originalFilename
+               ));
+               $this->assertSame($expectedExtension, $fixture->getExtension());
+       }
+
+
 }
 
 ?>
\ No newline at end of file