[FEATURE] Rounding functionality in stdWrap
authorKilian Hann <kilian.hann@gmail.com>
Sat, 9 Jul 2011 10:01:10 +0000 (12:01 +0200)
committerAndreas Wolf <andreas.wolf@ikt-werk.de>
Mon, 11 Jul 2011 17:14:31 +0000 (19:14 +0200)
Provides rounding functionality (round, ceil, floor) to stdWrap.

Change-Id: Iec4dd4df61e3e0dcb0556fa4ddeb34adaffa9823
Resolves: #28027
Reviewed-on: http://review.typo3.org/3178
Reviewed-by: Andreas Wolf
Tested-by: Andreas Wolf
tests/typo3/sysext/cms/tslib/class.tslib_contentTest.php
typo3/sysext/cms/tslib/class.tslib_content.php

index a3ce27d..9715569 100644 (file)
@@ -654,6 +654,73 @@ class tslib_contentTest extends tx_phpunit_testcase {
                        $result
                );
        }
                        $result
                );
        }
+
+       /**
+        * @return array
+        */
+       public function stdWrap_roundDataProvider() {
+               return array(
+                       'rounding off without any configuration' => array(
+                               1.123456789,
+                               array(
+                               ),
+                               1
+                       ),
+                       'rounding up without any configuration' => array(
+                               1.523456789,
+                               array(
+                               ),
+                               2
+                       ),
+                       'regular rounding (off) to two decimals' => array(
+                               0.123456789,
+                               array(
+                                       'decimals' => 2
+                               ),
+                               0.12
+                       ),
+                       'regular rounding (up) to two decimals' => array(
+                               0.1256789,
+                               array(
+                                       'decimals' => 2
+                               ),
+                               0.13
+                       ),
+                       'rounding up to integer with type ceil' => array(
+                               0.123456789,
+                               array(
+                                       'roundType' => 'ceil'
+                               ),
+                               1
+                       ),
+                       'rounding down to integer with type floor' => array(
+                               2.3481,
+                               array(
+                                       'roundType' => 'floor'
+                               ),
+                               2
+                       )
+               );
+       }
+
+       /**
+        * Test for the stdWrap function "round"
+        *
+        * @param float $float
+        * @param array $conf
+        * @param float $expected
+        * @return void
+        *
+        * @dataProvider stdWrap_roundDataProvider
+        * @test
+        */
+       public function stdWrap_round($float, $conf, $expected) {
+               $conf = array(
+                       'round.' => $conf
+               );
+               $result = $this->cObj->stdWrap_round($float, $conf);
+               $this->assertEquals($expected, $result);
+       }
        
        /**
         * Data provider for the numberFormat test
        
        /**
         * Data provider for the numberFormat test
index 5bcb040..0aeefb2 100644 (file)
@@ -119,6 +119,8 @@ class tslib_cObj {
                'char.' => 'array',
                'intval' => 'boolean',
                'intval.' => 'array',
                'char.' => 'array',
                'intval' => 'boolean',
                'intval.' => 'array',
+               'round' => 'boolean',
+               'round.' => 'array',
                'numberFormat.' => 'array',
                'date' => 'dateconf',
                'date.' => 'array',
                'numberFormat.' => 'array',
                'date' => 'dateconf',
                'date.' => 'array',
@@ -2323,6 +2325,19 @@ class tslib_cObj {
                return $content;
        }
 
                return $content;
        }
 
+       /**
+        * stdWrap_round will return a rounded number with ceil(), floor() or round(), defaults to round()
+        * Only the english number format is supported . (dot) as decimal point
+        *
+        * @param       string          Input value undergoing processing in this function.
+        * @param       array           stdWrap properties for round.
+        * @return      string          The processed input value
+        */
+       public function stdWrap_round($content = '', $conf = array()){
+               $content = $this->round($content, $conf['round.']);
+               return $content;
+       }
+
        /**
         * numberFormat
         * Will return a formatted number based on configuration given as stdWrap properties
        /**
         * numberFormat
         * Will return a formatted number based on configuration given as stdWrap properties
@@ -4120,6 +4135,38 @@ class tslib_cObj {
                return $content;
        }
 
                return $content;
        }
 
+       /**
+        * Implements the stdWrap property "round"
+        * This is a Wrapper function for php's rounding functions (round,ceil,floor), defaults to round()
+        *
+        * @param       string  Value to process
+        * @param       array   TypoScript Configuration for round
+        * @return      string  The formated number
+        */
+       private function round($content, $conf = array()) {
+               $decimals = isset($conf['decimals.'])
+                               ? $this->stdWrap($conf['decimals'], $conf['decimals.'])
+                               : $conf['decimals'];
+               $type = isset($conf['roundType.'])
+                               ? $this->stdWrap($conf['roundType'], $conf['roundType.'])
+                               : $conf['roundType'];
+
+               $floatVal = floatval($content);
+               switch ($type) {
+                       case 'ceil':
+                               $content = ceil($floatVal);
+                               break;
+                       case 'floor':
+                               $content = floor($floatVal);
+                               break;
+                       case 'round':
+                       default:
+                               $content = round($floatVal, intval($decimals));
+                               break;
+               }
+               return $content;
+       }
+
        /**
         * Implements the stdWrap property "numberFormat"
         * This is a Wrapper function for php's number_format()
        /**
         * Implements the stdWrap property "numberFormat"
         * This is a Wrapper function for php's number_format()