PHP on T R A X
Rapid Application Development Made Easy

Ticket #111: number_helper_with_tests_by_mirek.diff

File number_helper_with_tests_by_mirek.diff, 13.3 kB (added by mirek, 9 months ago)

number_helper

  • test/NumberHelperTest.php

    old new  
     1<?php 
     2/** 
     3 *  File for the NumberHelperTest class, translated from Ruby on Rails. 
     4 * 
     5 * (PHP 5) 
     6 * 
     7 * @package PHPonTraxTest 
     8 * @license http://opensource.org/licenses/gpl-license.php GNU Public License 
     9 * @copyright (c) 2007 Mirek Rusin translated from Ruby on Rails 
     10 * @version $Id$ 
     11 * @author Mirek Rusin <info@mirekrusin.com> 
     12 */ 
     13 
     14echo "testing NumberHelper\n"; 
     15require_once 'testenv.php'; 
     16 
     17// Call NumberHelperTest::main() if this source file is executed directly. 
     18if (!defined("PHPUnit2_MAIN_METHOD")) { 
     19    define("PHPUnit2_MAIN_METHOD", "NumberHelperTest::main"); 
     20} 
     21 
     22require_once "PHPUnit2/Framework/TestCase.php"; 
     23require_once "PHPUnit2/Framework/TestSuite.php"; 
     24 
     25// You may remove the following line when all tests have been implemented. 
     26require_once "PHPUnit2/Framework/IncompleteTestError.php"; 
     27 
     28require_once "action_view/helpers.php"; 
     29require_once "action_view/helpers/number_helper.php"; 
     30 
     31/** 
     32 * Test class for NumberHelper. 
     33 * Generated by PHPUnit2_Util_Skeleton 
     34 */ 
     35class NumberHelperTest extends PHPUnit2_Framework_TestCase { 
     36    /** 
     37     * Runs the test methods of this class. 
     38     * 
     39     * @access public 
     40     * @static 
     41     */ 
     42    public static function main() { 
     43        require_once "PHPUnit2/TextUI/TestRunner.php"; 
     44 
     45        $suite  = new PHPUnit2_Framework_TestSuite("NumberHelperTest"); 
     46        $result = PHPUnit2_TextUI_TestRunner::run($suite); 
     47    } 
     48 
     49    /** 
     50     * Sets up the fixture, for example, open a network connection. 
     51     * This method is called before a test is executed. 
     52     * 
     53     * @access protected 
     54     */ 
     55    protected function setUp() { 
     56    } 
     57 
     58    /** 
     59     * Tears down the fixture, for example, close a network connection. 
     60     * This method is called after a test is executed. 
     61     * 
     62     * @access protected 
     63     */ 
     64    protected function tearDown() { 
     65    } 
     66 
     67    /** 
     68     *  Empty test to prevent failure 
     69     *  @todo Write test for the FormTagHelper class 
     70     */ 
     71    public function testNumber_to_phone_function() { 
     72        $this->assertEquals("800-555-1212", number_to_phone(8005551212)); 
     73        $this->assertEquals("(800) 555-1212", number_to_phone(8005551212, array("area_code" => true))); 
     74        $this->assertEquals("800 555 1212", number_to_phone(8005551212, array("delimiter" => " "))); 
     75        $this->assertEquals("(800) 555-1212 x 123", number_to_phone(8005551212, array("area_code" => true, "extension" => 123))); 
     76        $this->assertEquals("800-555-1212", number_to_phone(8005551212, array("extension" => " "))); 
     77        $this->assertEquals("800-555-1212", number_to_phone("8005551212")); 
     78        $this->assertEquals("+1-800-555-1212", number_to_phone(8005551212, array("country_code" => 1))); 
     79        $this->assertEquals("+18005551212", number_to_phone(8005551212, array("country_code" => 1, "delimiter" => ''))); 
     80        $this->assertEquals("22-555-1212", number_to_phone(225551212)); 
     81        $this->assertEquals("+45-22-555-1212", number_to_phone(225551212, array("country_code" => 45))); 
     82        $this->assertEquals("x", number_to_phone("x")); 
     83        $this->assertNull(number_to_phone(null)); 
     84    } 
     85} 
     86 
     87// Call NumberHelperTest::main() if this source file is executed directly. 
     88if (PHPUnit2_MAIN_METHOD == "NumberHelperTest::main") { 
     89    NumberHelperTest::main(); 
     90} 
     91 
     92// -- set Emacs parameters -- 
     93// Local variables: 
     94// tab-width: 4 
     95// c-basic-offset: 4 
     96// c-hanging-comment-ender-p: nil 
     97// indent-tabs-mode: nil 
     98// End: 
     99?> 
  • vendor/trax/action_view/helpers/number_helper.php

    old new  
     1<?php 
     2/** 
     3 *  File containing the NumberHelper class, translated from Ruby on Rails. 
     4 * 
     5 *  (PHP 5) 
     6 * 
     7 *  @package PHPonTrax 
     8 *  @version $Id: number_helper.php $ 
     9 *  @copyright (c) 2007 Mirek Rusin translated from Ruby on Rails 
     10 * 
     11 *  Permission is hereby granted, free of charge, to any person obtaining 
     12 *  a copy of this software and associated documentation files (the 
     13 *  "Software"), to deal in the Software without restriction, including 
     14 *  without limitation the rights to use, copy, modify, merge, publish, 
     15 *  distribute, sublicense, and/or sell copies of the Software, and to 
     16 *  permit persons to whom the Software is furnished to do so, subject to 
     17 *  the following conditions: 
     18 * 
     19 *  The above copyright notice and this permission notice shall be 
     20 *  included in all copies or substantial portions of the Software. 
     21 * 
     22 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 
     23 *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 
     24 *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
     25 *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 
     26 *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 
     27 *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 
     28 *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 
     29 */ 
     30 
     31# Provides methods for converting a numbers into formatted strings. 
     32# Methods are provided for phone numbers, currency, percentage, 
     33# precision, positional notation, and file size. 
     34class NumberHelper extends Helpers { 
     35 
     36    # Formats a +number+ into a US phone number. You can customize the format 
     37    # in the +options+ hash. 
     38    # * <tt>area_code</tt>  - Adds parentheses around the area code. 
     39    # * <tt>delimiter</tt>  - Specifies the delimiter to use, defaults to "-". 
     40    # * <tt>extension</tt>  - Specifies an extension to add to the end of the generated number 
     41    # * <tt>country_code</tt>  - Sets the country code for the phone number. 
     42    # 
     43    #  number_to_phone(1235551234) => 123-555-1234 
     44    #  number_to_phone(1235551234, array('area_code' => true))   => (123) 555-1234 
     45    #  number_to_phone(1235551234, array('delimiter' => " "))    => 123 555 1234 
     46    #  number_to_phone(1235551234, array('area_code' => true, 'extension' => 555))  => (123) 555-1234 x 555 
     47    #  number_to_phone(1235551234, array('country_code' => 1)) 
     48    public static function number_to_phone($number, $options = array()) { 
     49        if (strlen($number) > 0) { 
     50            $number = trim("$number"); 
     51            $area_code = array_key_exists('area_code', $options) ? $options['area_code'] : null; 
     52            $delimiter = array_key_exists('delimiter', $options) ? $options['delimiter'] : '-'; 
     53            $extension = array_key_exists('extension', $options) ? trim("$options[extension]") : null; 
     54            $country_code = array_key_exists('country_code', $options) ? trim("$options[country_code]") : null; 
     55            $str = ""; 
     56            if (strlen($country_code) > 0) $str .= "+$country_code$delimiter"; 
     57            if (strlen($area_code) > 0) 
     58                $str .= preg_replace('/([0-9]{1,3})([0-9]{3})([0-9]{4}$)/', "(\\1) \\2$delimiter\\3", $number); 
     59            else 
     60                $str .= preg_replace('/([0-9]{1,3})([0-9]{3})([0-9]{4})$/', "\\1$delimiter\\2$delimiter\\3", $number); 
     61            if (strlen($extension)) $str .= " x $extension"; 
     62            return $str; 
     63        } else { 
     64            return null; 
     65        } 
     66    } 
     67 
     68    # Formats a +number+ into a currency string. You can customize the format 
     69    # in the +options+ hash. 
     70    # * <tt>precision</tt>  -  Sets the level of precision, defaults to 2 
     71    # * <tt>unit</tt>  - Sets the denomination of the currency, defaults to "$" 
     72    # * <tt>separator</tt>  - Sets the separator between the units, defaults to "." 
     73    # * <tt>delimiter</tt>  - Sets the thousands delimiter, defaults to "," 
     74    # 
     75    #  number_to_currency(1234567890.50)     => $1,234,567,890.50 
     76    #  number_to_currency(1234567890.506)    => $1,234,567,890.51 
     77    #  number_to_currency(1234567890.506, array('precision' => 3))    => $1,234,567,890.506 
     78    #  number_to_currency(1234567890.50, array('unit' => "&pound;", 'separator' => ",", 'delimiter' => "")) 
     79    #     => &pound;1234567890,50 
     80    public static function number_to_currency($number, $options = array()) { 
     81        $precision = array_key_exists('precision', $options) ? $options['precision'] : 2; 
     82        $unit = array_key_exists('unit', $options) ? $options['unit'] : '$'; 
     83        $separator = $precision > 0 ? ($options['separator'] !== null ? $options['separator'] : '.') : ""; 
     84        $delimiter = array_key_exists('delimiter', $options) ? $options['delimiter'] : ','; 
     85        $parts = split(".", self::number_with_precision($number, $precision)); 
     86        return $unit . self::number_with_delimiter($parts[0], $delimiter) . $separator . $parts[1]; 
     87    } 
     88 
     89    # Formats a +number+ as a percentage string. You can customize the 
     90    # format in the +options+ hash. 
     91    # * <tt>precision</tt>  - Sets the level of precision, defaults to 3 
     92    # * <tt>separator</tt>  - Sets the separator between the units, defaults to "." 
     93    # 
     94    #  number_to_percentage(100)    => 100.000% 
     95    #  number_to_percentage(100, array('precision' => 0))   => 100% 
     96    #  number_to_percentage(302.0574, array('precision' => 2))   => 302.06% 
     97    public static function number_to_percentage($number, $options = array()) { 
     98        $precision = array_key_exists("precision", $options) ? $options["precision"] : 3; 
     99        $separator = array_key_exists("separator", $options) ? $options["separator"] : "."; 
     100        $number = self::number_with_precision($number, $precision); 
     101        $parts = split(".", $number); 
     102        if ($parts[1] === null) { 
     103            return $parts[0] . "%"; 
     104        } else { 
     105            return $parts[0] . $separator . $parts[1] . "%"; 
     106        } 
     107    } 
     108 
     109 
     110    # Formats a +number+ with grouped thousands using +delimiter+. You 
     111    # can customize the format using optional <em>delimiter</em> and <em>separator</em> parameters. 
     112    # * <tt>delimiter</tt>  - Sets the thousands delimiter, defaults to "," 
     113    # * <tt>separator</tt>  - Sets the separator between the units, defaults to "." 
     114    # 
     115    #  number_with_delimiter(12345678)      => 12,345,678 
     116    #  number_with_delimiter(12345678.05)   => 12,345,678.05 
     117    #  number_with_delimiter(12345678, ".")   => 12.345.678 
     118    public static function number_with_delimiter($number, $delimiter = ",", $separator = ".") { 
     119        $parts = split(".", $number); 
     120        $parts[0] = preg_replace('/(\d)(?=(\d\d\d)+(?!\d))/', "\\1$delimiter", $parts[0]); 
     121        return join($separator, $parts); 
     122    } 
     123 
     124    # Formats a +number+ with the specified level of +precision+. The default 
     125    # level of precision is 3. 
     126    # 
     127    #  number_with_precision(111.2345)    => 111.235 
     128    #  number_with_precision(111.2345, 2) => 111.24 
     129    public static function number_with_precision($number, $precision = 3) { 
     130        return sprintf("%01.${precision}f", $number); 
     131    } 
     132     
     133    # Formats the bytes in +size+ into a more understandable representation. 
     134    # Useful for reporting file sizes to users. This method returns nil if  
     135    # +size+ cannot be converted into a number. You can change the default  
     136    # precision of 1 in +precision+. 
     137    #  
     138    #  number_to_human_size(123)           => 123 Bytes 
     139    #  number_to_human_size(1234)          => 1.2 KB 
     140    #  number_to_human_size(12345)         => 12.1 KB 
     141    #  number_to_human_size(1234567)       => 1.2 MB 
     142    #  number_to_human_size(1234567890)    => 1.1 GB 
     143    #  number_to_human_size(1234567890123) => 1.1 TB 
     144    #  number_to_human_size(1234567, 2)    => 1.18 MB 
     145    public static function number_to_human_size($size, $precision = 1) { 
     146        $size = float($size); 
     147        $return = null; 
     148        if ($size == 1) $return = "1 Byte"; 
     149        elseif ($size < 1024) $return = sprintf("%d Bytes", $size); 
     150        elseif ($size < 1024 * 1024) $return = sprintf("%.${precision}f KB", $size / (1024 * 1024)); 
     151        elseif ($size < 1024 * 1024 * 1024) $return = sprinf("%.${precision}f MB", $size / (1024 * 1024 * 1024)); 
     152        elseif ($size < 1024 * 1024 * 1024 * 1024) $return = sprinf("%.${precision}f GB", $size / (1024 * 1024 * 1024 * 1024)); 
     153        else $return = sprintf("%.${precision}f TB", $size / (1024 * 1024 * 1024 * 1024 * 1024)); 
     154        return str_replace(".0", "", $return); 
     155    } 
     156} 
     157 
     158function number_to_phone($number, $options=array()) { 
     159  return NumberHelper::number_to_phone($number, $options); 
     160} 
     161 
     162function number_to_currency($number, $options = array()) { 
     163  return NumberHelper::number_to_currency($number, $options); 
     164} 
     165 
     166function number_to_percentage($number, $options = array()) { 
     167  return NumberHelper::number_to_percentage($number, $options); 
     168} 
     169 
     170function number_with_delimiter($number, $delimiter = ",", $separator = ".") { 
     171  return NumberHelper::number_with_delimiter($number, $delimiter, $separator); 
     172} 
     173 
     174function number_with_precision($number, $precision = 3) { 
     175  return NumberHelper::number_with_precision($number, $precision); 
     176} 
     177 
     178function number_to_human_size($size, $precision = 1) { 
     179  return NumberHelper::number_to_human_size($size, $precision = 1); 
     180} 
     181 
     182?>