~tsep-dev/tsep/0.9-beta

« back to all changes in this revision

Viewing changes to branches/symfony/cake/libs/view/helpers/number.php

  • Committer: geoffreyfishing
  • Date: 2011-01-11 23:46:12 UTC
  • Revision ID: svn-v4:ae0de26e-ed09-4cbe-9a20-e40b4c60ac6c::125
Created a symfony branch for future migration to symfony

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
/**
 
3
 * Number Helper.
 
4
 *
 
5
 * Methods to make numbers more readable.
 
6
 *
 
7
 * PHP versions 4 and 5
 
8
 *
 
9
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
 
10
 * Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
 
11
 *
 
12
 * Licensed under The MIT License
 
13
 * Redistributions of files must retain the above copyright notice.
 
14
 *
 
15
 * @copyright     Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
 
16
 * @link          http://cakephp.org CakePHP(tm) Project
 
17
 * @package       cake
 
18
 * @subpackage    cake.cake.libs.view.helpers
 
19
 * @since         CakePHP(tm) v 0.10.0.1076
 
20
 * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
 
21
 */
 
22
 
 
23
/**
 
24
 * Number helper library.
 
25
 *
 
26
 * Methods to make numbers more readable.
 
27
 *
 
28
 * @package       cake
 
29
 * @subpackage    cake.cake.libs.view.helpers
 
30
 * @link http://book.cakephp.org/view/1452/Number
 
31
 */
 
32
class NumberHelper extends AppHelper {
 
33
 
 
34
/**
 
35
 * Currencies supported by the helper.  You can add additional currency formats
 
36
 * with NumberHelper::addFormat
 
37
 *
 
38
 * @var array
 
39
 * @access protected
 
40
 */
 
41
        var $_currencies = array(
 
42
                'USD' => array(
 
43
                        'before' => '$', 'after' => 'c', 'zero' => 0, 'places' => 2, 'thousands' => ',',
 
44
                        'decimals' => '.', 'negative' => '()', 'escape' => true
 
45
                ),
 
46
                'GBP' => array(
 
47
                        'before'=>'&#163;', 'after' => 'p', 'zero' => 0, 'places' => 2, 'thousands' => ',',
 
48
                        'decimals' => '.', 'negative' => '()','escape' => false
 
49
                ),
 
50
                'EUR' => array(
 
51
                        'before'=>'&#8364;', 'after' => 'c', 'zero' => 0, 'places' => 2, 'thousands' => '.',
 
52
                        'decimals' => ',', 'negative' => '()', 'escape' => false
 
53
                )
 
54
        );
 
55
 
 
56
/**
 
57
 * Default options for currency formats
 
58
 *
 
59
 * @var array
 
60
 * @access protected
 
61
 */
 
62
        var $_currencyDefaults = array(
 
63
                'before'=>'', 'after' => '', 'zero' => '0', 'places' => 2, 'thousands' => ',',
 
64
                'decimals' => '.','negative' => '()', 'escape' => true
 
65
        );
 
66
 
 
67
/**
 
68
 * Formats a number with a level of precision.
 
69
 *
 
70
 * @param float $number A floating point number.
 
71
 * @param integer $precision The precision of the returned number.
 
72
 * @return float Formatted float.
 
73
 * @access public
 
74
 * @link http://book.cakephp.org/view/1454/precision
 
75
 */
 
76
        function precision($number, $precision = 3) {
 
77
                return sprintf("%01.{$precision}f", $number);
 
78
        }
 
79
 
 
80
/**
 
81
 * Returns a formatted-for-humans file size.
 
82
 *
 
83
 * @param integer $length Size in bytes
 
84
 * @return string Human readable size
 
85
 * @access public
 
86
 * @link http://book.cakephp.org/view/1456/toReadableSize
 
87
 */
 
88
        function toReadableSize($size) {
 
89
                switch (true) {
 
90
                        case $size < 1024:
 
91
                                return sprintf(__n('%d Byte', '%d Bytes', $size, true), $size);
 
92
                        case round($size / 1024) < 1024:
 
93
                                return sprintf(__('%d KB', true), $this->precision($size / 1024, 0));
 
94
                        case round($size / 1024 / 1024, 2) < 1024:
 
95
                                return sprintf(__('%.2f MB', true), $this->precision($size / 1024 / 1024, 2));
 
96
                        case round($size / 1024 / 1024 / 1024, 2) < 1024:
 
97
                                return sprintf(__('%.2f GB', true), $this->precision($size / 1024 / 1024 / 1024, 2));
 
98
                        default:
 
99
                                return sprintf(__('%.2f TB', true), $this->precision($size / 1024 / 1024 / 1024 / 1024, 2));
 
100
                }
 
101
        }
 
102
 
 
103
/**
 
104
 * Formats a number into a percentage string.
 
105
 *
 
106
 * @param float $number A floating point number
 
107
 * @param integer $precision The precision of the returned number
 
108
 * @return string Percentage string
 
109
 * @access public
 
110
 * @link http://book.cakephp.org/view/1455/toPercentage
 
111
 */
 
112
        function toPercentage($number, $precision = 2) {
 
113
                return $this->precision($number, $precision) . '%';
 
114
        }
 
115
 
 
116
/**
 
117
 * Formats a number into a currency format.
 
118
 *
 
119
 * @param float $number A floating point number
 
120
 * @param integer $options if int then places, if string then before, if (,.-) then use it
 
121
 *   or array with places and before keys
 
122
 * @return string formatted number
 
123
 * @access public
 
124
 * @link http://book.cakephp.org/view/1457/format
 
125
 */
 
126
        function format($number, $options = false) {
 
127
                $places = 0;
 
128
                if (is_int($options)) {
 
129
                        $places = $options;
 
130
                }
 
131
 
 
132
                $separators = array(',', '.', '-', ':');
 
133
 
 
134
                $before = $after = null;
 
135
                if (is_string($options) && !in_array($options, $separators)) {
 
136
                        $before = $options;
 
137
                }
 
138
                $thousands = ',';
 
139
                if (!is_array($options) && in_array($options, $separators)) {
 
140
                        $thousands = $options;
 
141
                }
 
142
                $decimals = '.';
 
143
                if (!is_array($options) && in_array($options, $separators)) {
 
144
                        $decimals = $options;
 
145
                }
 
146
 
 
147
                $escape = true;
 
148
                if (is_array($options)) {
 
149
                        $options = array_merge(array('before'=>'$', 'places' => 2, 'thousands' => ',', 'decimals' => '.'), $options);
 
150
                        extract($options);
 
151
                }
 
152
 
 
153
                $out = $before . number_format($number, $places, $decimals, $thousands) . $after;
 
154
 
 
155
                if ($escape) {
 
156
                        return h($out);
 
157
                }
 
158
                return $out;
 
159
        }
 
160
 
 
161
/**
 
162
 * Formats a number into a currency format.
 
163
 *
 
164
 * ### Options
 
165
 *
 
166
 * - `before` - The currency symbol to place before whole numbers ie. '$'
 
167
 * - `after` - The currency symbol to place after decimal numbers ie. 'c'. Set to boolean false to 
 
168
 *    use no decimal symbol.  eg. 0.35 => $0.35.
 
169
 * - `zero` - The text to use for zero values, can be a string or a number. ie. 0, 'Free!'
 
170
 * - `places` - Number of decimal places to use. ie. 2
 
171
 * - `thousands` - Thousands separator ie. ','
 
172
 * - `decimals` - Decimal separator symbol ie. '.'
 
173
 * - `negative` - Symbol for negative numbers. If equal to '()', the number will be wrapped with ( and )
 
174
 * - `escape` - Should the output be htmlentity escaped? Defaults to true
 
175
 *
 
176
 * @param float $number
 
177
 * @param string $currency Shortcut to default options. Valid values are 'USD', 'EUR', 'GBP', otherwise
 
178
 *   set at least 'before' and 'after' options.
 
179
 * @param array $options
 
180
 * @return string Number formatted as a currency.
 
181
 * @access public
 
182
 * @link http://book.cakephp.org/view/1453/currency
 
183
 */
 
184
        function currency($number, $currency = 'USD', $options = array()) {
 
185
                $default = $this->_currencyDefaults;
 
186
 
 
187
                if (isset($this->_currencies[$currency])) {
 
188
                        $default = $this->_currencies[$currency];
 
189
                } elseif (is_string($currency)) {
 
190
                        $options['before'] = $currency;
 
191
                }
 
192
 
 
193
                $options = array_merge($default, $options);
 
194
 
 
195
                $result = null;
 
196
 
 
197
                if ($number == 0 ) {
 
198
                        if ($options['zero'] !== 0 ) {
 
199
                                return $options['zero'];
 
200
                        }
 
201
                        $options['after'] = null;
 
202
                } elseif ($number < 1 && $number > -1 ) {
 
203
                        if ($options['after'] !== false) {
 
204
                                $multiply = intval('1' . str_pad('', $options['places'], '0'));
 
205
                                $number = $number * $multiply;
 
206
                                $options['before'] = null;
 
207
                                $options['places'] = null;
 
208
                        }
 
209
                } elseif (empty($options['before'])) {
 
210
                        $options['before'] = null;
 
211
                } else {
 
212
                        $options['after'] = null;
 
213
                }
 
214
 
 
215
                $abs = abs($number);
 
216
                $result = $this->format($abs, $options);
 
217
 
 
218
                if ($number < 0 ) {
 
219
                        if ($options['negative'] == '()') {
 
220
                                $result = '(' . $result .')';
 
221
                        } else {
 
222
                                $result = $options['negative'] . $result;
 
223
                        }
 
224
                }
 
225
                return $result;
 
226
        }
 
227
 
 
228
/**
 
229
 * Add a currency format to the Number helper.  Makes reusing
 
230
 * currency formats easier.
 
231
 *
 
232
 * {{{ $number->addFormat('NOK', array('before' => 'Kr. ')); }}}
 
233
 * 
 
234
 * You can now use `NOK` as a shortform when formatting currency amounts.
 
235
 *
 
236
 * {{{ $number->currency($value, 'NOK'); }}}
 
237
 *
 
238
 * Added formats are merged with the following defaults.
 
239
 *
 
240
 * {{{
 
241
 *      array(
 
242
 *              'before' => '$', 'after' => 'c', 'zero' => 0, 'places' => 2, 'thousands' => ',',
 
243
 *              'decimals' => '.', 'negative' => '()', 'escape' => true
 
244
 *      )
 
245
 * }}}
 
246
 *
 
247
 * @param string $formatName The format name to be used in the future.
 
248
 * @param array $options The array of options for this format.
 
249
 * @return void
 
250
 * @see NumberHelper::currency()
 
251
 * @access public
 
252
 */
 
253
        function addFormat($formatName, $options) {
 
254
                $this->_currencies[$formatName] = $options + $this->_currencyDefaults;
 
255
        }
 
256
 
 
257
}