1: <?php
2: /**
3: * Number Helper.
4: *
5: * Methods to make numbers more readable.
6: *
7: * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
8: * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
9: *
10: * Licensed under The MIT License
11: * For full copyright and license information, please see the LICENSE.txt
12: * Redistributions of files must retain the above copyright notice.
13: *
14: * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
15: * @link http://cakephp.org CakePHP(tm) Project
16: * @package Cake.View.Helper
17: * @since CakePHP(tm) v 0.10.0.1076
18: * @license http://www.opensource.org/licenses/mit-license.php MIT License
19: */
20:
21: App::uses('CakeNumber', 'Utility');
22: App::uses('AppHelper', 'View/Helper');
23: App::uses('Hash', 'Utility');
24:
25: /**
26: * Number helper library.
27: *
28: * Methods to make numbers more readable.
29: *
30: * @package Cake.View.Helper
31: * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/number.html
32: * @see CakeNumber
33: */
34: class NumberHelper extends AppHelper {
35:
36: /**
37: * CakeNumber instance
38: *
39: * @var CakeNumber
40: */
41: protected $_engine = null;
42:
43: /**
44: * Default Constructor
45: *
46: * ### Settings:
47: *
48: * - `engine` Class name to use to replace CakeNumber functionality
49: * The class needs to be placed in the `Utility` directory.
50: *
51: * @param View $View The View this helper is being attached to.
52: * @param array $settings Configuration settings for the helper
53: * @throws CakeException When the engine class could not be found.
54: */
55: public function __construct(View $View, $settings = array()) {
56: $settings = Hash::merge(array('engine' => 'CakeNumber'), $settings);
57: parent::__construct($View, $settings);
58: list($plugin, $engineClass) = pluginSplit($settings['engine'], true);
59: App::uses($engineClass, $plugin . 'Utility');
60: if (class_exists($engineClass)) {
61: $this->_engine = new $engineClass($settings);
62: } else {
63: throw new CakeException(__d('cake_dev', '%s could not be found', $engineClass));
64: }
65: }
66:
67: /**
68: * Call methods from CakeNumber utility class
69: *
70: * @param string $method Method to call.
71: * @param array $params Parameters to pass to method.
72: * @return mixed Whatever is returned by called method, or false on failure
73: */
74: public function __call($method, $params) {
75: return call_user_func_array(array($this->_engine, $method), $params);
76: }
77:
78: /**
79: * Formats a number with a level of precision.
80: *
81: * @param float $number A floating point number.
82: * @param int $precision The precision of the returned number.
83: * @return float Formatted float.
84: * @see CakeNumber::precision()
85: * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/number.html#NumberHelper::precision
86: */
87: public function precision($number, $precision = 3) {
88: return $this->_engine->precision($number, $precision);
89: }
90:
91: /**
92: * Returns a formatted-for-humans file size.
93: *
94: * @param int $size Size in bytes
95: * @return string Human readable size
96: * @see CakeNumber::toReadableSize()
97: * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/number.html#NumberHelper::toReadableSize
98: */
99: public function toReadableSize($size) {
100: return $this->_engine->toReadableSize($size);
101: }
102:
103: /**
104: * Formats a number into a percentage string.
105: *
106: * Options:
107: *
108: * - `multiply`: Multiply the input value by 100 for decimal percentages.
109: *
110: * @param float $number A floating point number
111: * @param int $precision The precision of the returned number
112: * @param array $options Options
113: * @return string Percentage string
114: * @see CakeNumber::toPercentage()
115: * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/number.html#NumberHelper::toPercentage
116: */
117: public function toPercentage($number, $precision = 2, $options = array()) {
118: return $this->_engine->toPercentage($number, $precision, $options);
119: }
120:
121: /**
122: * Formats a number into a currency format.
123: *
124: * @param float $number A floating point number
125: * @param int $options If integer then places, if string then before, if (,.-) then use it
126: * or array with places and before keys
127: * @return string formatted number
128: * @see CakeNumber::format()
129: * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/number.html#NumberHelper::format
130: */
131: public function format($number, $options = false) {
132: return $this->_engine->format($number, $options);
133: }
134:
135: /**
136: * Formats a number into a currency format.
137: *
138: * @param float $number Number to format.
139: * @param string $currency Shortcut to default options. Valid values are 'USD', 'EUR', 'GBP', otherwise
140: * set at least 'before' and 'after' options.
141: * 'USD' is the default currency, use CakeNumber::defaultCurrency() to change this default.
142: * @param array $options Options list.
143: * @return string Number formatted as a currency.
144: * @see CakeNumber::currency()
145: * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/number.html#NumberHelper::currency
146: */
147: public function currency($number, $currency = null, $options = array()) {
148: return $this->_engine->currency($number, $currency, $options);
149: }
150:
151: /**
152: * Add a currency format to the Number helper. Makes reusing
153: * currency formats easier.
154: *
155: * {{{ $this->Number->addFormat('NOK', array('before' => 'Kr. ')); }}}
156: *
157: * You can now use `NOK` as a shortform when formatting currency amounts.
158: *
159: * {{{ $this->Number->currency($value, 'NOK'); }}}
160: *
161: * Added formats are merged with the defaults defined in Cake\Utility\Number::$_currencyDefaults
162: * See Cake\Utility\Number::currency() for more information on the various options and their function.
163: *
164: * @param string $formatName The format name to be used in the future.
165: * @param array $options The array of options for this format.
166: * @return void
167: * @see CakeNumber::addFormat()
168: * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/number.html#NumberHelper::addFormat
169: */
170: public function addFormat($formatName, $options) {
171: return $this->_engine->addFormat($formatName, $options);
172: }
173:
174: /**
175: * Getter/setter for default currency
176: *
177: * @param string $currency The currency to be used in the future.
178: * @return string Currency
179: * @see CakeNumber::defaultCurrency()
180: */
181: public function defaultCurrency($currency) {
182: return $this->_engine->defaultCurrency($currency);
183: }
184:
185: }
186: