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: * @return mixed Whatever is returned by called method, or false on failure
71: */
72: public function __call($method, $params) {
73: return call_user_func_array(array($this->_engine, $method), $params);
74: }
75:
76: /**
77: * Formats a number with a level of precision.
78: *
79: * @see CakeNumber::precision()
80: *
81: * @param float $number A floating point number.
82: * @param integer $precision The precision of the returned number.
83: * @return float Formatted float.
84: * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/number.html#NumberHelper::precision
85: */
86: public function precision($number, $precision = 3) {
87: return $this->_engine->precision($number, $precision);
88: }
89:
90: /**
91: * Returns a formatted-for-humans file size.
92: *
93: * @see CakeNumber::toReadableSize()
94: *
95: * @param integer $size Size in bytes
96: * @return string Human readable size
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: * @see CakeNumber::toPercentage()
111: *
112: * @param float $number A floating point number
113: * @param integer $precision The precision of the returned number
114: * @param array $options Options
115: * @return string Percentage string
116: * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/number.html#NumberHelper::toPercentage
117: */
118: public function toPercentage($number, $precision = 2, $options = array()) {
119: return $this->_engine->toPercentage($number, $precision, $options);
120: }
121:
122: /**
123: * Formats a number into a currency format.
124: *
125: * @see CakeNumber::format()
126: *
127: * @param float $number A floating point number
128: * @param integer $options If integer then places, if string then before, if (,.-) then use it
129: * or array with places and before keys
130: * @return string formatted number
131: * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/number.html#NumberHelper::format
132: */
133: public function format($number, $options = false) {
134: return $this->_engine->format($number, $options);
135: }
136:
137: /**
138: * Formats a number into a currency format.
139: *
140: * @see CakeNumber::currency()
141: *
142: * @param float $number
143: * @param string $currency Shortcut to default options. Valid values are 'USD', 'EUR', 'GBP', otherwise
144: * set at least 'before' and 'after' options.
145: * 'USD' is the default currency, use CakeNumber::defaultCurrency() to change this default.
146: * @param array $options
147: * @return string Number formatted as a currency.
148: * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/number.html#NumberHelper::currency
149: */
150: public function currency($number, $currency = null, $options = array()) {
151: return $this->_engine->currency($number, $currency, $options);
152: }
153:
154: /**
155: * Add a currency format to the Number helper. Makes reusing
156: * currency formats easier.
157: *
158: * {{{ $this->Number->addFormat('NOK', array('before' => 'Kr. ')); }}}
159: *
160: * You can now use `NOK` as a shortform when formatting currency amounts.
161: *
162: * {{{ $this->Number->currency($value, 'NOK'); }}}
163: *
164: * Added formats are merged with the defaults defined in Cake\Utility\Number::$_currencyDefaults
165: * See Cake\Utility\Number::currency() for more information on the various options and their function.
166: *
167: * @see CakeNumber::addFormat()
168: *
169: * @param string $formatName The format name to be used in the future.
170: * @param array $options The array of options for this format.
171: * @return void
172: * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/number.html#NumberHelper::addFormat
173: */
174: public function addFormat($formatName, $options) {
175: return $this->_engine->addFormat($formatName, $options);
176: }
177:
178: /**
179: * Getter/setter for default currency
180: *
181: * @see CakeNumber::defaultCurrency()
182: *
183: * @param string $currency The currency to be used in the future.
184: * @return string Currency
185: */
186: public function defaultCurrency($currency) {
187: return $this->_engine->defaultCurrency($currency);
188: }
189:
190: }
191: