Completed
Push — 3.5 ( 1a9180...1bec8a )
by Daniel
24s
created

Float::requireField()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 0
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * Represents a floating point field.
4
 *
5
 * @package framework
6
 * @subpackage model
7
 */
8
class Float extends DBField {
9
10
	public function __construct($name = null, $defaultVal = 0) {
11
		$this->defaultVal = is_float($defaultVal) ? $defaultVal : (float) 0;
12
13
		parent::__construct($name);
14
	}
15
16
	public function requireField() {
17
		$parts = Array(
18
			'datatype'=>'float',
19
			'null'=>'not null',
20
			'default'=>$this->defaultVal,
21
			'arrayValue'=>$this->arrayValue
22
		);
23
		$values = Array('type'=>'float', 'parts'=>$parts);
24
		DB::require_field($this->tableName, $this->name, $values);
25
	}
26
27
	/**
28
	 * Returns the number, with commas and decimal places as appropriate, eg “1,000.00”.
29
	 *
30
	 * @uses number_format()
31
	 */
32
	public function Nice() {
33
		return number_format($this->value, 2);
34
	}
35
36
	public function Round($precision = 3) {
37
		return round($this->value, $precision);
38
	}
39
40
	public function NiceRound($precision = 3) {
41
		return number_format(round($this->value, $precision), $precision);
42
	}
43
44
	public function scaffoldFormField($title = null) {
45
		return new NumericField($this->name, $title);
46
	}
47
48
	public function nullValue() {
49
		return 0;
50
	}
51
52
	public function prepValueForDB($value) {
53
		if($value === true) {
54
			return 1;
55
		} elseif(empty($value) || !is_numeric($value)) {
56
			return 0;
57
		}
58
59
		return $value;
60
	}
61
62
}
63