silverstripe-framework/model/FieldType/DBCurrency.php
Sam Minnee aeccb8b8e0 API: Move DBField subclasses into SilverStripe\Model\FieldType namespace
API: Deprecate SS_Datetime.

The DBField subclasses are have all been renamed to start with “DB” and
be in the SilverStripe\Model\FieldType namespace. To keep DataObject
definitions concise, the original short variations of their names are
preserved as service definitions. Most of the field generation code
doesn’t need to change, but where field classes are referenced directly,
changes will be needed.

SS_Datetime, which is commonly referenced outside the model system
itself, has been preserved as a subclass of DBDatetime. This has been
marked as deprecated and can be removed in SilverStripe 5.

A few places that referred to $db and $casting values weren’t using
the Injector to instantiate the relevant classes. This meant that the
remapping we have created as part of moving classes into a namespace
didn’t work.
2016-03-22 18:09:30 +13:00

78 lines
2.0 KiB
PHP

<?php
namespace SilverStripe\Model\FieldType;
use Deprecation;
/**
* 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 DBCurrency extends DBDecimal {
/**
* @config
* @var string
*/
private static $currency_symbol = '$';
public 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”.
*/
public function Nice() {
// return "<span title=\"$this->value\">$" . number_format($this->value, 2) . '</span>';
$val = $this->config()->currency_symbol . 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”.
*/
public function Whole() {
$val = $this->config()->currency_symbol . number_format(abs($this->value), 0);
if($this->value < 0) return "($val)";
else return $val;
}
public function setValue($value, $record = null, $markChanged = true) {
$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('$',',',$this->config()->currency_symbol),'',$matches[0]);
} else {
$this->value = 0;
}
}
/**
* @deprecated 4.0 Use the "Currency.currency_symbol" config setting instead
* @param [type] $value [description]
*/
public static function setCurrencySymbol($value) {
Deprecation::notice('4.0', 'Use the "Currency.currency_symbol" config setting instead');
DBCurrency::config()->currency_symbol = $value;
}
}