Fixing content type ordering when content_type is not defined.
[akelos.git] / lib / AkActionView / helpers / number_helper.php
bloba267d4e6c3c946f8b960368712a19bd927777643
1 <?php
2 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
4 // +----------------------------------------------------------------------+
5 // | Akelos Framework - http://www.akelos.org |
6 // +----------------------------------------------------------------------+
7 // | Copyright (c) 2002-2006, Akelos Media, S.L. & Bermi Ferrer Martinez |
8 // | Released under the GNU Lesser General Public License, see LICENSE.txt|
9 // +----------------------------------------------------------------------+
11 /**
12 * @package ActionView
13 * @subpackage Helpers
14 * @author Bermi Ferrer <bermi a.t akelos c.om>
15 * @copyright Copyright (c) 2002-2006, Akelos Media, S.L. http://www.akelos.org
16 * @license GNU Lesser General Public License <http://www.gnu.org/copyleft/lesser.html>
20 /**
21 * Provides methods for converting a number into a formatted string that currently represents
22 * one of the following forms: phone number, percentage, money, or precision level.
24 class NumberHelper extends AkObject
26 /**
27 * Formats a +number+ into a US phone number string. The +options+ can be a array used to customize the
28 * format of the output.
29 * The area code can be surrounded by parentheses by setting +area_code+ to true; default is false
30 * The delimiter can be set using +delimiter+; default is "-"
31 * Examples:
32 * $number_helper->number_to_phone(1235551234) => 123-555-1234
33 * $number_helper->number_to_phone(1235551234, array('area_code' => true)) => (123) 555-1234
34 * $number_helper->number_to_phone(1235551234, array('delimiter' => " ")) => 123 555 1234
35 * $number_helper->number_to_phone(1235551234, array('area_code' => true, 'extension' => 555)) => (123) 555-1234 x 555
37 function number_to_phone($number, $options = array())
39 $default_options = array(
40 'area_code'=>false,
41 'delimiter' => '-',
42 'extension'=> '',
43 'extension_delimiter' => 'x'
46 $options = array_merge($default_options, $options);
48 $number = $options['area_code'] == true ? preg_replace(
49 '/([0-9]{3})([0-9]{3})([0-9]{4})/',"(\\1) \\2{$options['delimiter']}\\3",$number) :
50 preg_replace('/([0-9]{3})([0-9]{3})([0-9]{4})/',"\\1{$options['delimiter']}\\2{$options['delimiter']}\\3",$number);
52 return empty($options['extension']) ? $number : "$number {$options['extension_delimiter']} {$options['extension']}";
56 /**
57 * Formats a +number+ into a currency string. The +options+ array can be used to customize the format of the output.
58 * The +number+ can contain a level of precision using the +precision+ key; default is 2
59 * The currency type can be set using the +unit+ key; default is "$"
60 * The unit separator can be set using the +separator+ key; default is "."
61 * The delimiter can be set using the +delimiter+ key; default is ","
62 * Examples:
63 * $number_helper->number_to_currency(1234567890.50) => $1,234,567,890.50
64 * $number_helper->number_to_currency(1234567890.506) => $1,234,567,890.51
65 * $number_helper->number_to_currency(1234567890.50,
66 * array('unit' => "&pound;", 'separator' => ",", 'delimiter' => "")) => &pound;1234567890,50
67 * $number_helper->number_to_currency(1234567890.50,
68 * array('unit' => " &euro;", 'separator' => ",", 'delimiter' => ".",
69 * 'unit_position' => 'right')) => 1.234.567.890,50 &euro;
72 function number_to_currency($number, $options = array())
74 $default_options = Ak::locale('currency');
76 $options = array_merge($default_options, $options);
78 return
79 ($options['unit_position'] == 'left' ? $options['unit'] : '').
80 number_format($number, $options['precision'],$options['separator'], $options['delimiter']).
81 ($options['unit_position'] == 'right' ? $options['unit'] : '');
86 /**
87 * Formats a +number+ as into a percentage string. The +options+ array can be used to customize the format of
88 * the output.
89 * The +number+ can contain a level of precision using the +precision+ key; default is 2
90 * The unit separator can be set using the +separator+ key; default is "."
91 * Examples:
92 * $number_helper->number_to_percentage(100) => 100.00%
93 * $number_helper->number_to_percentage(100, array('precision' => 0)) => 100%
94 * $number_helper->number_to_percentage(302.0576, array('precision' => 3)) => 302.058%
96 function number_to_percentage($number, $options = array())
99 $default_options = array(
100 'precision'=>2,
101 'separator' => '.',
104 $options = array_merge($default_options, $options);
106 return number_format($number, $options['precision'],$options['separator'],'').'%';
110 * Formats a +number+ with a +delimiter+.
111 * Example:
112 * $number_helper->number_with_delimiter(12345678) => 12,345,678
114 function number_with_delimiter($number, $delimiter=',')
116 return preg_replace('/(\d)(?=(\d\d\d)+(?!\d))/', "\\1{$delimiter}", $number);
120 * Returns a formatted-for-humans file size.
122 * Examples:
123 * $number_helper->human_size(123) => 123 Bytes
124 * $number_helper->human_size(1234) => 1.2 KB
125 * $number_helper->human_size(12345) => 12.1 KB
126 * $number_helper->human_size(1234567) => 1.2 MB
127 * $number_helper->human_size(1234567890) => 1.1 GB
130 function number_to_human_size($size, $decimal = 1)
132 if(is_numeric($size )){
133 $position = 0;
134 $units = array( ' Bytes', ' KB', ' MB', ' GB', ' TB', ' PB', ' EB', ' ZB', ' YB');
135 while( $size >= 1024 && ( $size / 1024 ) >= 1 ) {
136 $size /= 1024;
137 $position++;
139 return round( $size, $decimal ) . $units[$position];
140 }else {
141 return '0 Bytes';
144 function human_size($size)
146 return NumberHelper::number_to_human_size($size);
149 function human_size_to_bytes($size)
151 $units = array('BYTE','KB','MB','GB','TB');
152 $size = str_replace(array('BYTE','KILOBYTE','MEGABYTE','GIGABYTE','TERABYTE'), $units, rtrim(strtoupper($size),'S'));
153 if(preg_match('/([0-9\.]+) ?('.join('|',$units).')/', $size, $match)){
154 return intval(ceil((double)$match[1] * pow(1024,array_search($match[2], $units))));
156 return 0;
161 * Formats a +number+ with a level of +precision+.
162 * Example:
163 * $number_helper->number_with_precision(111.2345) => 111.235
166 function number_with_precision($number, $precision=3)
169 * @todo fix number rounding. Precision on linux boxes rounds to the lower (Mac and Windows work as the example)
171 if(strstr($number,'.')){
172 $decimal = round(substr($number,strpos($number,'.')+1),$precision+1);
173 if(substr($decimal , -1) == 5){
174 $number = substr($number,0,strpos($number,'.')) .'.'. ($decimal+1);
178 return round( $number, $precision );
183 * Add zeros to the begining of the string until it reaches desired length
184 * Example:
185 * $number_helper->zeropad(123, 6) => 000123
187 function zeropad($number, $length)
189 return str_pad($number, $length*-1, '0');