mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-01-31 09:06:17 +00:00
changing methods in CakeNumber class to static
This commit is contained in:
parent
abd6d22445
commit
9624c27ef4
3 changed files with 35 additions and 22 deletions
|
@ -25,12 +25,18 @@ App::uses('NumberHelper', 'View/Helper');
|
|||
*/
|
||||
class NumberHelperTestObject extends NumberHelper {
|
||||
|
||||
public function attach(CakeNumber $cakeNumber) {
|
||||
public function attach(CakeNumberMock $cakeNumber) {
|
||||
$this->_CakeNumber = $cakeNumber;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* CakeNumberMock class
|
||||
*/
|
||||
class CakeNumberMock {
|
||||
}
|
||||
|
||||
/**
|
||||
* NumberHelperTest class
|
||||
*
|
||||
|
@ -45,10 +51,7 @@ class NumberHelperTest extends CakeTestCase {
|
|||
*/
|
||||
public function setUp() {
|
||||
parent::setUp();
|
||||
$view = $this->getMock('View', array(), array(), '', false);
|
||||
$this->CakeNumber = $this->getMock('CakeNumber');
|
||||
$this->Number = new NumberHelperTestObject($view);
|
||||
$this->Number->attach($this->CakeNumber);
|
||||
$this->View = new View(null);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -58,7 +61,7 @@ class NumberHelperTest extends CakeTestCase {
|
|||
*/
|
||||
public function tearDown() {
|
||||
parent::tearDown();
|
||||
unset($this->Number);
|
||||
unset($this->View);
|
||||
}
|
||||
|
||||
|
||||
|
@ -70,9 +73,12 @@ class NumberHelperTest extends CakeTestCase {
|
|||
'precision', 'toReadableSize', 'toPercentage', 'format',
|
||||
'currency', 'addFormat',
|
||||
);
|
||||
$CakeNumber = $this->getMock('CakeNumberMock', $methods);
|
||||
$Number = new NumberHelperTestObject($this->View, array('engine' => 'CakeNumberMock'));
|
||||
$Number->attach($CakeNumber);
|
||||
foreach ($methods as $method) {
|
||||
$this->CakeNumber->expects($this->at(0))->method($method);
|
||||
$this->Number->{$method}('who', 'what', 'when', 'where', 'how');
|
||||
$CakeNumber->expects($this->at(0))->method($method);
|
||||
$Number->{$method}('who', 'what', 'when', 'where', 'how');
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -68,7 +68,7 @@ class CakeNumber {
|
|||
* @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) {
|
||||
public static function precision($number, $precision = 3) {
|
||||
return sprintf("%01.{$precision}f", $number);
|
||||
}
|
||||
|
||||
|
@ -79,18 +79,18 @@ class CakeNumber {
|
|||
* @return string Human readable size
|
||||
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/number.html#NumberHelper::toReadableSize
|
||||
*/
|
||||
public function toReadableSize($size) {
|
||||
public static function toReadableSize($size) {
|
||||
switch (true) {
|
||||
case $size < 1024:
|
||||
return __dn('cake', '%d Byte', '%d Bytes', $size, $size);
|
||||
case round($size / 1024) < 1024:
|
||||
return __d('cake', '%d KB', $this->precision($size / 1024, 0));
|
||||
return __d('cake', '%d KB', self::precision($size / 1024, 0));
|
||||
case round($size / 1024 / 1024, 2) < 1024:
|
||||
return __d('cake', '%.2f MB', $this->precision($size / 1024 / 1024, 2));
|
||||
return __d('cake', '%.2f MB', self::precision($size / 1024 / 1024, 2));
|
||||
case round($size / 1024 / 1024 / 1024, 2) < 1024:
|
||||
return __d('cake', '%.2f GB', $this->precision($size / 1024 / 1024 / 1024, 2));
|
||||
return __d('cake', '%.2f GB', self::precision($size / 1024 / 1024 / 1024, 2));
|
||||
default:
|
||||
return __d('cake', '%.2f TB', $this->precision($size / 1024 / 1024 / 1024 / 1024, 2));
|
||||
return __d('cake', '%.2f TB', self::precision($size / 1024 / 1024 / 1024 / 1024, 2));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -102,8 +102,8 @@ class CakeNumber {
|
|||
* @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) {
|
||||
return $this->precision($number, $precision) . '%';
|
||||
public static function toPercentage($number, $precision = 2) {
|
||||
return self::precision($number, $precision) . '%';
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -115,7 +115,7 @@ class CakeNumber {
|
|||
* @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) {
|
||||
public static function format($number, $options = false) {
|
||||
$places = 0;
|
||||
if (is_int($options)) {
|
||||
$places = $options;
|
||||
|
@ -172,7 +172,7 @@ class CakeNumber {
|
|||
* @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()) {
|
||||
public static function currency($number, $currency = 'USD', $options = array()) {
|
||||
$default = self::$_currencyDefaults;
|
||||
|
||||
if (isset(self::$_currencies[$currency])) {
|
||||
|
@ -210,7 +210,7 @@ class CakeNumber {
|
|||
$options[$position] = $options[$symbolKey.'Symbol'];
|
||||
|
||||
$abs = abs($number);
|
||||
$result = $this->format($abs, $options);
|
||||
$result = self::format($abs, $options);
|
||||
|
||||
if ($number < 0 ) {
|
||||
if ($options['negative'] == '()') {
|
||||
|
@ -247,7 +247,7 @@ class CakeNumber {
|
|||
* @see NumberHelper::currency()
|
||||
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/number.html#NumberHelper::addFormat
|
||||
*/
|
||||
public function addFormat($formatName, $options) {
|
||||
public static function addFormat($formatName, $options) {
|
||||
self::$_currencies[$formatName] = $options + self::$_currencyDefaults;
|
||||
}
|
||||
|
||||
|
|
|
@ -43,14 +43,21 @@ class NumberHelper extends AppHelper {
|
|||
* @param array $settings Configuration settings for the helper
|
||||
*/
|
||||
function __construct(View $View, $settings = array()) {
|
||||
$settings = Set::merge(array('engine' => 'CakeNumber'), $settings);
|
||||
parent::__construct($View, $settings);
|
||||
$this->_CakeNumber = new CakeNumber();
|
||||
$engineClass = $settings['engine'];
|
||||
App::uses($engineClass, 'Utility');
|
||||
if (class_exists($engineClass)) {
|
||||
$this->_CakeNumber = new $engineClass($settings);
|
||||
} else {
|
||||
throw new CakeException(__d('cake_dev', '%s could not be found', $engineClass));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Call methods from CakeNumber utility class
|
||||
*/
|
||||
function __call($method, $params) {
|
||||
public function __call($method, $params) {
|
||||
return call_user_func_array(array($this->_CakeNumber, $method), $params);
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue