silverstripe-framework/model/FieldType/DBPercentage.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

48 lines
986 B
PHP

<?php
namespace SilverStripe\Model\FieldType;
/**
* 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 DBPercentage extends DBDecimal {
/**
* Create a new Decimal field.
*/
public 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%”
*/
public function Nice() {
return number_format($this->value * 100, $this->decimalSize - 2) . '%';
}
public function saveInto($dataObject) {
parent::saveInto($dataObject);
$fieldName = $this->name;
if($fieldName && $dataObject->$fieldName > 1.0) {
$dataObject->$fieldName = 1.0;
}
}
}