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