Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
13532 anikendra 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
 * @return mixed Whatever is returned by called method, or false on failure
70
 */
71
	public function __call($method, $params) {
72
		return call_user_func_array(array($this->_engine, $method), $params);
73
	}
74
 
75
/**
76
 * @see CakeNumber::precision()
77
 *
78
 * @param float $number A floating point number.
79
 * @param integer $precision The precision of the returned number.
80
 * @return float Formatted float.
81
 * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/number.html#NumberHelper::precision
82
 */
83
	public function precision($number, $precision = 3) {
84
		return $this->_engine->precision($number, $precision);
85
	}
86
 
87
/**
88
 * @see CakeNumber::toReadableSize()
89
 *
90
 * @param integer $size Size in bytes
91
 * @return string Human readable size
92
 * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/number.html#NumberHelper::toReadableSize
93
 */
94
	public function toReadableSize($size) {
95
		return $this->_engine->toReadableSize($size);
96
	}
97
 
98
/**
99
 * @see CakeNumber::toPercentage()
100
 *
101
 * @param float $number A floating point number
102
 * @param integer $precision The precision of the returned number
103
 * @param array $options Options
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, $options = array()) {
108
		return $this->_engine->toPercentage($number, $precision, $options);
109
	}
110
 
111
/**
112
 * @see CakeNumber::format()
113
 *
114
 * @param float $number A floating point number
115
 * @param integer $options If integer 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
}