mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-09-19 07:56:38 +02:00
82495f5a7e
CHANGE Given that Config::inst is an implementation of the singleton pattern itself, I've removed the extra call to singleton(). A side effect of this is that it gets around a possibly nasty circular reference with the dependency injector (which relies on the config object); in future, this dependency structure should really be structured from the DI directly. MINOR Change singleton and strong_create to use dependency injector BUGFIX: Provide default constructor values for classes (fixes issues when used in 'singleton' scenario during dev/build in particular) MINOR Clear out injector state when resetting db schema during tests (a follow on from changing singleton() calls to use the injector underneath)
62 lines
1.6 KiB
PHP
62 lines
1.6 KiB
PHP
<?php
|
|
/**
|
|
* Represents a decimal field containing a currency amount.
|
|
* The currency class only supports single currencies. For multi-currency support, use {@link Money}
|
|
*
|
|
*
|
|
* Example definition via {@link DataObject::$db}:
|
|
* <code>
|
|
* static $db = array(
|
|
* "Price" => "Currency",
|
|
* "Tax" => "Currency(5)",
|
|
* );
|
|
* </code>
|
|
*
|
|
* @package framework
|
|
* @subpackage model
|
|
*/
|
|
class Currency extends Decimal {
|
|
protected static $currencySymbol = '$';
|
|
|
|
function __construct($name = null, $wholeSize = 9, $decimalSize = 2, $defaultValue = 0) {
|
|
parent::__construct($name, $wholeSize, $decimalSize, $defaultValue);
|
|
}
|
|
|
|
/**
|
|
* Returns the number as a currency, eg “$1,000.00”.
|
|
*/
|
|
function Nice() {
|
|
// return "<span title=\"$this->value\">$" . number_format($this->value, 2) . '</span>';
|
|
$val = self::$currencySymbol . number_format(abs($this->value), 2);
|
|
if($this->value < 0) return "($val)";
|
|
else return $val;
|
|
}
|
|
|
|
/**
|
|
* Returns the number as a whole-number currency, eg “$1,000”.
|
|
*/
|
|
function Whole() {
|
|
$val = self::$currencySymbol . number_format(abs($this->value), 0);
|
|
if($this->value < 0) return "($val)";
|
|
else return $val;
|
|
}
|
|
|
|
function setValue($value, $record = null) {
|
|
$matches = null;
|
|
if(is_numeric($value)) {
|
|
$this->value = $value;
|
|
|
|
} else if(preg_match('/-?\$?[0-9,]+(.[0-9]+)?([Ee][0-9]+)?/', $value, $matches)) {
|
|
$this->value = str_replace(array('$',',',self::$currencySymbol),'',$matches[0]);
|
|
|
|
} else {
|
|
$this->value = 0;
|
|
}
|
|
}
|
|
|
|
static function setCurrencySymbol($value) {
|
|
self::$currencySymbol = $value;
|
|
}
|
|
}
|
|
|