| | 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. |
|---|
| | 34 | class 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' => "£", 'separator' => ",", 'delimiter' => "")) |
|---|
| | 79 | # => £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 | |
|---|
| | 158 | function number_to_phone($number, $options=array()) { |
|---|
| | 159 | return NumberHelper::number_to_phone($number, $options); |
|---|
| | 160 | } |
|---|
| | 161 | |
|---|
| | 162 | function number_to_currency($number, $options = array()) { |
|---|
| | 163 | return NumberHelper::number_to_currency($number, $options); |
|---|
| | 164 | } |
|---|
| | 165 | |
|---|
| | 166 | function number_to_percentage($number, $options = array()) { |
|---|
| | 167 | return NumberHelper::number_to_percentage($number, $options); |
|---|
| | 168 | } |
|---|
| | 169 | |
|---|
| | 170 | function number_with_delimiter($number, $delimiter = ",", $separator = ".") { |
|---|
| | 171 | return NumberHelper::number_with_delimiter($number, $delimiter, $separator); |
|---|
| | 172 | } |
|---|
| | 173 | |
|---|
| | 174 | function number_with_precision($number, $precision = 3) { |
|---|
| | 175 | return NumberHelper::number_with_precision($number, $precision); |
|---|
| | 176 | } |
|---|
| | 177 | |
|---|
| | 178 | function number_to_human_size($size, $precision = 1) { |
|---|
| | 179 | return NumberHelper::number_to_human_size($size, $precision = 1); |
|---|
| | 180 | } |
|---|
| | 181 | |
|---|
| | 182 | ?> |