2008-05-30 11:40:08 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Number Helper.
|
|
|
|
*
|
|
|
|
* Methods to make numbers more readable.
|
|
|
|
*
|
2009-11-06 06:46:59 +00:00
|
|
|
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
2013-02-08 11:59:49 +00:00
|
|
|
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
2013-02-08 12:22:51 +00:00
|
|
|
* For full copyright and license information, please see the LICENSE.txt
|
2008-05-30 11:40:08 +00:00
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
2013-02-08 11:59:49 +00:00
|
|
|
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2009-11-06 06:00:11 +00:00
|
|
|
* @link http://cakephp.org CakePHP(tm) Project
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.View.Helper
|
2008-10-30 17:30:26 +00:00
|
|
|
* @since CakePHP(tm) v 0.10.0.1076
|
2013-05-30 22:11:14 +00:00
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2012-02-07 01:03:21 +00:00
|
|
|
App::uses('CakeNumber', 'Utility');
|
2010-12-04 18:10:24 +00:00
|
|
|
App::uses('AppHelper', 'View/Helper');
|
2012-09-13 15:20:26 +00:00
|
|
|
App::uses('Hash', 'Utility');
|
2010-12-04 18:10:24 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Number helper library.
|
|
|
|
*
|
|
|
|
* Methods to make numbers more readable.
|
|
|
|
*
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.View.Helper
|
2011-10-15 17:06:19 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/number.html
|
2012-02-07 01:03:21 +00:00
|
|
|
* @see CakeNumber
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
|
|
|
class NumberHelper extends AppHelper {
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2012-03-03 22:10:12 +00:00
|
|
|
/**
|
|
|
|
* CakeNumber instance
|
|
|
|
*
|
|
|
|
* @var CakeNumber
|
2012-02-14 16:26:55 +00:00
|
|
|
*/
|
2012-02-14 16:14:18 +00:00
|
|
|
protected $_engine = null;
|
2012-02-07 01:03:21 +00:00
|
|
|
|
2012-03-03 22:10:12 +00:00
|
|
|
/**
|
|
|
|
* Default Constructor
|
2012-02-14 16:26:55 +00:00
|
|
|
*
|
|
|
|
* ### Settings:
|
|
|
|
*
|
|
|
|
* - `engine` Class name to use to replace CakeNumber functionality
|
|
|
|
* The class needs to be placed in the `Utility` directory.
|
|
|
|
*
|
|
|
|
* @param View $View The View this helper is being attached to.
|
|
|
|
* @param array $settings Configuration settings for the helper
|
2012-03-03 22:10:12 +00:00
|
|
|
* @throws CakeException When the engine class could not be found.
|
2012-02-14 16:26:55 +00:00
|
|
|
*/
|
2012-02-17 07:13:12 +00:00
|
|
|
public function __construct(View $View, $settings = array()) {
|
2012-03-11 01:57:18 +00:00
|
|
|
$settings = Hash::merge(array('engine' => 'CakeNumber'), $settings);
|
2012-02-07 01:03:21 +00:00
|
|
|
parent::__construct($View, $settings);
|
2012-02-14 15:56:48 +00:00
|
|
|
list($plugin, $engineClass) = pluginSplit($settings['engine'], true);
|
|
|
|
App::uses($engineClass, $plugin . 'Utility');
|
2012-02-13 02:13:39 +00:00
|
|
|
if (class_exists($engineClass)) {
|
2012-02-14 16:14:18 +00:00
|
|
|
$this->_engine = new $engineClass($settings);
|
2012-02-13 02:13:39 +00:00
|
|
|
} else {
|
|
|
|
throw new CakeException(__d('cake_dev', '%s could not be found', $engineClass));
|
|
|
|
}
|
2012-02-07 01:03:21 +00:00
|
|
|
}
|
2009-09-11 02:54:49 +00:00
|
|
|
|
2012-02-14 16:26:55 +00:00
|
|
|
/**
|
|
|
|
* Call methods from CakeNumber utility class
|
2014-04-06 20:27:51 +00:00
|
|
|
*
|
2014-05-29 19:01:20 +00:00
|
|
|
* @param string $method Method to call.
|
|
|
|
* @param array $params Parameters to pass to method.
|
2013-10-15 02:43:17 +00:00
|
|
|
* @return mixed Whatever is returned by called method, or false on failure
|
2012-02-14 16:26:55 +00:00
|
|
|
*/
|
2012-02-13 02:13:39 +00:00
|
|
|
public function __call($method, $params) {
|
2012-02-14 16:14:18 +00:00
|
|
|
return call_user_func_array(array($this->_engine, $method), $params);
|
2012-02-07 01:03:21 +00:00
|
|
|
}
|
2009-09-11 02:54:49 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2014-02-07 14:45:00 +00:00
|
|
|
* Formats a number with a level of precision.
|
|
|
|
*
|
2013-07-05 12:15:18 +00:00
|
|
|
* @param float $number A floating point number.
|
2014-07-03 13:36:42 +00:00
|
|
|
* @param int $precision The precision of the returned number.
|
2010-05-29 15:10:48 +00:00
|
|
|
* @return float Formatted float.
|
2014-05-29 19:01:20 +00:00
|
|
|
* @see CakeNumber::precision()
|
2011-10-15 17:06:19 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/number.html#NumberHelper::precision
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function precision($number, $precision = 3) {
|
2012-02-14 16:14:18 +00:00
|
|
|
return $this->_engine->precision($number, $precision);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2014-02-07 14:45:00 +00:00
|
|
|
* Returns a formatted-for-humans file size.
|
|
|
|
*
|
2014-07-03 13:36:42 +00:00
|
|
|
* @param int $size Size in bytes
|
2008-05-30 11:40:08 +00:00
|
|
|
* @return string Human readable size
|
2014-05-29 19:01:20 +00:00
|
|
|
* @see CakeNumber::toReadableSize()
|
2011-10-15 17:06:19 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/number.html#NumberHelper::toReadableSize
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function toReadableSize($size) {
|
2012-02-14 16:14:18 +00:00
|
|
|
return $this->_engine->toReadableSize($size);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2014-04-06 20:27:51 +00:00
|
|
|
* Formats a number into a percentage string.
|
|
|
|
*
|
|
|
|
* Options:
|
|
|
|
*
|
2014-02-07 14:45:00 +00:00
|
|
|
* - `multiply`: Multiply the input value by 100 for decimal percentages.
|
|
|
|
*
|
2008-05-30 11:40:08 +00:00
|
|
|
* @param float $number A floating point number
|
2014-07-03 13:36:42 +00:00
|
|
|
* @param int $precision The precision of the returned number
|
2013-07-09 19:01:55 +00:00
|
|
|
* @param array $options Options
|
2008-05-30 11:40:08 +00:00
|
|
|
* @return string Percentage string
|
2014-05-29 19:01:20 +00:00
|
|
|
* @see CakeNumber::toPercentage()
|
2011-10-15 17:06:19 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/number.html#NumberHelper::toPercentage
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2013-07-09 19:01:55 +00:00
|
|
|
public function toPercentage($number, $precision = 2, $options = array()) {
|
|
|
|
return $this->_engine->toPercentage($number, $precision, $options);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2014-02-07 14:45:00 +00:00
|
|
|
* Formats a number into a currency format.
|
|
|
|
*
|
2008-05-30 11:40:08 +00:00
|
|
|
* @param float $number A floating point number
|
2014-07-03 13:36:42 +00:00
|
|
|
* @param int $options If integer then places, if string then before, if (,.-) then use it
|
2009-02-03 18:48:28 +00:00
|
|
|
* or array with places and before keys
|
2008-05-30 11:40:08 +00:00
|
|
|
* @return string formatted number
|
2014-05-29 19:01:20 +00:00
|
|
|
* @see CakeNumber::format()
|
2011-10-15 17:06:19 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/number.html#NumberHelper::format
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function format($number, $options = false) {
|
2012-02-14 16:14:18 +00:00
|
|
|
return $this->_engine->format($number, $options);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2014-02-07 14:45:00 +00:00
|
|
|
* Formats a number into a currency format.
|
|
|
|
*
|
2014-05-29 19:01:20 +00:00
|
|
|
* @param float $number Number to format.
|
2008-06-20 11:57:56 +00:00
|
|
|
* @param string $currency Shortcut to default options. Valid values are 'USD', 'EUR', 'GBP', otherwise
|
2009-02-03 18:48:28 +00:00
|
|
|
* set at least 'before' and 'after' options.
|
2012-10-26 17:32:39 +00:00
|
|
|
* 'USD' is the default currency, use CakeNumber::defaultCurrency() to change this default.
|
2014-05-29 19:01:20 +00:00
|
|
|
* @param array $options Options list.
|
2008-05-30 11:40:08 +00:00
|
|
|
* @return string Number formatted as a currency.
|
2014-05-29 19:01:20 +00:00
|
|
|
* @see CakeNumber::currency()
|
2011-10-15 17:06:19 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/number.html#NumberHelper::currency
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2012-10-26 17:32:39 +00:00
|
|
|
public function currency($number, $currency = null, $options = array()) {
|
2012-02-14 16:14:18 +00:00
|
|
|
return $this->_engine->currency($number, $currency, $options);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-09-11 02:54:49 +00:00
|
|
|
|
|
|
|
/**
|
2014-02-07 14:45:00 +00:00
|
|
|
* Add a currency format to the Number helper. Makes reusing
|
|
|
|
* currency formats easier.
|
|
|
|
*
|
2015-01-09 12:47:25 +00:00
|
|
|
* ``` $this->Number->addFormat('NOK', array('before' => 'Kr. ')); ```
|
2014-02-07 14:45:00 +00:00
|
|
|
*
|
|
|
|
* You can now use `NOK` as a shortform when formatting currency amounts.
|
|
|
|
*
|
2015-01-09 12:47:25 +00:00
|
|
|
* ``` $this->Number->currency($value, 'NOK'); ```
|
2014-02-07 14:45:00 +00:00
|
|
|
*
|
|
|
|
* Added formats are merged with the defaults defined in Cake\Utility\Number::$_currencyDefaults
|
|
|
|
* See Cake\Utility\Number::currency() for more information on the various options and their function.
|
|
|
|
*
|
2009-09-11 02:54:49 +00:00
|
|
|
* @param string $formatName The format name to be used in the future.
|
|
|
|
* @param array $options The array of options for this format.
|
|
|
|
* @return void
|
2014-05-29 19:01:20 +00:00
|
|
|
* @see CakeNumber::addFormat()
|
2011-11-14 06:08:20 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/number.html#NumberHelper::addFormat
|
2009-11-14 12:19:25 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function addFormat($formatName, $options) {
|
2012-02-14 16:14:18 +00:00
|
|
|
return $this->_engine->addFormat($formatName, $options);
|
2009-09-11 02:54:49 +00:00
|
|
|
}
|
|
|
|
|
2012-10-26 17:32:39 +00:00
|
|
|
/**
|
2014-02-07 14:45:00 +00:00
|
|
|
* Getter/setter for default currency
|
|
|
|
*
|
2012-10-26 17:32:39 +00:00
|
|
|
* @param string $currency The currency to be used in the future.
|
2014-04-06 20:27:51 +00:00
|
|
|
* @return string Currency
|
2014-05-29 19:01:20 +00:00
|
|
|
* @see CakeNumber::defaultCurrency()
|
2012-10-26 17:32:39 +00:00
|
|
|
*/
|
|
|
|
public function defaultCurrency($currency) {
|
|
|
|
return $this->_engine->defaultCurrency($currency);
|
|
|
|
}
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|