cakephp2-php8/lib/Cake/View/Helper/NumberHelper.php

150 lines
4.6 KiB
PHP
Raw Normal View History

<?php
/**
* Number Helper.
*
* Methods to make numbers more readable.
*
2010-10-03 16:38:58 +00:00
* PHP 5
*
2009-11-06 06:46:59 +00:00
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
2012-03-13 02:46:46 +00:00
* Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
2012-03-13 02:46:46 +00:00
* @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
2009-11-06 06:00:11 +00:00
* @link http://cakephp.org CakePHP(tm) Project
* @package Cake.View.Helper
* @since CakePHP(tm) v 0.10.0.1076
2009-11-06 06:51:51 +00:00
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
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');
/**
* Number helper library.
*
* Methods to make numbers more readable.
*
* @package Cake.View.Helper
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/number.html
2012-02-07 01:03:21 +00:00
* @see CakeNumber
*/
class NumberHelper extends AppHelper {
2012-03-03 22:10:12 +00:00
/**
* CakeNumber instance
*
* @var CakeNumber
*/
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
*
* ### 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-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');
if (class_exists($engineClass)) {
2012-02-14 16:14:18 +00:00
$this->_engine = new $engineClass($settings);
} else {
throw new CakeException(__d('cake_dev', '%s could not be found', $engineClass));
}
2012-02-07 01:03:21 +00:00
}
/**
* Call methods from CakeNumber utility class
*/
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
}
/**
2012-02-07 01:03:21 +00:00
* @see: CakeNumber::precision()
*
* @param float $number A floating point number.
* @param integer $precision The precision of the returned number.
* @return float Formatted float.
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/number.html#NumberHelper::precision
*/
public function precision($number, $precision = 3) {
2012-02-14 16:14:18 +00:00
return $this->_engine->precision($number, $precision);
}
/**
2012-02-07 01:03:21 +00:00
* @see: CakeNumber::toReadableSize()
*
2011-07-29 02:45:47 +00:00
* @param integer $size Size in bytes
* @return string Human readable size
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/number.html#NumberHelper::toReadableSize
*/
public function toReadableSize($size) {
2012-02-14 16:14:18 +00:00
return $this->_engine->toReadableSize($size);
}
/**
2012-02-07 01:03:21 +00:00
* @see: CakeNumber::toPercentage()
*
* @param float $number A floating point number
* @param integer $precision The precision of the returned number
* @return string Percentage string
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/number.html#NumberHelper::toPercentage
*/
public function toPercentage($number, $precision = 2) {
2012-02-14 16:14:18 +00:00
return $this->_engine->toPercentage($number, $precision);
}
/**
2012-02-07 01:03:21 +00:00
* @see: CakeNumber::format()
*
* @param float $number A floating point number
* @param integer $options if int then places, if string then before, if (,.-) then use it
* or array with places and before keys
* @return string formatted number
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/number.html#NumberHelper::format
*/
public function format($number, $options = false) {
2012-02-14 16:14:18 +00:00
return $this->_engine->format($number, $options);
}
/**
2012-02-07 01:03:21 +00:00
* @see: CakeNumber::currency()
*
* @param float $number
* @param string $currency Shortcut to default options. Valid values are 'USD', 'EUR', 'GBP', otherwise
* set at least 'before' and 'after' options.
* @param array $options
* @return string Number formatted as a currency.
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/number.html#NumberHelper::currency
*/
public function currency($number, $currency = 'USD', $options = array()) {
2012-02-14 16:14:18 +00:00
return $this->_engine->currency($number, $currency, $options);
}
/**
2012-02-07 01:03:21 +00:00
* @see: CakeNumber::addFormat()
*
* @param string $formatName The format name to be used in the future.
* @param array $options The array of options for this format.
* @return void
* @see NumberHelper::currency()
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/number.html#NumberHelper::addFormat
*/
public function addFormat($formatName, $options) {
2012-02-14 16:14:18 +00:00
return $this->_engine->addFormat($formatName, $options);
}
}