1: <?php
2: /**
3: * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
4: * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
5: *
6: * Licensed under The MIT License
7: * For full copyright and license information, please see the LICENSE.txt
8: * Redistributions of files must retain the above copyright notice.
9: *
10: * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
11: * @link https://cakephp.org CakePHP(tm) Project
12: * @since 0.10.0
13: * @license https://opensource.org/licenses/mit-license.php MIT License
14: */
15: namespace Cake\View\Helper;
16:
17: use Cake\Core\App;
18: use Cake\Core\Exception\Exception;
19: use Cake\View\Helper;
20: use Cake\View\View;
21:
22: /**
23: * Number helper library.
24: *
25: * Methods to make numbers more readable.
26: *
27: * @link https://book.cakephp.org/3.0/en/views/helpers/number.html
28: * @see \Cake\I18n\Number
29: */
30: class NumberHelper extends Helper
31: {
32:
33: /**
34: * Default config for this class
35: *
36: * @var array
37: */
38: protected $_defaultConfig = [
39: 'engine' => 'Cake\I18n\Number'
40: ];
41:
42: /**
43: * Cake\I18n\Number instance
44: *
45: * @var \Cake\I18n\Number
46: */
47: protected $_engine;
48:
49: /**
50: * Default Constructor
51: *
52: * ### Settings:
53: *
54: * - `engine` Class name to use to replace Cake\I18n\Number functionality
55: * The class needs to be placed in the `Utility` directory.
56: *
57: * @param \Cake\View\View $View The View this helper is being attached to.
58: * @param array $config Configuration settings for the helper
59: * @throws \Cake\Core\Exception\Exception When the engine class could not be found.
60: */
61: public function __construct(View $View, array $config = [])
62: {
63: parent::__construct($View, $config);
64:
65: $config = $this->_config;
66:
67: $engineClass = App::className($config['engine'], 'Utility');
68: if ($engineClass) {
69: $this->_engine = new $engineClass($config);
70: } else {
71: throw new Exception(sprintf('Class for %s could not be found', $config['engine']));
72: }
73: }
74:
75: /**
76: * Call methods from Cake\I18n\Number utility class
77: *
78: * @param string $method Method to invoke
79: * @param array $params Array of params for the method.
80: * @return mixed Whatever is returned by called method, or false on failure
81: */
82: public function __call($method, $params)
83: {
84: return call_user_func_array([$this->_engine, $method], $params);
85: }
86:
87: /**
88: * Formats a number with a level of precision.
89: *
90: * @param float $number A floating point number.
91: * @param int $precision The precision of the returned number.
92: * @return string Formatted float.
93: * @see \Cake\I18n\Number::precision()
94: * @link https://book.cakephp.org/3.0/en/views/helpers/number.html#formatting-floating-point-numbers
95: */
96: public function precision($number, $precision = 3)
97: {
98: return $this->_engine->precision($number, $precision);
99: }
100:
101: /**
102: * Returns a formatted-for-humans file size.
103: *
104: * @param int $size Size in bytes
105: * @return string Human readable size
106: * @see \Cake\I18n\Number::toReadableSize()
107: * @link https://book.cakephp.org/3.0/en/views/helpers/number.html#interacting-with-human-readable-values
108: */
109: public function toReadableSize($size)
110: {
111: return $this->_engine->toReadableSize($size);
112: }
113:
114: /**
115: * Formats a number into a percentage string.
116: *
117: * Options:
118: *
119: * - `multiply`: Multiply the input value by 100 for decimal percentages.
120: *
121: * @param float $number A floating point number
122: * @param int $precision The precision of the returned number
123: * @param array $options Options
124: * @return string Percentage string
125: * @see \Cake\I18n\Number::toPercentage()
126: * @link https://book.cakephp.org/3.0/en/views/helpers/number.html#formatting-percentages
127: */
128: public function toPercentage($number, $precision = 2, array $options = [])
129: {
130: return $this->_engine->toPercentage($number, $precision, $options);
131: }
132:
133: /**
134: * Formats a number into the correct locale format
135: *
136: * Options:
137: *
138: * - `places` - Minimum number or decimals to use, e.g 0
139: * - `precision` - Maximum Number of decimal places to use, e.g. 2
140: * - `locale` - The locale name to use for formatting the number, e.g. fr_FR
141: * - `before` - The string to place before whole numbers, e.g. '['
142: * - `after` - The string to place after decimal numbers, e.g. ']'
143: * - `escape` - Whether or not to escape html in resulting string
144: *
145: * @param float $number A floating point number.
146: * @param array $options An array with options.
147: * @return string Formatted number
148: * @link https://book.cakephp.org/3.0/en/views/helpers/number.html#formatting-numbers
149: */
150: public function format($number, array $options = [])
151: {
152: $formatted = $this->_engine->format($number, $options);
153: $options += ['escape' => true];
154:
155: return $options['escape'] ? h($formatted) : $formatted;
156: }
157:
158: /**
159: * Formats a number into a currency format.
160: *
161: * ### Options
162: *
163: * - `locale` - The locale name to use for formatting the number, e.g. fr_FR
164: * - `fractionSymbol` - The currency symbol to use for fractional numbers.
165: * - `fractionPosition` - The position the fraction symbol should be placed
166: * valid options are 'before' & 'after'.
167: * - `before` - Text to display before the rendered number
168: * - `after` - Text to display after the rendered number
169: * - `zero` - The text to use for zero values, can be a string or a number. e.g. 0, 'Free!'
170: * - `places` - Number of decimal places to use. e.g. 2
171: * - `precision` - Maximum Number of decimal places to use, e.g. 2
172: * - `pattern` - An ICU number pattern to use for formatting the number. e.g #,##0.00
173: * - `useIntlCode` - Whether or not to replace the currency symbol with the international
174: * currency code.
175: * - `escape` - Whether or not to escape html in resulting string
176: *
177: * @param float $number Value to format.
178: * @param string|null $currency International currency name such as 'USD', 'EUR', 'JPY', 'CAD'
179: * @param array $options Options list.
180: * @return string Number formatted as a currency.
181: */
182: public function currency($number, $currency = null, array $options = [])
183: {
184: $formatted = $this->_engine->currency($number, $currency, $options);
185: $options += ['escape' => true];
186:
187: return $options['escape'] ? h($formatted) : $formatted;
188: }
189:
190: /**
191: * Formats a number into the correct locale format to show deltas (signed differences in value).
192: *
193: * ### Options
194: *
195: * - `places` - Minimum number or decimals to use, e.g 0
196: * - `precision` - Maximum Number of decimal places to use, e.g. 2
197: * - `locale` - The locale name to use for formatting the number, e.g. fr_FR
198: * - `before` - The string to place before whole numbers, e.g. '['
199: * - `after` - The string to place after decimal numbers, e.g. ']'
200: * - `escape` - Set to false to prevent escaping
201: *
202: * @param float $value A floating point number
203: * @param array $options Options list.
204: * @return string formatted delta
205: */
206: public function formatDelta($value, array $options = [])
207: {
208: $formatted = $this->_engine->formatDelta($value, $options);
209: $options += ['escape' => true];
210:
211: return $options['escape'] ? h($formatted) : $formatted;
212: }
213:
214: /**
215: * Getter/setter for default currency
216: *
217: * @param string|bool $currency Default currency string to be used by currency()
218: * if $currency argument is not provided. If boolean false is passed, it will clear the
219: * currently stored value
220: * @return string Currency
221: */
222: public function defaultCurrency($currency)
223: {
224: return $this->_engine->defaultCurrency($currency);
225: }
226:
227: /**
228: * Event listeners.
229: *
230: * @return array
231: */
232: public function implementedEvents()
233: {
234: return [];
235: }
236:
237: /**
238: * Formats a number into locale specific ordinal suffix.
239: *
240: * @param int|float $value An integer
241: * @param array $options An array with options.
242: * @return string formatted number
243: */
244: public function ordinal($value, array $options = [])
245: {
246: return $this->_engine->ordinal($value, $options);
247: }
248: }
249: