[FEATURE] stdWrap for string-padding (like PHP-function str_pad)
authorStefan Neufeind <typo3.neufeind@speedpartner.de>
Mon, 20 Aug 2012 14:40:45 +0000 (16:40 +0200)
committerDmitry Dulepov <dmitry@typo3.org>
Tue, 4 Dec 2012 11:11:20 +0000 (12:11 +0100)
Name is .strPad to follow usual TypoScript-syntax.
Parameters:
* left (number)
* padWith (string; default: space)
* type (left, right, both; default: right)

Change-Id: Iedb29a774b6a696b9f3de09049ba9da5a75cd9b8
Resolves: #40001
Releases: 6.1
Reviewed-on: http://review.typo3.org/13969
Reviewed-by: Wouter Wolters
Tested-by: Wouter Wolters
Reviewed-by: Philipp Gampe
Tested-by: Philipp Gampe
Reviewed-by: Dmitry Dulepov
Tested-by: Dmitry Dulepov
typo3/sysext/frontend/Classes/ContentObject/ContentObjectRenderer.php
typo3/sysext/frontend/Tests/Unit/ContentObject/ContentObjectRendererTest.php

index 17b5ea3..772c99d 100644 (file)
@@ -92,6 +92,7 @@ class ContentObjectRenderer {
                'listNum.' => 'array',
                'trim' => 'boolean',
                'trim.' => 'array',
+               'strPad.' => 'array',
                'stdWrap' => 'stdWrap',
                'stdWrap.' => 'array',
                'stdWrapProcess' => 'hook',
@@ -2314,6 +2315,38 @@ class ContentObjectRenderer {
        }
 
        /**
+        * strPad
+        * Will return a string padded left/right/on both sides, based on configuration given as stdWrap properties
+        *
+        * @param string $content Input value undergoing processing in this function.
+        * @param array $conf stdWrap properties for strPad.
+        * @return string The processed input value
+        */
+       public function stdWrap_strPad($content = '', $conf = array()) {
+               // Must specify a length in conf for this to make sense
+               $length = 0;
+               // Padding with space is PHP-default
+               $padWith = ' ';
+               // Padding on the right side is PHP-default
+               $padType = STR_PAD_RIGHT;
+               if (!empty($conf['strPad.']['length'])) {
+                       $length = intval($conf['strPad.']['length']);
+               }
+               if (!empty($conf['strPad.']['padWith'])) {
+                       $padWith = $conf['strPad.']['padWith'];
+               }
+               if (!empty($conf['strPad.']['type'])) {
+                       if (strtolower($conf['strPad.']['type']) === 'left') {
+                               $padType = STR_PAD_LEFT;
+                       } elseif (strtolower($conf['strPad.']['type']) === 'both') {
+                               $padType = STR_PAD_BOTH;
+                       }
+               }
+               $content = str_pad($content, $length, $padWith, $padType);
+               return $content;
+       }
+
+       /**
         * stdWrap
         * A recursive call of the stdWrap function set
         * This enables the user to execute stdWrap functions in another than the predefined order
index 75bd60f..7e31ff5 100644 (file)
@@ -802,6 +802,66 @@ class ContentObjectRendererTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
        }
 
        /**
+        * @return array
+        */
+       public function stdWrap_strPadDataProvider() {
+               return array(
+                       'pad string with default settings and length 10' => array(
+                               'Alien',
+                               array(
+                                       'length' => '10',
+                               ),
+                               'Alien     ',
+                       ),
+                       'pad string with padWith -= and type left and length 10' => array(
+                               'Alien',
+                               array(
+                                       'length' => '10',
+                                       'padWith' => '-=',
+                                       'type' => 'left',
+                               ),
+                               '-=-=-Alien',
+                       ),
+                       'pad string with padWith _ and type both and length 10' => array(
+                               'Alien',
+                               array(
+                                       'length' => '10',
+                                       'padWith' => '_',
+                                       'type' => 'both',
+                               ),
+                               '__Alien___',
+                       ),
+                       'pad string with padWith _ and type both and length 6' => array(
+                               'Alien',
+                               array(
+                                       'length' => '6',
+                                       'padWith' => '___',
+                                       'type' => 'both',
+                               ),
+                               'Alien_',
+                       ),
+               );
+       }
+
+       /**
+        * Test for the stdWrap function "strPad"
+        *
+        * @param string $content
+        * @param array $conf
+        * @param string $expected
+        *
+        * @dataProvider stdWrap_strPadDataProvider
+        * @test
+        */
+       public function stdWrap_strPad($content, $conf, $expected) {
+               $conf = array(
+                       'strPad.' => $conf
+               );
+               $result = $this->cObj->stdWrap_strPad($content, $conf);
+               $this->assertEquals($expected, $result);
+       }
+
+       /**
         * Data provider for the hash test
         *
         * @return array multi-dimensional array with the second level like this:
@@ -1165,4 +1225,4 @@ class ContentObjectRendererTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
        }
 }
 
-?>
\ No newline at end of file
+?>