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 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
11: *
12: * Licensed under The MIT License
13: * Redistributions of files must retain the above copyright notice.
14: *
15: * @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
16: * @link http://cakephp.org CakePHP(tm) Project
17: * @package Cake.View.Helper
18: * @since CakePHP(tm) v 0.10.0.1076
19: * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
20: */
21:
22: App::uses('CakeNumber', 'Utility');
23: App::uses('AppHelper', 'View/Helper');
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: public function __call($method, $params) {
71: return call_user_func_array(array($this->_engine, $method), $params);
72: }
73:
74: /**
75: * @see: CakeNumber::precision()
76: *
77: * @param float $number A floating point number.
78: * @param integer $precision The precision of the returned number.
79: * @return float Formatted float.
80: * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/number.html#NumberHelper::precision
81: */
82: public function precision($number, $precision = 3) {
83: return $this->_engine->precision($number, $precision);
84: }
85:
86: /**
87: * @see: CakeNumber::toReadableSize()
88: *
89: * @param integer $size Size in bytes
90: * @return string Human readable size
91: * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/number.html#NumberHelper::toReadableSize
92: */
93: public function toReadableSize($size) {
94: return $this->_engine->toReadableSize($size);
95: }
96:
97: /**
98: * @see: CakeNumber::toPercentage()
99: *
100: * @param float $number A floating point number
101: * @param integer $precision The precision of the returned number
102: * @return string Percentage string
103: * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/number.html#NumberHelper::toPercentage
104: */
105: public function toPercentage($number, $precision = 2) {
106: return $this->_engine->toPercentage($number, $precision);
107: }
108:
109: /**
110: * @see: CakeNumber::format()
111: *
112: * @param float $number A floating point number
113: * @param integer $options if int then places, if string then before, if (,.-) then use it
114: * or array with places and before keys
115: * @return string formatted number
116: * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/number.html#NumberHelper::format
117: */
118: public function format($number, $options = false) {
119: return $this->_engine->format($number, $options);
120: }
121:
122: /**
123: * @see: CakeNumber::currency()
124: *
125: * @param float $number
126: * @param string $currency Shortcut to default options. Valid values are 'USD', 'EUR', 'GBP', otherwise
127: * set at least 'before' and 'after' options.
128: * @param array $options
129: * @return string Number formatted as a currency.
130: * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/number.html#NumberHelper::currency
131: */
132: public function currency($number, $currency = 'USD', $options = array()) {
133: return $this->_engine->currency($number, $currency, $options);
134: }
135:
136: /**
137: * @see: CakeNumber::addFormat()
138: *
139: * @param string $formatName The format name to be used in the future.
140: * @param array $options The array of options for this format.
141: * @return void
142: * @see NumberHelper::currency()
143: * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/number.html#NumberHelper::addFormat
144: */
145: public function addFormat($formatName, $options) {
146: return $this->_engine->addFormat($formatName, $options);
147: }
148:
149: }
150: