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