mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-09-18 23:46:21 +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)
45 lines
927 B
PHP
45 lines
927 B
PHP
<?php
|
|
/**
|
|
* Represents a decimal field from 0-1 containing a percentage value.
|
|
*
|
|
* Example instantiation in {@link DataObject::$db}:
|
|
* <code>
|
|
* static $db = array(
|
|
* "SuccessRatio" => "Percentage",
|
|
* "ReallyAccurate" => "Percentage(6)",
|
|
* );
|
|
* </code>
|
|
*
|
|
* @package framework
|
|
* @subpackage model
|
|
*/
|
|
class Percentage extends Decimal {
|
|
|
|
/**
|
|
* Create a new Decimal field.
|
|
*/
|
|
function __construct($name = null, $precision = 4) {
|
|
if(!$precision) $precision = 4;
|
|
|
|
parent::__construct($name, $precision + 1, $precision);
|
|
}
|
|
|
|
/**
|
|
* Returns the number, expressed as a percentage. For example, “36.30%”
|
|
*/
|
|
function Nice() {
|
|
return number_format($this->value * 100, $this->decimalSize - 2) . '%';
|
|
}
|
|
|
|
function saveInto($dataObject) {
|
|
parent::saveInto($dataObject);
|
|
|
|
$fieldName = $this->name;
|
|
if($fieldName && $dataObject->$fieldName > 1.0) {
|
|
$dataObject->$fieldName = 1.0;
|
|
}
|
|
}
|
|
}
|
|
|
|
|