2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
2008-02-25 03:10:37 +01:00
|
|
|
/**
|
|
|
|
* Represents a decimal field from 0-1 containing a percentage value.
|
2010-05-05 08:50:54 +02:00
|
|
|
*
|
|
|
|
* Example instantiation in {@link DataObject::$db}:
|
|
|
|
* <code>
|
|
|
|
* static $db = array(
|
|
|
|
* "SuccessRatio" => "Percentage",
|
|
|
|
* "ReallyAccurate" => "Percentage(6)",
|
|
|
|
* );
|
|
|
|
* </code>
|
|
|
|
*
|
2008-02-25 03:10:37 +01:00
|
|
|
* @package sapphire
|
|
|
|
* @subpackage model
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
class Percentage extends Decimal {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new Decimal field.
|
|
|
|
*/
|
|
|
|
function __construct($name, $precision = 4) {
|
2009-02-03 21:29:21 +01:00
|
|
|
if(!$precision) $precision = 4;
|
2007-07-19 12:40:28 +02:00
|
|
|
|
2009-07-30 01:58:27 +02:00
|
|
|
parent::__construct($name, $precision + 1, $precision);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
2010-05-05 08:50:54 +02:00
|
|
|
/**
|
|
|
|
* Returns the number, expressed as a percentage. For example, “36.30%”
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
function Nice() {
|
|
|
|
return number_format($this->value * 100, $this->decimalSize - 2) . '%';
|
|
|
|
}
|
2009-07-30 01:58:27 +02:00
|
|
|
|
|
|
|
function saveInto($dataObject) {
|
|
|
|
parent::saveInto($dataObject);
|
|
|
|
|
|
|
|
$fieldName = $this->name;
|
|
|
|
if($fieldName && $dataObject->$fieldName > 1.0) {
|
|
|
|
$dataObject->$fieldName = 1.0;
|
|
|
|
}
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
2009-07-30 01:58:27 +02:00
|
|
|
?>
|